From 0f05f7ad930f944e6df20b2df439de7e287770a7 Mon Sep 17 00:00:00 2001 From: Dan Helfman Date: Sun, 26 May 2019 16:34:46 -0700 Subject: [PATCH] Log to syslog in addition to existing standard out logging (#53). --- NEWS | 1 + borgmatic/commands/borgmatic.py | 24 ++++++++++++++++++++---- borgmatic/logger.py | 6 +++++- 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/NEWS b/NEWS index 492c863..9df2b6f 100644 --- a/NEWS +++ b/NEWS @@ -1,4 +1,5 @@ 1.3.6.dev0 + * #53: Log to syslog in addition to existing standard out logging. * #178: Look for .yml configuration file extension in addition to .yaml. * Remove Python cache files before each Tox run. * Add #borgmatic Freenode IRC channel to documentation. diff --git a/borgmatic/commands/borgmatic.py b/borgmatic/commands/borgmatic.py index 318899e..5657614 100644 --- a/borgmatic/commands/borgmatic.py +++ b/borgmatic/commands/borgmatic.py @@ -410,17 +410,22 @@ def collect_configuration_run_summary_logs(config_filenames, args): yield logging.makeLogRecord( dict( levelno=logging.CRITICAL, + levelname='CRITICAL', msg='{}: Error parsing configuration file'.format(config_filename), ) ) - yield logging.makeLogRecord(dict(levelno=logging.CRITICAL, msg=error)) + yield logging.makeLogRecord( + dict(levelno=logging.CRITICAL, levelname='CRITICAL', msg=error) + ) # Run cross-file validation checks. if args.extract or (args.list and args.archive): try: validate.guard_configuration_contains_repository(args.repository, configs) except ValueError as error: - yield logging.makeLogRecord(dict(levelno=logging.CRITICAL, msg=error)) + yield logging.makeLogRecord( + dict(levelno=logging.CRITICAL, levelname='CRITICAL', msg=error) + ) return # Execute the actions corresponding to each configuration file. @@ -431,6 +436,7 @@ def collect_configuration_run_summary_logs(config_filenames, args): yield logging.makeLogRecord( dict( levelno=logging.INFO, + levelname='INFO', msg='{}: Successfully ran configuration file'.format(config_filename), ) ) @@ -438,10 +444,13 @@ def collect_configuration_run_summary_logs(config_filenames, args): yield logging.makeLogRecord( dict( levelno=logging.CRITICAL, + levelname='CRITICAL', msg='{}: Error running configuration file'.format(config_filename), ) ) - yield logging.makeLogRecord(dict(levelno=logging.CRITICAL, msg=error)) + yield logging.makeLogRecord( + dict(levelno=logging.CRITICAL, levelname='CRITICAL', msg=error) + ) if json_results: sys.stdout.write(json.dumps(json_results)) @@ -450,6 +459,7 @@ def collect_configuration_run_summary_logs(config_filenames, args): yield logging.makeLogRecord( dict( levelno=logging.CRITICAL, + levelname='CRITICAL', msg='{}: No configuration files found'.format(' '.join(args.config_paths)), ) ) @@ -475,7 +485,13 @@ def main(): # pragma: no cover colorama.init(autoreset=True, strip=not should_do_markup(args.no_color)) - logging.basicConfig(level=verbosity_to_log_level(args.verbosity), format='%(message)s') + syslog_handler = logging.handlers.SysLogHandler(address='/dev/log') + syslog_handler.setFormatter(logging.Formatter('borgmatic: %(levelname)s %(message)s')) + logging.basicConfig( + level=verbosity_to_log_level(args.verbosity), + format='%(message)s', + handlers=(logging.StreamHandler(), syslog_handler), + ) if args.version: print(pkg_resources.require('borgmatic')[0].version) diff --git a/borgmatic/logger.py b/borgmatic/logger.py index 37e9472..c704d51 100644 --- a/borgmatic/logger.py +++ b/borgmatic/logger.py @@ -74,7 +74,11 @@ class Borgmatic_logger(logging.Logger): def handle(self, record): color = LOG_LEVEL_TO_COLOR.get(record.levelno) colored_record = logging.makeLogRecord( - dict(levelno=record.levelno, msg=color_text(color, record.msg)) + dict( + levelno=record.levelno, + levelname=record.levelname, + msg=color_text(color, record.msg), + ) ) return super(Borgmatic_logger, self).handle(colored_record)