Skip to content
This repository has been archived by the owner on Jan 19, 2018. It is now read-only.

Commit

Permalink
Merge pull request #583 from dustymabe/dusty-log-filename
Browse files Browse the repository at this point in the history
logging: add more context to filename in verbose mode
  • Loading branch information
cdrage committed Mar 3, 2016
2 parents 2ff06cf + e2c57cc commit 098372f
Showing 1 changed file with 27 additions and 3 deletions.
30 changes: 27 additions & 3 deletions atomicapp/applogging.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
"""
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 098372f

Please sign in to comment.