python logging现学现用 – TimedRotatingFileHandler使用方法
2012-07-04 23:06
896 查看
原文链接:
http://openexz.sinaapp.com/2011/10/12/python-logging%e7%8e%b0%e5%ad%a6%e7%8e%b0%e7%94%a8-timedrotatingfilehandler%e4%bd%bf%e7%94%a8%e6%96%b9%e6%b3%95/
TimedRotatingFileHandler这个模块是满足文件名按时间自动更换的需求,这样就可以保证日志单个文件不会太大。
用法很简单,示例代码:
本地代码:
TimedRotatingFileHandler的构造函数定义如下(2.5版本API为例):
TimedRotatingFileHandler(filename [,when [,interval [,backupCount]]])
filename 是输出日志文件名的前缀
when 是一个字符串的定义如下:
“S”: Seconds
“M”: Minutes
“H”: Hours
“D”: Days
“W”: Week day (0=Monday)
“midnight”: Roll over at midnight
interval 是指等待多少个单位when的时间后,Logger会自动重建文件,当然,这个文件的创建
取决于filename+suffix,若这个文件跟之前的文件有重名,则会自动覆盖掉以前的文件,所以
有些情况suffix要定义的不能因为when而重复。
backupCount 是保留日志个数。默认的0是不会自动删除掉日志。若设10,则在文件的创建过程中
库会判断是否有超过这个10,若超过,则会从最先创建的开始删除。
http://openexz.sinaapp.com/2011/10/12/python-logging%e7%8e%b0%e5%ad%a6%e7%8e%b0%e7%94%a8-timedrotatingfilehandler%e4%bd%bf%e7%94%a8%e6%96%b9%e6%b3%95/
TimedRotatingFileHandler这个模块是满足文件名按时间自动更换的需求,这样就可以保证日志单个文件不会太大。
用法很简单,示例代码:
import logging import logging.handlers # logging初始化工作 logging.basicConfig() # nor的初始化工作 nor = logging.getLogger("nor") nor.setLevel(logging.INFO) # 添加TimedRotatingFileHandler到nor # 定义一个1分钟换一次log文件的handler filehandler = logging.handlers.TimedRotatingFileHandler( "logging_test2", 'M', 1, 0) # 设置后缀名称,跟strftime的格式一样 filehandler.suffix = "%Y%m%d-%H%M.log" nor.addHandler(filehandler)
本地代码:
import logging from logging.handlers import TimedRotatingFileHandler root = logging.getLogger() if len(root.handlers) == 0: #避免重复 level = logging.INFO filename = os.path.join(HERE,'static/log/log.log').replace('\\','/') #filename = 'D:\\workspace_myeclipse\\djangopro_dwz\\log.log' format = '%(asctime)s %(levelname)s %(module)s.%(funcName)s Line:%(lineno)d%(message)s' hdlr = TimedRotatingFileHandler(filename,"M",1,10) fmt = logging.Formatter(format) hdlr.setFormatter(fmt) root.addHandler(hdlr) root.setLevel(level)
TimedRotatingFileHandler的构造函数定义如下(2.5版本API为例):
TimedRotatingFileHandler(filename [,when [,interval [,backupCount]]])
filename 是输出日志文件名的前缀
when 是一个字符串的定义如下:
“S”: Seconds
“M”: Minutes
“H”: Hours
“D”: Days
“W”: Week day (0=Monday)
“midnight”: Roll over at midnight
interval 是指等待多少个单位when的时间后,Logger会自动重建文件,当然,这个文件的创建
取决于filename+suffix,若这个文件跟之前的文件有重名,则会自动覆盖掉以前的文件,所以
有些情况suffix要定义的不能因为when而重复。
backupCount 是保留日志个数。默认的0是不会自动删除掉日志。若设10,则在文件的创建过程中
库会判断是否有超过这个10,若超过,则会从最先创建的开始删除。
相关文章推荐
- Python多进程不要使用TimedRotatingFileHandler
- python logging 模块之TimedRotatingFileHandler 实现每天一个日志文件
- python logging类库中RotatingFileHandler的使用
- [bigdata-078] python3 logging 的示例 TimedRotatingFileHandler和RotatingFileHandler 基于时间切分和基于文件大小切分
- logging TimedRotatingFileHandler不支持多进程
- Python logging RotatingFileHandler bug
- Python logging RotatingFileHandler bug
- python日志按时间切分-----TimedRotatingFileHandler
- python中fileinput模块使用方法
- 实际应用中遇到TimedRotatingFileHandler不滚动的问题
- python标准日志模块logging的使用方法
- (转)python标准日志模块logging的使用方法
- 用两种不同的方法导出ORACLE 查询数据为CSV 文件 (python 代码 与 使用 utl_file 包)
- Python使用logging结合decorator模式实现优化日志输出的方法
- python 的日志logging模块使用方法
- TimedRotatingFileHandler
- Python LOGGING使用方法
- python标准日志模块logging的使用方法
- python标准日志模块logging的使用方法
- Python的RotatingFileHandler的Bug