目的logging模块是Python的内置模块,主要用于输出操作日志,可以灵活配置输出日志的信息。基本用法logging.basicConfig(level=logging.DEBUG,format='levelname:%(levelname)sfilename:%(filename)s''outputNumber:[%(lineno)d]thread:%(threadName)soutputmsg:%(message)s''-%(asctime)s',datefmt='[%d/%b/%Y%H:%M:%S]',filename='./loggmsg.log',filemode="a")参数日志分为5个级别,从低到高:DEBUG、INFO、WARNING、ERROR、CRITICAL。%(levelno)s:打印日志级别的值%(levelname)s:打印日志级别的名称%(pathname)s:打印当前执行程序的路径,其实就是sys.argv[0]%(filename)s:打印当前执行程序名%(funcName)s:打印日志当前函数%(lineno)d:打印日志当前行号%(asctime)s:打印时间log%(thread)d:printthethreadID%(threadName)s:printthreadname%(process)d:printprocessID%(message)s:printloginformationcalllogging.debug('Thisisdebugmessage')logging.info('Thisisinfomessage')logging.warning('Thisiswarningmessage')示例importlogginglogging.basicConfig(level=logging.DEBUG,format='levelname:%(levelname)sfilename:%(filename)s''outputNumber:[%(lineno)d]thread:%(threadName)soutputmsg:%(message)s''-%(asctime)s',datefmt='[%d/%b/%Y%H:%M:%S]',filename='./loggmsg.log',filemode="a")logging.debug("Hello")日志文件loggmsg.loglevelname:DEBUG文件名:test.pyoutputNumber:[7]thread:MainThreadoutputmsg:你好-[19/Nov/201922:28:13]想了解更多编程开发,和我一起成长进步,请关注我的公众号「松果仓库」和书呆子&程序员的各种资源分享,谢谢!!!
