且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Python-->logging....实例应用

更新时间:2022-09-03 16:18:26

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import logging
logging.debug('debug message')
logging.info('info message')
logging.warning('warning message')
logging.error('error message')
logging.critical('critical message')
 
输出:
E:\Python>python lianxi.py
WARNING:root:warning message
ERROR:root:error message
CRITICAL:root:critical message
 
#默认情况下Python的logging模块将日志输出,且只输出大于等于WARNING级别的日志。(日志级别等级critical>error>warning>info>debug)( 危急>错误>警告>信息>调试>没有设置),默认输出的日志格式为 日志级别:logger名称:用户输出消息。
#设置日志的输出格式
 
 
import logging
logging.basicConfig(level=logging.DEBUG,
    format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
    datefmt='%a,%d %b %Y %H:%M:%S',
                #r'xxx'   r后接一个字符串表示其中的转义字符不解析
    filename=r'E:\Python\test.log',
    filemode='w')
logging.debug('debug message')
logging.info('info message')
logging.warning('warning message')
logging.error('error message')
logging.critical('critical message')
 
 
文件内容为:
2016-05-15 10:05:24,843 lianxi.py[line:7] DEBUG debug message
2016-05-15 10:05:24,843 lianxi.py[line:8] INFO info message
2016-05-15 10:05:24,843 lianxi.py[line:9] WARNING warning message
2016-05-15 10:05:24,843 lianxi.py[line:10] ERROR error message
2016-05-15 10:05:24,843 lianxi.py[line:11] CRITICAL critical message
 
#可见在logging.basicConfig()函数中可以通过具体参数来更改logging模块的默认行为,可用参数有:
#ilename:用指定的文件名创建FileHandler,这样日志就会被储存在指定的文件中。
#filemode:文件打开的方式,在指定了filename时使用这个参数,默认为‘a’。
#format:  指定handler使用日期格式。
#datefmt:指定日期格式。
#level:     设置rootlogger的日志级别。
#stream: 用指定的stream创建streamhandler。当filename和stream两个参数,则stream参数会被忽略。
 
#format参数中可能用到的格式化串:
#%(name)s Logger的名字
#%(levelno)s 数字形式的日志级别
#%(levelname)s 文本形式的日志级别
#%(pathname)s 调用日志输出函数的模块的完整路径名,可能没有
#%(filename)s 调用日志输出函数的模块的文件名
#%(module)s 调用日志输出函数的模块名
#%(funcName)s 调用日志输出函数的函数名
#%(lineno)d 调用日志输出函数的语句所在的代码行
#%(created)f 当前时间,用UNIX标准的表示时间的浮 点数表示
#%(relativeCreated)d 输出日志信息时的,自Logger创建以 来的毫秒数
#%(asctime)s 字符串形式的当前时间。默认格式是 “2003-07-08 16:49:45,896”。逗号后面的是毫秒
#%(thread)d 线程ID。可能没有
#%(threadName)s 线程名。可能没有
#%(process)d 进程ID。可能没有
#%(message)s用户输出的消息
#Logging的实例应用过程。。。。。
 
import logging            #1、申请logging模块   
 
logger = logging.getLogger("My_log")  #2、创建一个logging       logging.getLogger(name),name可不要
 
logger.setLverl(logging.INFO)     #3、设置logging的日志显示的级别
  
Filelog = logging.FileHandler(logfilename)  #4、创建一个日志的文件句柄 logfilename 是日志文件的路径
 
Streamlog = logging.StreamHandler()         #4、创建呢一个日志输出的句柄
 
Fmatter = logging.Formatter("%(asctime)s - %(filename)s - %(levelname)-8s: %(message)s",
                                             datefmt='%Y-%m-%d %I:%M:%S %p')   
                                             #5、设定日志输出到文件的格式
Smatter = logging.Formatter("%(asctime)s %(filename)s %(levelname)s:%(message)s",datefmt='%Y-%m-%d %I:%M:%S')    #5、设定日志输出的格式
 
Filelog.setFormatter(Fmatter)   #6、将日志文件格式加载
Streamlog.setFormattrt(Fmatter)     #6、将输出日志格式加载
 
 
Filelog.setLevel(logging.INFO)     #7、设置日志文件的级别,将决定输出的消息
Stream.setLevel(logging.INFO)  #7、设置日志输出的级别
 
logger.addHandler(Filelog)  #8、设置文件的输出方向
logger.addHandler(Stream) 
 
 
logger.info("msg")  #9、设置日志的输出内容,还有critical、error、warning、info、debug级别
                                #上面设定的日志输出级别决定了在这里需要定义那种级别的日志
 
 
logger.removeHandler(Filelog) #10、将句柄销毁
logger.removeHandler(Streamlog)






本文转自 nw01f 51CTO博客,原文链接:http://blog.51cto.com/dearch/1775695,如需转载请自行联系原作者