Skip to content

Instantly share code, notes, and snippets.

@BIGBALLON
Last active October 19, 2023 10:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BIGBALLON/f97784f8a27aa1b525d20f617e17cb3c to your computer and use it in GitHub Desktop.
Save BIGBALLON/f97784f8a27aa1b525d20f617e17cb3c to your computer and use it in GitHub Desktop.
colorlog for color logger.
import colorlog
import logging
# 创建日志记录器
logger = colorlog.getLogger(__name__)
# 设置日志级别和格式
logger.setLevel(logging.DEBUG)
formatter = colorlog.ColoredFormatter(
"%(log_color)s%(levelname)-8s%(reset)s %(log_color)s%(asctime)s | %(blue)s%(message)s",
datefmt=None,
reset=True,
log_colors={
'DEBUG': 'cyan',
'INFO': 'green',
'WARNING': 'yellow',
'ERROR': 'red',
'CRITICAL': 'red,bg_white'
}
)
# 添加日志处理器并设置格式化器
handler = colorlog.StreamHandler()
handler.setFormatter(formatter)
logger.addHandler(handler)
# 打印不同级别的日志
logger.debug('Debug message')
logger.info('Info message')
logger.warning('Warning message')
logger.error('Error message')
# 输出
# cyanDEBUG | Direct print debug
# greenINFO | Info message
# yellowWARNING | Warning message
# redERROR | Error message
@BIGBALLON
Copy link
Author

BIGBALLON commented Aug 17, 2023

  • python color_log.py

image

@BIGBALLON
Copy link
Author

BIGBALLON commented Oct 19, 2023

TT

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment