直接看代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 初始化日志模块
import logging
import logging.handlers
logger = logging.getLogger('manager')
# 输出到控制台, 级别为DEBUG
console = logging.StreamHandler()
console.setLevel(logging.DEBUG)
logger.addHandler(console)
# 输出到文件, 级别为INFO, 文件按大小切分
filelog = logging.handlers.RotatingFileHandler('log.txt', maxBytes=1024*1024, backupCount=5)
filelog.setFormatter(logging.Formatter('%(asctime)s - %(levelname)s - %(message)s'))
logger.setLevel(logging.INFO)
logger.addHandler(filelog)
# 按时间切分
logging.handlers.TimedRotatingFileHandler(filename="all.log", when='S', interval=1, backupCount=3)