logging模块
logging模块:
basicConfig:
1 # # -------------------重点:basicConfig★★★-------------
2 import logging
3 logging.basicConfig(
4 level=logging.DEBUG,
5 filename='logging.log',#追加模式
6 filemode="w",#W模式
7 # datefmt=
8 format="%(asctime)s [%(lineno)d] %(filename)s %(message)s"
9 )
10
11 logging.debug('debug message')
12 logging.info('info message')
13 logging.warning('warning message')
14 logging.error('error message')
15 logging.critical('critical message')
16 运行结果:
17 2018-06-18 15:49:18,926 [11] logging模块.py debug message
18 2018-06-18 15:49:18,926 [12] logging模块.py info message
19 2018-06-18 15:49:18,926 [13] logging模块.py warning message
20 2018-06-18 15:49:18,926 [14] logging模块.py error message
21 2018-06-18 15:49:18,926 [15] logging模块.py critical message
logger:
1 ------------重点:logger★★★-----------
2 def logger():
3
4 import logging
5 logger = logging.getLogger()
6 fh = logging.FileHandler("text_log")
7 sh = logging.StreamHandler()
8
9 fm = logging.Formatter("%(asctime)s %(message)s")
10
11 fh.setFormatter(fm)
12 sh.setFormatter(fm)#如果没有 按照默认格式输出信息
13
14 logger.addHandler(fh)
15 logger.addHandler(sh)
16 # logger.setLevel("DEBUG")#与下面等价
17 logger.setLevel(logging.DEBUG)
18
19 return logger
20 # #---------------
21 logger = logger()
22
23 logger.debug("debug message")
24 logger.info("info message")
25 logger.warning("warning message")
26 logger.error("error message")
27 logger.critical("critical message")
28 运行结果:
29 D:\pycharm\untitled\venv\Scripts\python.exe D:/pycharm/untitled/logging模块.py
30 2018-06-18 15:51:29,753 debug message
31 2018-06-18 15:51:29,753 info message
32 2018-06-18 15:51:29,753 warning message
33 2018-06-18 15:51:29,753 error message
34 2018-06-18 15:51:29,753 critical message
35
36 Process finished with exit code 0
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 import logging 2 3 logger = logging.getLogger()#创建根用户 4 5 logger1 = logging.getLogger('my_logger')#创建根用户下的子用户my_logger 6 logger1.setLevel(logging.INFO) 7 8 logger2 = logging.getLogger('my_logger')#创建根用户下的子用户my_logger 9 logger2.setLevel(logging.DEBUG) 10 11 fh = logging.FileHandler('text_log')#写入文件text_log中 12 sh = logging.StreamHandler() 13 # logger.addHandler(fh)#根用户添加fh的内容<添加此条内容 子最后输出打印时成倍打印输出结果 在txt文件中> 14 # logger.addHandler(sh)#根用户添加sh的内容<添加此条内容 子最后输出打印时成倍打印输出结果在屏幕上> 15 16 logger1.addHandler(fh)#在文件中显示写入内容 17 logger1.addHandler(sh)#在屏幕下方显示添加内容(如果没有此行代码 则屏幕下方显示默认个事输出的内容) 18 19 logger2.addHandler(fh)#在文件中显示写入内容 20 logger2.addHandler(sh)#在屏幕下方显示添加内容 21 22 # #打印父亲对象的内容 23 logger.debug('logger message') 24 logger.info('logger message') 25 logger.warning('logger message') 26 logger.error('logger message') 27 logger.critical('logger message') 28 29 # #打印十个logger1的内容 有个父辈 多打印一次(5个)有n个父辈 多打印n次 30 logger1.debug('logger1 message dashu1') 31 logger1.info('logger1 message dashuinfo') 32 logger1.warning('logger1 message dashuw') 33 logger1.error('logger1 message dashuerr') 34 logger1.critical('logger1 message dashucri') 35 36 # #打印十个logger2的内容 有一个父辈 多打印一次(5个)有n个父辈 多打印n次 37 logger2.debug('logger2 message dashu2') 38 logger2.info('logger2 message dashuinfo2') 39 logger2.warning('logger2 message dashuw2') 40 logger2.error('logger2 message dashuerr2') 41 logger2.critical('logger2 message dashucri2') 42 运行结果: 43 D:\pycharm\untitled\venv\Scripts\python.exe D:/pycharm/untitled/logging模块.py 44 logger message 45 logger message 46 logger message 47 logger1 message dashu1 48 logger1 message dashuinfo 49 logger1 message dashuw 50 logger1 message dashuerr 51 logger1 message dashucri 52 logger2 message dashu2 53 logger2 message dashuinfo2 54 logger2 message dashuw2 55 logger2 message dashuerr2 56 logger2 message dashucri2 57 58 Process finished with exit code 0