Python: logging in colour

If you use logging module, you should use helpful module for coloured command line.

https://pypi.org/project/coloredlogs/

Install:

pip install coloredlogs

Enter fullscreen mode Exit fullscreen mode

It’s really easy-to-use:

import coloredlogs, logging

# Create a logger object. logger = logging.getLogger(__name__)

# By default the install() function installs a handler on the root logger, # this means that log messages from your code and log messages from the # libraries that you use will all show up on the terminal. coloredlogs.install(level='DEBUG')

# If you don't want to see log messages from libraries, you can pass a # specific logger object to the install() function. In this case only log # messages originating from that logger will show up on the terminal. coloredlogs.install(level='DEBUG', logger=logger)

# Some examples. logger.debug("this is a debugging message")
logger.info("this is an informational message")
logger.warning("this is a warning message")
logger.error("this is an error message")
logger.critical("this is a critical message")

Enter fullscreen mode Exit fullscreen mode

That’s all 😉

原文链接:Python: logging in colour

© 版权声明
THE END
喜欢就支持一下吧
点赞15 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容