欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程语言 > python >内容正文

python

python中的logger之二

发布时间:2023/12/15 python 48 豆豆
生活随笔 收集整理的这篇文章主要介绍了 python中的logger之二 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

Rotating-logger

日志文件太大,一般不容易使用。现在的日志系统一般都提供了方便的日志回绕分片。一般有按照文件大小、记录时间长度来对日志文件分片。
在python logging中,提供了这2种分片方式。

  • 按照文件大小分片
  • 这种方式使用的Handler是RotatingFileHandler;

    class RotatingFileHandler(BaseRotatingHandler):"""Handler for logging to a set of files, which switches from one fileto the next when the current file reaches a certain size."""def __init__(self, filename, mode='a', maxBytes=0, backupCount=0, encoding=None, delay=0):

    下面看看使用方式:

    rlogger = logging.getLogger("loggingtest2") rfh = RotatingFileHandler("log/rtest.log", maxBytes=2000,backupCount=3)formatter = logging.Formatter('%(name)-12s %(asctime)s.%(msecs)03d %(levelname)-8s %(message)s', datefmt='%Y-%m-%d,%H:%M:%S') rfh.setFormatter(formatter)rlogger.addHandler(rfh)

    完整示例:

    import sys,time import logging from logging.handlers import RotatingFileHandler,TimedRotatingFileHandlerrlogger = logging.getLogger("loggingtest2") rfh = RotatingFileHandler("log/rtest.log", maxBytes=2000,backupCount=3)formatter = logging.Formatter('%(name)-12s %(asctime)s.%(msecs)03d %(levelname)-8s %(message)s', datefmt='%Y-%m-%d,%H:%M:%S') rfh.setFormatter(formatter)rlogger.addHandler(rfh)rlogger.setLevel(level = logging.INFO)i = 0 while i<2000:rlogger.info("this is a %s %i.......","info",i)rlogger.warning("this is a %s %i........", "warning",i)i=i+1

    执行时,会在log下生成rtest.log,以及分片的3个backup文件rtest.log.0, rtest.log.1, rtest.log.2

  • 按照时间长度分片
  • 这里使用的是TimedRotatingFileHandler。

    定义如下:

    class TimedRotatingFileHandler(BaseRotatingHandler):"""Handler for logging to a file, rotating the log file at certain timedintervals.If backupCount is > 0, when rollover is done, no more than backupCountfiles are kept - the oldest ones are deleted."""def __init__(self, filename, when='h', interval=1, backupCount=0, encoding=None, delay=False, utc=False):

    使用方式:

    formatter = logging.Formatter('%(name)-12s %(asctime)s.%(msecs)03d %(levelname)-8s %(message)s', datefmt='%Y-%m-%d,%H:%M:%S') trlogger = logging.getLogger() trfh = TimedRotatingFileHandler("log/tr_test.log",when="S",interval=5,backupCount=3)trfh.setFormatter(formatter) trlogger.addHandler(trfh)

    完整示例:

    import sys,time import logging from logging.handlers import RotatingFileHandler,TimedRotatingFileHandlerformatter = logging.Formatter('%(name)-12s %(asctime)s.%(msecs)03d %(levelname)-8s %(message)s', datefmt='%Y-%m-%d,%H:%M:%S') trlogger = logging.getLogger() trfh = TimedRotatingFileHandler("log/tr_test.log",when="S",interval=5,backupCount=3)trfh.setFormatter(formatter) trlogger.addHandler(trfh)trlogger.setLevel(level = logging.INFO)i=0 while i < 500:trlogger.info("this is a %s .......","info")trlogger.warning("this is a %s........", "warning")time.sleep(0.5)i=i+1

    总结

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

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