欢迎访问 生活随笔!

生活随笔

当前位置: 首页 >

python的logging模块

发布时间:2025/4/16 64 豆豆
生活随笔 收集整理的这篇文章主要介绍了 python的logging模块 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

1.简单的将日志打印到屏幕

import logginglogging.debug('This is debug message') logging.info('This is info message') logging.warning('This is warning message')屏幕上打印: WARNING:root:Thisis warning message

 默认情况下,logging将日志打印到屏幕,日志级别为WARNING;

 日志级别大小关系为:CRITICAL > ERROR > WARNING > INFO > DEBUG > NOTSET,当然也可以自己定义日志级别。

 

2.通过logging.basicConfig函数对日志的输出格式及方式做相关配置

import logginglogging.basicConfig(level=logging.DEBUG,format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',datefmt='%a, %d %b %Y %H:%M:%S',filename='myapp.log',filemode='w')logging.debug('This is debug message') logging.info('This is info message') logging.warning('This is warning message')./myapp.log文件中内容为: Sun, 24 May 2009 21:48:54 demo2.py[line:11] DEBUG This is debug message Sun, 24 May 2009 21:48:54 demo2.py[line:12] INFO This is info message Sun, 24 May 2009 21:48:54 demo2.py[line:13] WARNING This is warning message

logging.basicConfig函数各参数:
filename: 指定日志文件名
filemode: 和file函数意义相同,指定日志文件的打开模式,'w'或'a'
format: 指定输出的格式和内容,format可以输出很多有用信息,如上例所示:
%(levelno)s: 打印日志级别的数值
%(levelname)s: 打印日志级别名称
%(pathname)s: 打印当前执行程序的路径,其实就是sys.argv[0]
%(filename)s: 打印当前执行程序名
%(funcName)s: 打印日志的当前函数
%(lineno)d: 打印日志的当前行号
%(asctime)s: 打印日志的时间
%(thread)d: 打印线程ID
%(threadName)s: 打印线程名称
%(process)d: 打印进程ID
%(message)s: 打印日志信息
datefmt: 指定时间格式,同time.strftime()
level: 设置日志级别,默认为logging.WARNING
stream: 指定将日志的输出流,可以指定输出到sys.stderr,sys.stdout或者文件,默认输出到sys.stderr,当stream和filename同时指定时,stream被忽略

 

import os import logging import logging.handlersdef init_log(log_path, level=logging.INFO, when="D", backup=7,format="%(levelname)s: %(asctime)s: %(filename)s:%(lineno)d * %(thread)d %(message)s",datefmt="%m-%d %H:%M:%S"):"""init_log - initialize log moduleArgs:log_path - Log file path prefix.Log data will go to two files: log_path.log and log_path.log.wfAny non-exist parent directories will be created automaticallylevel - msg above the level will be displayedDEBUG < INFO < WARNING < ERROR < CRITICALthe default value is logging.INFOwhen - how to split the log file by time interval'S' : Seconds'M' : Minutes'H' : Hours'D' : Days'W' : Week daydefault value: 'D'format - format of the logdefault format:%(levelname)s: %(asctime)s: %(filename)s:%(lineno)d * %(thread)d %(message)sINFO: 12-09 18:02:42: log.py:40 * 139814749787872 HELLO WORLDbackup - how many backup file to keepdefault value: 7Raises:OSError: fail to create log directoriesIOError: fail to open log file"""formatter = logging.Formatter(format, datefmt)logger = logging.getLogger()logger.setLevel(level)dir = os.path.dirname(log_path)if not os.path.isdir(dir):os.makedirs(dir)handler = logging.handlers.TimedRotatingFileHandler(log_path + ".log",when=when,backupCount=backup)handler.setLevel(level)handler.setFormatter(formatter)logger.addHandler(handler)handler = logging.handlers.TimedRotatingFileHandler(log_path + ".log.wf",when=when,backupCount=backup)handler.setLevel(logging.WARNING)handler.setFormatter(formatter)logger.addHandler(handler)

 

转载于:https://www.cnblogs.com/cfox/p/3868181.html

总结

以上是生活随笔为你收集整理的python的logging模块的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。