原生loggging类+ TimedRotatingFileHandler类 实现按day hour second 切分

原生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 在午夜

相关文章:

  • 2022-12-23
  • 2021-06-20
  • 2021-07-10
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-08-30
  • 2022-12-23
  • 2022-12-23
  • 2022-01-23
  • 2022-12-23
相关资源
相似解决方案