python-coloredlogs
python-coloredlogs copied to clipboard
Align log message types
The default log format misaligns log messages of different types:
2015-10-23 03:32:22 peter-macbook coloredlogs.demo[30462] DEBUG message with level 'debug'
2015-10-23 03:32:23 peter-macbook coloredlogs.demo[30462] VERBOSE message with level 'verbose'
2015-10-23 03:32:24 peter-macbook coloredlogs.demo[30462] INFO message with level 'info'
It would be great if by default coloredlogs worked like:
2015-10-23 03:32:22 peter-macbook coloredlogs.demo[30462] DEBUG message with level 'debug'
2015-10-23 03:32:23 peter-macbook coloredlogs.demo[30462] VERBOSE message with level 'verbose'
2015-10-23 03:32:24 peter-macbook coloredlogs.demo[30462] INFO message with level 'info'
You can get this behavior with a custom log format since those use the old printf-style formatting syntax.
import coloredlogs
import logging
log = logging.getLogger(__name__)
# Figure out the how long the field needs to be
w = max(len(s) for s in coloredlogs.find_defined_levels().keys())
# Specify minimum width in the log format
coloredlogs.install(
level="DEBUG", logger=log, fmt=f"%(filename)s %(levelname)-{w}s %(message)s"
)
# Print some logs
log.debug("Don't mind me")
log.info("Just testing logs")
log.warning("Not sure if this will work")
log.error("Actually I should probably check first")
log.critical("Let me do that and come back")

I agree that it would be a nice default though.