diff options
author | Andy Doan <andy.doan@linaro.org> | 2015-06-17 13:58:57 -0500 |
---|---|---|
committer | Andy Doan <andy.doan@linaro.org> | 2015-07-07 11:09:02 -0500 |
commit | f96418fea0858a47993dc1f4916aac43be988588 (patch) | |
tree | 5be8aff90dbb2629e69e86939a217b92ca1b8da8 /bin.py | |
parent | f2463d5fcc14e600654a99b4db72f2709d592deb (diff) |
create a more sane way to manage logging options from CLI
We need a way to enable debug logging and the current logging setup was
inadequate. This provides a common way to add a "--log" option which
includes an argparse.Action that can set the logging level if the users
specifies.
This also changes the default logging level to WARN so that cron won't
send emails unless there is a problem.
gitrepo.py was updating to make its output adhere to logging settings
and not dump debug info the stdout when not needed.
Change-Id: I9c43217c5cf93c6f9cd12555733250f5e6d25c6b
Diffstat (limited to 'bin.py')
-rw-r--r-- | bin.py | 25 |
1 files changed, 17 insertions, 8 deletions
@@ -1,8 +1,10 @@ -import django +import argparse import logging import os import sys +import django + def django_setup(): try: @@ -22,7 +24,7 @@ def django_setup(): django.setup() -def _init_logging(): +def _init_logging(level): handler = logging.StreamHandler(stream=sys.stderr) formatter = logging.Formatter( '%(asctime)s %(levelname)-5s %(name)s: %(message)s', @@ -30,11 +32,18 @@ def _init_logging(): handler.setFormatter(formatter) l = logging.getLogger('') l.addHandler(handler) - l.setLevel(logging.INFO) -_init_logging.called = False + l.setLevel(getattr(logging, level)) + + +class LoggingAction(argparse.Action): + def __call__(self, parser, namespace, value, option_string=None): + log = logging.getLogger() + log.setLevel(getattr(logging, value)) + log.debug('logging level set to: %s', value) -def get_logger(name): - if not _init_logging.called: - _init_logging.called = _init_logging() - return logging.getLogger(name) +def add_logging_arguments(parser, level='WARN'): + _init_logging(level) + parser.add_argument('--log', default=level, action=LoggingAction, + choices=('WARN', 'INFO', 'DEBUG'), + help='Logging level to use. Default=%(default)s') |