首页 > 代码库 > Python3 logging 模块

Python3 logging 模块

Python3 logging模块

日志模块:

用于便捷记录日志且线程安全的模块

CRITICAL = 50

FATAL = CRITICAL

ERROR = 40

WARNING = 30

WARN = WARNING

INFO =20

DEBUG = 10

NOTSET = 0

设置为debug

 import logging

技术分享
 1 import logging
 2  logging.debug(debug)
 3  logging.info(info)
 4  logging.warning(warning)
 5  logging.error(error)
 6  logging.critical(critical)
 7 
 8  logging.basicConfig(filename = access.log,format = %(asctime)s-%(name)s-%(levelname)s-%(module)s:%(message)s,
 9                      datefmt = %Y-%m-%d %H:%M:%S %p,
10                      level = 10)
11  logging.debug(debug)
12 formatter1=logging.Formatter(%(asctime)s - %(name)s - %(levelname)s -%(module)s:  %(message)s,
13                     datefmt=%Y-%m-%d %H:%M:%S %p,)
14 fh1 = logging.FileHandler(test1.log)
15 fh2 = logging.FileHandler(test2.log)
16 fh3 = logging.FileHandler(test3.log)
17 ch = logging.StreamHandler()
18 
19 fh1.setFormatter(formatter1)
20 fh2.setFormatter(formatter1)
21 fh3.setFormatter(formatter1)
22 ch.setFormatter(formatter1)
23 
24 logger1 = logging.getLogger(egon)
25 logger1.setLevel(10)
26 logger1.addHandler(fh1)
27 logger1.addHandler(fh2)
28 logger1.addHandler(fh3)
29 logger1.addHandler(ch)
30 logger1.debug(debug)
31 logger1.info(info)
32 logger1.warning(warning)
33 logger1.critical(critical)
View Code

 logging配置模板

技术分享
  1 import os
  2 
  3 import logging.config
  4 
  5  
  6 
  7 # 定义三种日志输出格式 开始
  8 
  9  
 10 
 11 standard_format = [%(asctime)s][%(threadName)s:%(thread)d][task_id:%(name)s][%(filename)s:%(lineno)d]  12 
 13                   [%(levelname)s][%(message)s] #其中name为getlogger指定的名字
 14 
 15  
 16 
 17 simple_format = [%(levelname)s][%(asctime)s][%(filename)s:%(lineno)d]%(message)s
 18 
 19  
 20 
 21 id_simple_format = [%(levelname)s][%(asctime)s] %(message)s
 22 
 23  
 24 
 25 # 定义日志输出格式 结束
 26 
 27  
 28 
 29 logfile_dir = os.path.dirname(os.path.abspath(__file__))  # log文件的目录
 30 
 31  
 32 
 33 logfile_name = all2.log  # log文件名
 34 
 35  
 36 
 37 # 如果不存在定义的日志目录就创建一个
 38 
 39 if not os.path.isdir(logfile_dir):
 40 
 41     os.mkdir(logfile_dir)
 42 
 43  
 44 
 45 # log文件的全路径
 46 
 47 logfile_path = os.path.join(logfile_dir, logfile_name)
 48 
 49  
 50 
 51 # log配置字典
 52 
 53 LOGGING_DIC = {
 54 
 55     version: 1,
 56 
 57     disable_existing_loggers: False,
 58 
 59     formatters: {
 60 
 61         standard: {
 62 
 63             format: standard_format
 64 
 65         },
 66 
 67         simple: {
 68 
 69             format: simple_format
 70 
 71         },
 72 
 73         id_simple: {
 74 
 75             format: id_simple_format
 76 
 77         },
 78 
 79     },
 80 
 81     filters: {},
 82 
 83     handlers: {
 84 
 85         #打印到终端的日志
 86 
 87         console: {
 88 
 89             level: DEBUG,
 90 
 91             class: logging.StreamHandler,  # 打印到屏幕
 92 
 93             formatter: simple
 94 
 95         },
 96 
 97         #打印到文件的日志,收集info及以上的日志
 98 
 99         default: {
100 
101             level: DEBUG,
102 
103             class: logging.handlers.RotatingFileHandler,  # 保存到文件
104 
105             formatter: standard,
106 
107             filename: logfile_path,  # 日志文件
108 
109             maxBytes: 1024*1024*5,  # 日志大小 5M
110 
111             backupCount: 5,
112 
113             encoding: utf-8,  # 日志文件的编码,再也不用担心中文log乱码了
114 
115         },
116 
117         boss: {
118 
119             level: DEBUG,
120 
121             class: logging.handlers.RotatingFileHandler,  # 保存到文件
122 
123             formatter: standard,
124 
125             filename: boss.log,  # 日志文件
126 
127             maxBytes: 1024 * 1024 * 5,  # 日志大小 5M
128 
129             backupCount: 5,
130 
131             encoding: utf-8,  # 日志文件的编码,再也不用担心中文log乱码了
132 
133         },
134 
135     },
136 
137     loggers: {
138 
139         #logger1=logging.getLogger(__name__)拿到的logger配置
140 
141         ‘‘: {
142 
143             handlers: [default, console],  # 这里把上面定义的两个handler都加上,即log数据既写入文件又打印到屏幕
144 
145             level: DEBUG,
146 
147             propagate: True,  # 向上(更高level的logger)传递
148 
149         },
150 
151         #logger1=logging.getLogger(‘collect‘)拿到的logger配置
152 
153         collect: {
154 
155             handlers: [boss,],  # 这里把上面定义的两个handler都加上,即log数据既写入文件又打印到屏幕
156 
157             level: DEBUG,
158 
159             propagate: True,  # 向上(更高level的logger)传递
160 
161         },
162 
163     },
164 
165 }
166 
167 def load_my_logging_cfg():
168 
169     logging.config.dictConfig(LOGGING_DIC)  # 导入上面定义的logging配置
170 
171     # logger = logging.getLogger(__name__)  # 生成一个log实例
172 
173     # logger.info(‘It works!‘)  # 记录该文件的运行状态
174 
175 if __name__ == __main__:
176 
177     load_my_logging_cfg()
View Code

 调用 logging 配置模板

技术分享
 1 import logging
 2 import my_log_settings
 3 my_log_settings.load_my_logging_cfg()
 4 
 5 logger1=logging.getLogger(__name__)
 6 logger2=logging.getLogger(collect)
 7 
 8 logger1.debug(默认日志的debug)
 9 logger2.debug(给老板一封信)
10 logger2.debug(给前台MM一封信)
View Code

 

Python3 logging 模块