Skip to content
This repository was archived by the owner on Jan 19, 2018. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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