1 code
#!coding:utf-8
import logging
import os
from config import LOG_NAME
from logging.handlers import RotatingFileHandler
logfile="{0}/logs/{1}".format(os.path.abspath('.'),LOG_NAME)
def_format='%(asctime)s %(name) 12s-%(levelname)-8s: %(message)s'
datefmt='%d-%b-%Y %H:%M:%S'
logging.basicConfig(
filename=logfile,
filemode="a",
format='%(asctime)s %(name) 12s-%(levelname)-8s: %(message)s',
#datefmt='%d-%b-%Y %H:%M:%S',
level=logging.INFO
)
formater=logging.Formatter(def_format)
ro=RotatingFileHandler(logfile,maxBytes=100*1024*1024,backupCount=10)#backup-size 100M
#ro=RotatingFileHandler(logfile,maxBytes=200,backupCount=10)#backup-size
ro.setFormatter(formater)
ro.setLevel(logging.INFO)
#logger1=logging.getLogger("hello")
#logger2=logging.getLogger("help")
#logger1.addHandler(ro)
#logger2.addHandler(ro)
#logger1=logging.getLogger("help")
#logger1.addHandler(ro)
#for i in range(100):
# logger1.info("x")
# logger2.info("y")
2, 实现功能:
在本文件中创建了一个"ro"的 RotatingFileHandler 对象,并且配置logfile,maxBytes 文件最大大小, backupCount备份数量
3, 总结:
logging.basicConfig() 是全局的配置,以后基于创建出来的logger 都默认带有这些配置
code 里的 logger1 logger2 共同使用一个logfile, 都配置"ro", 所以都遵循 Rotating的功能
谢谢, 欢迎点赞和交流