diff options
author | Doug Hellmann <doug.hellmann@dreamhost.com> | 2012-08-15 10:33:32 -0400 |
---|---|---|
committer | Doug Hellmann <doug.hellmann@dreamhost.com> | 2012-08-15 10:33:32 -0400 |
commit | 0aef93a0fdeb965baa2d20c37d80a2741e7cfa82 (patch) | |
tree | 6b19cce2d6d438ce7fa8b97fec52e00034b0e232 /cliff/app.py | |
parent | 3f1430cd08e0f68d1df16ffd5ccd9c26f865d3da (diff) | |
download | cliff-0aef93a0fdeb965baa2d20c37d80a2741e7cfa82.tar.gz |
Fix logging default behavior
This change turns off logging by default and adds
a global application command line switch to re-enable
it by specifying the file where the log should be
written.
Addresses issue #16
Change-Id: I288412f4d6d8ba7c8b00aecf471836067bc4d8e2
Signed-off-by: Doug Hellmann <doug.hellmann@dreamhost.com>
Diffstat (limited to 'cliff/app.py')
-rw-r--r-- | cliff/app.py | 32 |
1 files changed, 21 insertions, 11 deletions
diff --git a/cliff/app.py b/cliff/app.py index bc0b65e..06c0c5a 100644 --- a/cliff/app.py +++ b/cliff/app.py @@ -10,6 +10,10 @@ import sys from .help import HelpAction, HelpCommand from .interactive import InteractiveApp +# Make sure the cliff library has a logging handler +# in case the app developer doesn't set up logging. +logging.getLogger('cliff').addHandler(logging.NullHandler()) + LOG = logging.getLogger(__name__) @@ -82,6 +86,12 @@ class App(object): help='Increase verbosity of output. Can be repeated.', ) parser.add_argument( + '--log-file', + action='store', + default=None, + help='Specify a file to log output. Disabled by default.', + ) + parser.add_argument( '-q', '--quiet', action='store_const', dest='verbose_level', @@ -107,19 +117,19 @@ class App(object): """Create logging handlers for any log output. """ root_logger = logging.getLogger('') - - # Set up logging to a file root_logger.setLevel(logging.DEBUG) - file_handler = logging.handlers.RotatingFileHandler( - self.NAME + '.log', - maxBytes=10240, - backupCount=1, - ) - formatter = logging.Formatter(self.LOG_FILE_MESSAGE_FORMAT) - file_handler.setFormatter(formatter) - root_logger.addHandler(file_handler) - # Send higher-level messages to the console via stderr + # Set up logging to a file + if self.options.log_file: + print 'configuring logging to', self.options.log_file + file_handler = logging.FileHandler( + filename=self.options.log_file, + ) + formatter = logging.Formatter(self.LOG_FILE_MESSAGE_FORMAT) + file_handler.setFormatter(formatter) + root_logger.addHandler(file_handler) + + # Always send higher-level messages to the console via stderr console = logging.StreamHandler(self.stderr) console_level = {0: logging.WARNING, 1: logging.INFO, |