Posts python日志设置和使用,同时输出到文件和控制台
Post
Cancel

python日志设置和使用,同时输出到文件和控制台

直接看代码

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)

邀请您关注我的公众号,我将会不时地为您推送独家原创的技术内容分享。
This post is licensed under CC BY 4.0 by the author.

Trending Tags