这里介绍python的logging日志模块
1. 单纯显示日志在屏幕
# -*- encoding:utf-8 -*-
# 只输出日志到屏幕
import logging
import sys
create logger
logger_name = "example"
logger = logging.getLogger(logger_name)
logger.setLevel(logging.INFO) # 设置日志打印等级(logging默认等级是warn)
#下面两行没有的话,设置的logger等级若低于warn,则不会生效
sh = logging.StreamHandler(stream=sys.stdout) # output to standard output
logger.addHandler(sh)
#print log info
logger.debug('debug message')
logger.info('info message')
logger.warn('warn message')
logger.error('error message')
logger.critical('critical message')
或者简单一些
# -*- encoding:utf-8 -*-
# 只输出日志到屏幕
import logging
logging.basicConfig(level = logging.INFO,format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
# print log info
logging.debug('debug message')
logging.info('info message')
logging.warn('warn message')
logging.error('error message')
logging.critical('critical message')
2. 日志写入指定文件
# -*- encoding:utf-8 -*-
# 输出到日志文件
import logging
# create logger
logger_name = "example"
logger = logging.getLogger(logger_name)
#logger.setLevel(logging.INFO)
# create file handler
log_path = "./log.log"
fh = logging.FileHandler(log_path)
fh.setLevel(logging.WARN)
# create formatter
fmt = "%(asctime)-15s %(levelname)s %(filename)s %(lineno)d %(message)s"
datefmt = "%Y-%m-%d %H:%M:%S"
formatter = logging.Formatter(fmt, datefmt)
# add handler and formatter to logger
fh.setFormatter(formatter)
logger.addHandler(fh)
# print log info
logger.debug('debug message')
logger.info('info message')
logger.warn('warn message')
logger.error('error message')
logger.critical('critical message')
或者简单些
# -*- encoding:utf-8 -*-
# 输出到日志文件
import logging
import sys
logging.basicConfig(filename="log.log", level = logging.INFO,format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
# print log info
logging.debug('debug message')
logging.info('info message')
logging.warn('warn message')
logging.error('error message')
logging.critical('critical message')
3. 同时输出到终端和日志文件
import logging
logger = logging.getLogger() # 不加名称设置root logger
logger.setLevel(logging.DEBUG)
formatter_src = logging.Formatter(
'%(asctime)s: %(message)s',
datefmt='%H:%M:%S')
formatter_file = logging.Formatter(
'%(asctime)s %(filename)s:%(lineno)d [%(levelname)s]: %(message)s',
datefmt='%H:%M:%S')
# 使用FileHandler输出到文件
fh = logging.FileHandler('log.log')
fh.setLevel(logging.DEBUG)
fh.setFormatter(formatter_file)
# 使用StreamHandler输出到屏幕
ch = logging.StreamHandler()
ch.setLevel(logging.INFO)
ch.setFormatter(formatter_src)
# 添加两个Handler
logger.addHandler(ch)
logger.addHandler(fh)
logger.debug('debug message')
logger.info('info message')
logger.warn('warn message')
logger.error('error message')
logger.critical('critical message')
4. 给终端输出增加点儿颜色
import logging
import coloredlogs
FIELD_STYLES = dict(
asctime=dict(color='green'),
hostname=dict(color='magenta'),
levelname=dict(color='green'),
filename=dict(color='magenta'),
name=dict(color='blue'),
threadName=dict(color='green')
)
LEVEL_STYLES = dict(
debug=dict(color='green'),
info=dict(color='cyan'),
warning=dict(color='yellow'),
error=dict(color='red'),
critical=dict(color='red')
)
logger = logging.getLogger('tos')
coloredlogs.install(
level="DEBUG",
fmt="[%(levelname)s] [%(asctime)s] [%(filename)s:%(lineno)d] %(message)s",
level_styles=LEVEL_STYLES,
field_styles=FIELD_STYLES)
logger.debug('This is Debug mode')
logger.info('This is info mode')
logger.warn('This is warn mode')
logger.error('This is error mode')
logger.critical('This is critical mode')
最终的输出都是五颜六色的,特别醒目
5. 终端输出增加颜色的同时,输出日志文件
import logging
import coloredlogs
FIELD_STYLES = dict(
asctime=dict(color='green'),
hostname=dict(color='magenta'),
levelname=dict(color='green'),
filename=dict(color='magenta'),
name=dict(color='blue'),
threadName=dict(color='green')
)
LEVEL_STYLES = dict(
debug=dict(color='green'),
info=dict(color='cyan'),
warning=dict(color='yellow'),
error=dict(color='red'),
critical=dict(color='red')
)
logger = logging.getLogger('tos')
coloredlogs.install(
level="DEBUG",
fmt="[%(asctime)s]:%(message)s",
datefmt='%H:%M:%S',
level_styles=LEVEL_STYLES,
field_styles=FIELD_STYLES)
# 使用FileHandler输出到文件
fh = logging.FileHandler('log.log')
fh.setLevel(logging.DEBUG)
formatter_file = logging.Formatter(
'%(asctime)s %(filename)s:%(lineno)d [%(levelname)s]: %(message)s',
datefmt='%H:%M:%S')
fh.setFormatter(formatter_file)
logger.addHandler(fh)
logger.debug('This is Debug mode')
logger.info('This is info mode')
logger.warn('This is warn mode')
logger.error('This is error mode')
logger.critical('This is critical mode')
参考资料:
python logging模块使用教程
Python logging同时输出到屏幕和文件
Python 彩色命令行输出