From e2c57cc8f81507a45f1a129c239238f687e0aaa2 Mon Sep 17 00:00:00 2001 From: Dusty Mabe Date: Mon, 29 Feb 2016 17:38:55 -0500 Subject: [PATCH] logging: add more context to filename in verbose mode Previously where the filename would have been 'main.py' in the log messages, it would now be 'cli/main.py' for verbose mode. --- atomicapp/applogging.py | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/atomicapp/applogging.py b/atomicapp/applogging.py index ad5030a6..5d63ae2c 100644 --- a/atomicapp/applogging.py +++ b/atomicapp/applogging.py @@ -24,7 +24,24 @@ LOGGER_DEFAULT) -class colorizeOutputFormatter(logging.Formatter): +class customOutputFormatter(logging.Formatter): + """ + A class that adds 'longerfilename' support to the logging formatter + This 'longerfilename' will be filename + parent dir. + """ + + def format(self, record): + + # Add the 'longerfilename' field to the record dict. This is + # then used by the Formatter in the logging library when + # formatting the message string. + record.longerfilename = '/'.join(record.pathname.split('/')[-2:]) + + # Call the parent class to do formatting. + return super(customOutputFormatter, self).format(record) + + +class colorizeOutputFormatter(customOutputFormatter): """ A class to colorize the log msgs based on log level """ @@ -99,6 +116,13 @@ def setup_logging(verbose=None, quiet=None, logtype=None): else: logging_level = logging.INFO + # Set the format string to use based on the logging level. + # For debug we include more of the filename than for !debug. + if logging_level == logging.DEBUG: + formatstr = '%(asctime)s - [%(levelname)s] - %(longerfilename)s - %(message)s' + else: + formatstr = '%(asctime)s - [%(levelname)s] - %(filename)s - %(message)s' + # Get the loggers and clear out the handlers (allows this function # to be ran more than once) logger = logging.getLogger(LOGGER_DEFAULT) @@ -130,7 +154,7 @@ def setup_logging(verbose=None, quiet=None, logtype=None): # configure logger for basic no color printing to stdout handler = logging.StreamHandler(stream=sys.stdout) - formatter = logging.Formatter('%(asctime)s - [%(levelname)s] - %(filename)s - %(message)s') + formatter = customOutputFormatter(formatstr) handler.setFormatter(formatter) logger.addHandler(handler) logger.setLevel(logging_level) @@ -142,7 +166,7 @@ def setup_logging(verbose=None, quiet=None, logtype=None): # configure logger for color printing to stdout handler = logging.StreamHandler(stream=sys.stdout) - formatter = colorizeOutputFormatter('%(asctime)s - [%(levelname)s] - %(filename)s - %(message)s') + formatter = colorizeOutputFormatter(formatstr) handler.setFormatter(formatter) logger.addHandler(handler) logger.setLevel(logging_level)