From 0b7d3ecfbfda6ab18e87a604d5d3bb5288933b6d Mon Sep 17 00:00:00 2001 From: Roland Mas Date: Thu, 7 Jan 2016 09:03:02 +0100 Subject: Added --log=stderr to log to STDERR --- NEWS | 5 ++++- cliapp/app.py | 16 ++++++++++++++++ cliapp/settings.py | 1 + 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/NEWS b/NEWS index bd905dd..bba3b30 100644 --- a/NEWS +++ b/NEWS @@ -16,7 +16,7 @@ Bug fix: for `--foo`) are now sorted after the non-negated ones in manual pages. -New feature: +New features: * Configuration files may now also be in YAML format, if names with an `.yaml` suffix. @@ -24,6 +24,9 @@ New feature: * If the `python-xdg` library is available, it is used to allow user to specify locations of XDG Base Directory locations. +* Log to STDERR support has been added. Use `--log=stderr`. Patch by + Roland Mas. + Version 1.20150829 ------------------ diff --git a/cliapp/app.py b/cliapp/app.py index cd1b576..494d4b2 100644 --- a/cliapp/app.py +++ b/cliapp/app.py @@ -405,6 +405,8 @@ class Application(object): if self.settings['log'] == 'syslog': handler = self.setup_logging_handler_for_syslog() + elif self.settings['log'] == 'stderr': + handler = self.setup_logging_handler_for_stderr() elif self.settings['log'] and self.settings['log'] != 'none': handler = self.setup_logging_handler_for_file() else: @@ -431,6 +433,20 @@ class Application(object): fmt = progname + ": %(levelname)s %(message)s" return logging.Formatter(fmt) + def setup_logging_handler_for_stderr(self): # pragma: no cover + '''Setup a logging.Handler for logging to stderr.''' + + handler = logging.StreamHandler() + formatter = self.setup_logging_formatter_for_stderr() + handler.setFormatter(formatter) + + return handler + + def setup_logging_formatter_for_stderr(self): # pragma: no cover + '''Setup a logging.Formatter for stderr.''' + fmt = "%(levelname)s %(message)s" + return logging.Formatter(fmt) + def setup_logging_handler_for_file(self): # pragma: no cover '''Setup a logging handler for logging to a named file.''' diff --git a/cliapp/settings.py b/cliapp/settings.py index 969600b..f05f11e 100644 --- a/cliapp/settings.py +++ b/cliapp/settings.py @@ -329,6 +329,7 @@ class Settings(object): self.string(['log'], 'write log entries to FILE (default is to not write log ' 'files at all); use "syslog" to log to system log, ' + '"stderr" to log to the standard error output, ' 'or "none" to disable logging', metavar='FILE', group=log_group_name) self.choice(['log-level'], -- cgit v1.2.1