TimedRotatingFileHandler
2014-11-13 16:38
567 查看
python日志按时间切分-----TimedRotatingFileHandler - [coding]
2011-07-18
版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明http://www.blogbus.com/shenguanpu-logs/148520184.html
原生loggging类+ TimedRotatingFileHandler类 实现按day hour second 切分
import logging
from logging.handlers import TimedRotatingFileHandler
log = logging.getLogger(loggerName)
formatter = logging.Formatter('%(name)-12s %(asctime)s level-%(levelname)-8s thread-%(thread)-8d %(message)s') # 每行日志的前缀设置
fileTimeHandler = TimedRotatingFileHandler(BASIC_LOG_PATH + filename, "S", 1, 10)
fileTimeHandler.suffix = "%Y%m%d.log" #设置 切分后日志文件名的时间格式 默认 filename+"." + suffix 如果需要更改需要改logging 源码
fileTimeHandler.setFormatter(formatter)
logging.basicConfig(level = logging.INFO)
fileTimeHandler.setFormatter(formatter)
log.addHandler(fileTimeHandler)
try:
log.error(msg)
except Exception, e:
print "writeLog error"
finally:
log.removeHandler(fileTimeHandler)
值 interval的类型
S 秒
M 分钟
H 小时
D 天
W 周
midnight 在午夜
================
python logging现学现用 – TimedRotatingFileHandler使用方法
TimedRotatingFileHandler这个模块是满足文件名按时间自动更换的需求,这样就可以保证日志单个文件不会太大。用法很简单,示例代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | 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) |
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
- [bigdata-078] python3 logging 的示例 TimedRotatingFileHandler和RotatingFileHandler 基于时间切分和基于文件大小切分
- logging TimedRotatingFileHandler不支持多进程
- python logging 模块之TimedRotatingFileHandler 实现每天一个日志文件
- 实际应用中遇到TimedRotatingFileHandler不滚动的问题
- Python多进程不要使用TimedRotatingFileHandler
- python logging现学现用 – TimedRotatingFileHandler使用方法
- python logging类库中RotatingFileHandler的使用
- Python的RotatingFileHandler的Bug
- Python logging RotatingFileHandler bug
- Django开发模式会加载两次settings文件导致RotatingFileHandlerError
- Python logging RotatingFileHandler bug
- Flex与.NET互操作(五):使用FileReference+HttpHandler实现文件上传/下载
- NSFileManger+NFIleHandler使用
- WHEN STATICFILEHANDLER IS NOT STATICFILEHANDLER
- android studio failed to open zip file .Android Studio Error:Connection timed out: connect.
- Error creating AIR file: Could not generate timestamp: Connection timed out
- Downloading files from a server to client, using ASP.Net, when file size is too big for MemoryStream using Generic Handlers (ashx)
- [Python--]tornado.web.StaticFileHandler 实现文件下载
- Flex与.NET互操作(五):使用FileReference+HttpHandler实现文件上传/下载