mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-01-21 07:55:00 +00:00
email, plinth.log: Write more information to syslog
sudo journalctl -b -o short-monotonic --no-pager -f
This commit is contained in:
parent
7397326d57
commit
aab3fe9c02
@ -6,11 +6,15 @@ import logging
|
||||
import os
|
||||
import sys
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
import plinth.log
|
||||
|
||||
EXIT_SYNTAX = 10
|
||||
EXIT_PERM = 20
|
||||
|
||||
# Set up logging
|
||||
plinth.log.pipe_to_syslog(to_stderr='tty')
|
||||
logger = logging.getLogger(os.path.basename(__file__))
|
||||
|
||||
|
||||
def reserved_for_root(fun):
|
||||
def wrapped(*args, **kwargs):
|
||||
@ -22,6 +26,10 @@ def reserved_for_root(fun):
|
||||
|
||||
|
||||
def main():
|
||||
if not sys.stdin.isatty():
|
||||
print('WARNING: Output will not be shown. Check syslog for logs',
|
||||
file=sys.stderr)
|
||||
|
||||
parser = argparse.ArgumentParser()
|
||||
group = parser.add_mutually_exclusive_group(required=True)
|
||||
group.add_argument('-i', nargs=2, dest='ipc')
|
||||
@ -32,7 +40,12 @@ def main():
|
||||
subcommand, arguments = next(generator)
|
||||
|
||||
function = globals()['subcommand_' + subcommand]
|
||||
function(*arguments)
|
||||
try:
|
||||
function(*arguments)
|
||||
except Exception as e:
|
||||
logger.exception(e)
|
||||
_log_additional_info()
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
@reserved_for_root
|
||||
@ -56,13 +69,25 @@ def subcommand_ipc(module_name, action_name):
|
||||
def subcommand_touch_file(path):
|
||||
import pathlib
|
||||
|
||||
if os.getuid == 0:
|
||||
logger.critical('Do not run as root')
|
||||
if os.getuid() == 0:
|
||||
logger.critical('Do not run the `-t` option as root')
|
||||
sys.exit(EXIT_PERM)
|
||||
|
||||
# mode is influenced by umask
|
||||
pathlib.Path(path).touch(mode=0o660, exist_ok=True)
|
||||
|
||||
|
||||
def _log_additional_info():
|
||||
import grp
|
||||
import pwd
|
||||
resu = ','.join(pwd.getpwuid(uid).pw_name for uid in os.getresuid())
|
||||
resg = ','.join(grp.getgrgid(gid).gr_name for gid in os.getresgid())
|
||||
pyver = sys.version.replace('\n', ' ')
|
||||
logger.error('--- Additional Information ---')
|
||||
logger.error('resuid=%s, resgid=%s', resu, resg)
|
||||
logger.error('argv=%r, cwd=%r', sys.argv, os.getcwd())
|
||||
logger.error('pyver=%s (%s)', pyver, os.uname().machine)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
||||
@ -5,6 +5,8 @@ Setup logging for the application.
|
||||
|
||||
import importlib
|
||||
import logging
|
||||
import logging.handlers
|
||||
import sys
|
||||
import warnings
|
||||
|
||||
import cherrypy
|
||||
@ -127,3 +129,24 @@ def get_configuration():
|
||||
configuration['root']['handlers'].append('journal')
|
||||
|
||||
return configuration
|
||||
|
||||
|
||||
def pipe_to_syslog(level=logging.INFO, to_stderr=True):
|
||||
"""Make the root logger write to syslog and stderr. Useful in actions"""
|
||||
logger = logging.getLogger()
|
||||
logger.setLevel(level)
|
||||
|
||||
fmt = '/freedombox/%(name)s[%(process)d]: %(levelname)s: %(message)s'
|
||||
formatter = logging.Formatter(fmt=fmt)
|
||||
|
||||
# Using syslog in Python: https://stackoverflow.com/q/3968669
|
||||
syslog_handler = logging.handlers.SysLogHandler(address='/dev/log')
|
||||
syslog_handler.setFormatter(formatter)
|
||||
logger.addHandler(syslog_handler)
|
||||
|
||||
if to_stderr == 'tty' and sys.stdin.isatty():
|
||||
to_stderr = True
|
||||
if to_stderr is True:
|
||||
stderr_handler = logging.StreamHandler()
|
||||
stderr_handler.setFormatter(formatter)
|
||||
logger.addHandler(stderr_handler)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user