Python同时向控制台和文件输出日志logging的方法 Python logging模块详解

时间:2023-03-08 17:14:07
Python同时向控制台和文件输出日志logging的方法     Python logging模块详解

Python同时向控制台和文件输出日志logging的方法http://www.jb51.net/article/66756.htm

 1 #-*- coding:utf-8 -*-
2 import logging
3 # 配置日志信息
4 logging.basicConfig(level=logging.DEBUG,
5 format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
6 datefmt='%m-%d %H:%M',
7 filename='myapp.log',
8 filemode='w')
9 # 定义一个Handler打印INFO及以上级别的日志到sys.stderr
10 console = logging.StreamHandler()
11 console.setLevel(logging.INFO)
12 # 设置日志打印格式
13 formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s')
14 console.setFormatter(formatter)
15 # 将定义好的console日志handler添加到root logger
16 logging.getLogger('').addHandler(console)
17 logging.info('Jackdaws love my big sphinx of quartz.')
18 logger1 = logging.getLogger('myapp.area1')
19 logger2 = logging.getLogger('myapp.area2')
20 logger1.debug('Quick zephyrs blow, vexing daft Jim.')
21 logger1.info('How quickly daft jumping zebras vex.')
22 logger2.warning('Jail zesty vixen who grabbed pay from quack.')
23 logger2.error('The five boxing wizards jump quickly.')

http://blog.csdn.net/zyz511919766/article/details/25136485