Skip to content
This repository has been archived by the owner on Oct 11, 2021. It is now read-only.

Commit

Permalink
Provide format strings with a color code by log level
Browse files Browse the repository at this point in the history
  • Loading branch information
markpasc committed Jan 31, 2013
1 parent 544cf8d commit d0b7c83
Showing 1 changed file with 49 additions and 2 deletions.
51 changes: 49 additions & 2 deletions termtool.py
Expand Up @@ -101,6 +101,9 @@ class Termtool(object):
progressbar = ProgressBar
table = _PrettierTable

log_format = '%(levelname)s: %(message)s'
"""The logging format string that `configure()` will configure logging with."""

def write_config_file(self, *args):
"""Write out a config file containing the given arguments.
Expand Down Expand Up @@ -224,18 +227,62 @@ def build_arg_parser(self):

return parser

class _NoColorLogFormatter(logging.Formatter):

def format(self, record):
record.levelcolor = ''
record.resetcolor = ''
return super(Termtool._NoColorLogFormatter, self).format(record)

class _ColorLogFormatter(logging.Formatter):

color_for_level = {
logging.DEBUG: '32', # green
logging.INFO: '37', # white
logging.WARNING: '33', # yellow
logging.ERROR: '31', # red
logging.CRITICAL: '35', # magenta
}

def format(self, record):
color = self.color_for_level.get(record.levelno)
if color is not None:
record.levelcolor = u'\033[1;%sm' % color
record.resetcolor = u'\033[0m'
return super(Termtool._ColorLogFormatter, self).format(record)

def configure(self, args):
"""Configure the tool according to the command line arguments.
Override this method to configure your tool with the values of any
other options it supports.
This implementation configures the `logging` module to the log level
requested by the user through the ``-v`` and ``-q`` options.
requested by the user through the ``-v`` and ``-q`` options. It also
configures logging to go to stderr, formatted according to the
instance's `log_format` attribute.
Additionally, logging is configured to provide these additional format
strings:
%(levelcolor)s If stderr is a terminal, an ANSI color code
appropriate for the level of the logged record.
%(resetcolor)s If stderr is a terminal, an ANSI color reset code.
"""
log_level = args.loglevel
logging.basicConfig(level=log_level, format='%(levelname)s: %(message)s')
root_logger = logging.getLogger()
root_logger.setLevel(log_level)

log_format = self.log_format
handler = logging.StreamHandler() # using sys.stderr
if hasattr(sys.stderr, 'isatty') and sys.stderr.isatty():
formatter_class = self._ColorLogFormatter
else:
formatter_class = self._NoColorLogFormatter
handler.setFormatter(formatter_class(log_format))
root_logger.addHandler(handler)

logging.info('Set log level to %s', logging.getLevelName(log_level))

def main(self, argv):
Expand Down

0 comments on commit d0b7c83

Please sign in to comment.