python logging

import logging

# create logger
logger_name = "example"
logger = logging.getLogger(logger_name)
logger.setLevel(logging.ERROR)

# 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 %(process)d %(message)s"
datefmt = "%a %d %b %Y %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.warning('warn message')
logger.error('error message')
logger.critical('critical message')


属性名              格式                           描述
日志消息内容 %(message)s The logged message, computed as msg % args. 当调用Formatter.format()时设置
asctime  %(asctime)s 创建LogRecord时的可读时间。默认情况下,它的格式为’2003-07-08 16:49:45,896’(逗号后面的数字是毫秒部分的时间)
函数名 %(funcName)s 日志调用所在的函数名
日志级别名称 %(levelname)s 消息的级别名称 ‘DEBUG’, ‘INFO’, ‘WARNING’, ‘ERROR’, ‘CRITICAL’
日记级别数值 %(levelno)s 消息的级别数字 ,对应DEBUG, INFO, WARNING, ERROR, CRITICAL
行号 %(lineno)d 日志调用所在的源码行号
模块 %(module)s 模块(filename的名字部分)
进程ID %(process)d 进程 ID
线程ID %(thread)d 线程 ID
进程名称 %(processName)s 进程名
线程名称 %(threadName)s 线程名称
logger名称 %(name)s logger名字

注意:funcName、threadName、processName都是小驼峰。 行号和进线程ID 都是以d 结尾,其余都是小s

版权声明:本文内容由互联网用户撰写,该文观点仅代表作者本人。本站爱分享仅提供分享服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,请立马联系本站,本站将立刻删除。
THE END
分享
二维码
< <上一篇
下一篇>>
文章目录
关闭