Python Logging: Colorize Your Arguments!

David Ohana
Analytics Vidhya
Published in
3 min readMay 24, 2020
Logging in alternating colors for message arguments

The latest full code and samples for this article are available under the Apache-2.0 license at my GitHub Repo.

Yes, we love logging in colors.
Yes, there are many Python libraries and sample code that show you how to colorize your stdout log messages by logging level.

But I am going to show you something better — log messages with alternating colors for each argument in the format string, in addition to colorization by log level. And a bonus — argument formatting is made using the new “brace-style” formatting introduced in Python 3.2, for example:

Implementation is simple, and no 3rd party dependencies are introduced. To apply colors, all you need to do is to set the formatter of the StreamHandler to an instance of ColorizedArgsFormatter:

Bootstrapping

How does it work?

  • Each log record is inspected to determine if brace-style formatting is used. I decided to use brace style formatting because it makes it easier to identify the start and end of a formatting parameter: {param}
  • ANSI escape code for the current alternating color is added before each formatting parameter, and reset color escape code is added after it
  • LogRecord is updated so that message field is formatted using str.format() and args field is set to empty.
  • ANSI escape code for the specific level is added before levelname and levelno formatting placeholders, and reset color escape code is added after the placeholder.

Compatibility Challenges

  • We still need to support legacy-style string formatting, e.g:
    logger.info(“My name is %s and my age is %d”, “Dave”, 12)
    as many 3rd party dependencies of our code might log in the old format.
    My solution for this is using a few simple heuristics to identify whether brace-style formatting should be used: no ‘%’ character in string + number of curly braces pair matches the number of log record arguments. Otherwise, we fall-back to legacy formatting, but no argument colorization is available.
  • We have to support brace-style formatting also when logging to other mediums, like files. Otherwise, the logger will fail to understand format string with “TypeError: not all arguments converted during string formatting”. Solution for that is using another simple formatter named BraceFormatStyleFormatter for file logging handlers and other mediums that do not support colors. This formatter is very similar to ColorizedArgsFormatter, however, it only performs rewriting of log record with the formatted message, and not adding ANSI colors escape codes.

Other Tips

  • By default, there are two alternating colors. You can change the colors or add more alternating colors by changing the arg_colors list.
  • It is possible to alter the mapping of log levels to colors by changing the level_to_color dictionary.
  • Currently, curly brace message formatting with kwargs mapping is not supported in logging, e.g: My name is {name}”.format(name=”David”)
Customization — more alternating colors and different color dor DEBUG messages

Full code

Bootstrapping and Logging — Full Sample App:

The latest full code and samples are available under the Apache-2.0 license at my GitHub Repo.

Have a colorful day,
David

--

--