Provides logging interfaces and numerous processing modules for users to store logs in various formats to help debug programs or record output information during program running.
logging The log level is divided into five levels, and the priority from high to low is:
**CRITICAL; ** Serious program error
**ERROR; **Program error/partial function error
**WARNING; **The program may have errors
**INFO; **When the program is running normally Information
DEBUG program debugging information
The default log recording level is WARNING, that is, it will only be recorded when the log level is greater than or equal to WARNING.
The commonly used recording level is INFO, which is used to record some information about the normal operation of the program (similar to print).
When the log level reaches WARNING or above, it indicates that the program cannot run normally at this time;
logging.basicConfig(**kwargs)
does not explicitly create a recorder ( logger), a root logger will be created by default, and logging.basicConfig(**kwargs) can create a streamHandle with a default Formatter and add it to the root logger to initialize the basic configuration.
For example
import logging logging.debug('Debug code!') logging.info('Run code!') logging.warning('Watch out!') logging.error('This is an error') logging.critical('This is a ciritical')
In the above code, logging does not explicitly create a logger (logging.getLogger), but directly uses debug(), info(), warning(), error() , the default root logger will be used when critical() is called, and the custom or default logging.basicConfig(**kwargs) will be automatically called to initialize the root logger.
The parameters in the custom logging.basicConfig(**kwargs) have the following main options:
Parameters | Function |
---|---|
filename | Specify the file name to save the log, create a FileHandler with the specified file name, and the recorded log will be saved to the file |
format | Specify the format and content of the output. The default is levelname, name and message separated by colons |
datefmt | Use The specified date/time format, the same as that accepted by time.strftime(). |
level | Specify the root logger level, the default is logging.WARNING |
stream | Specify the output stream of the log. You can specify the output to sys.stderr, std.stdout or a file. The default output is to sys.stderr. Initialize StramHandler using the specified stream. Note: The stream and filename parameters are incompatible. If both are used at the same time, a ValueError will be raised. |
For example, the following custom logging.basicConfig (**kwargs) to initialize the root logger to obtain log records of DEBUG level and above and save them to the log.txt file.
import logging logging.basicConfig(filename='./log.txt', format='%(asctime)s-%(name)s-%(levelname)s-%(message)s-%(funcName)s:%(lineno)d', level=logging.DEBUG) logging.debug('Debug code!') logging.info('Run code!') logging.warning('Watch out!') logging.error('This is an error') logging.critical('This is a ciritical')
Logger
In addition to the root logger (root logger), the most important thing is that you can create your own logger.
Through module-level functions logging.getLogger(name)
Instantiate the logger
By default, the logger adopts a hierarchical structure, through .
to distinguish different levels. For example, if there is a logger named foo
, then foo.a
and foo.b
are both child loggers of foo
. Of course, the first or top-level logger is the root logger. If name=None, the root logger is built.
You can directly use the name of the current module as the name of the logger logging.getLogger(__name__)
Child-level loggers usually do not need to set the log level and Handler separately. , if the child logger is not set separately, its behavior is delegated to the parent. For example, the level of logger foo
is INFO, but neither foo.a
nor foo.b
has a log level set. At this time, foo.a
and foo.b
will follow the level setting of foo
, that is, only logs with a level greater than or equal to INFO will be recorded; and if foo is not set, You will find the root logger. The default level of root is WARGING.
Some commonly used methods of the logger class
Method | Function description |
---|---|
Logger.setLevel() | Set the log message level that the logger will process |
Logger.addHandler() | Add a handler object |
Logger.removeHandler() | Remove a handler object |
Logger.addFilter( ) | Add a filter object |
Logger.removeFilter() | Remove a filter object |
Logger.debug() | Set DEBUG level logging |
Logger.info() | Set INFO level logging |
Logger.warning() | Set WARNING level logging |
Logger.error() | Settings ERROR level logging |
Logger.critical() | Set CRITICAL level logging |
Output stack trace information | |
Set a custom level parameter to create a log record |
Handler.setLevel() | Set the minimum severity level of log messages that the processor will process |
Handler.setFormatter() | Set a format object for the processor |
Handler.addFilter() | Add a filter object for the processor |
Handler.removeFilter() | Remove a filter object for the handler |
logging.StramHandler() | Send log messages to the output Stream, such as std.out, std.err |
logging.FilterHandler() | Send log messages to disk files. By default, the file size will grow wirelessly |
RotationFileHandler() | Send log messages to disk files and support cutting log files according to size |
TimeRotatingFileHandler() | Send log messages to disk files and support splitting log files by time |
logging.handers.HTTPHandler() | Send log messages through GET or POST to an HTTP server |
logging.handlers.SMTPHandler() | Send log messages to email address |
Filter
filter组件用来过滤 logger 对象,一个 filter 可以直接添加到 logger对象上,也可以添加到 handler 对象上,而如果在logger和handler中都设置了filter,则日志是先通过logger的filter,再通过handler的filter。由于所有的信息都可以经过filter,所以filter不仅可以过滤信息,还可以增加信息。
Filter 类的实例化对象可以通过 logging.Filter(name) 来创建,其中name 为 记录器的名字,如果没有创建过该名字的记录器,就不会输出任何日志:
filter = logging.Filter("foo.a")
基本过滤器类只允许低于指定的日志记录器层级结构中低于特定层级的事件,例如 这个用 foo.a
初始化的过滤器,则foo.a.b
;foo.a.c
等日志记录器记录的日志都可以通过过滤器,而foo.c
; a.foo
等就不能通过。如果name为空字符串,则所有的日志都能通过。
Filter 类 有 三个方法 :
addFilter(filter) : 为 logger(logger..addFilter(filter)) 或者 handler(handler..addFilter(filter)) 增加过滤器
removeFilter(filter) : 为 logger 或者 handler 删除一个过滤器
filter(record) : 表示是否要记录指定的记录?返回零表示否,非零表示是。一般自定义Filter需要继承Filter基类,并重写filter方法
Formatter
格式化日志的输出;实例化:formatter = logging.Formatter(fmt=None,datefmt=None)
; 如果不指明 fmt,将默认使用 ‘%(message)s’ ,如果不指明 datefmt,将默认使用 ISO8601 日期格式。
其中 fmt 参数 有以下选项:
%(name)s | Logger的名字 |
---|---|
%(levelno)s | 数字形式的日志级别 |
%(levelname)s | 文本形式的日志级别;如果是logger.debug则它是DEBUG,如果是logger.error则它是ERROR |
%(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 | 用户输出的消息; 假如有logger.warning("NO Good"),则在%(message)s位置上是字符串NO Good |
例如:
formatter = logging.Formatter('%(asctime)s %(levelname)-8s: %(message)s') # -表示右对齐 8表示取8位 handler.formatter = formatter
datefmt 参数 有以下选项:
参数 | 含义 |
---|---|
%y | 两位数的年份表示(00-99) |
%Y | 四位数的年份表示(000-9999) |
%m | 月份(01-12) |
%d | 月内中的一天(0-31) |
%H | 24小时制小时数(0-23) |
%I | 12小时制小时数(01-12) |
%M | 分钟数(00=59) |
%S 秒 | (00-59) |
例如:
formatter = logging.Formatter('%(asctime)s %(levelname)-8s: %(message)s') # -表示右对齐 8表示取8位 handler.formatter = formatter
datefmt 参数 有以下选项:
参数 | 含义 |
---|---|
%y | 两位数的年份表示(00-99) |
%Y | 四位数的年份表示(000-9999) |
%m | 月份(01-12) |
%d | 月内中的一天(0-31) |
%H | 24小时制小时数(0-23) |
%I | 12小时制小时数(01-12) |
%M | 分钟数(00=59) |
%S 秒 | (00-59) |
例子:
formatter = logging.Formatter("%(asctime)s %(levelname)s %(message)s","%Y%m%d-%H:%M:%S") handler.formatter = formatter
conf 形式的配置
在 loguser.conf 中 写入相关的信息
[loggers] keys=root,fileLogger,rotatingFileLogger [handlers] keys=consoleHandler,fileHandler,rotatingFileHandler [formatters] keys=simpleFormatter [logger_root] level=INFO handlers=consoleHandler [logger_fileLogger] level=INFO handlers=fileHandler qualname=fileLogger propagate=0 [logger_rotatingFileLogger] level=INFO handlers=consoleHandler,rotatingFileHandler qualname=rotatingFileLogger propagate=0 [handler_consoleHandler] class=StreamHandler level=INFO formatter=simpleFormatter args=(sys.stdout,) [handler_fileHandler] class=FileHandler level=INFO formatter=simpleFormatter args=("logs/fileHandler_test.log", "a") [handler_rotatingFileHandler] class=handlers.RotatingFileHandler level=WARNING formatter=simpleFormatter args=("logs/rotatingFileHandler.log", "a", 10*1024*1024, 50) [formatter_simpleFormatter] format=%(asctime)s - %(module)s - %(levelname)s -%(thread)d : %(message)s datefmt=%Y-%m-%d %H:%M:%S
在使用logger时,直接导入配置文件即可
from logging import config with open('./loguser.conf', 'r', encoding='utf-8') as f: ## 加载配置 config.fileConfig(f) ## 创建同名Logger,其按照配置文件的handle,formatter,filter方法初始化 logger = logging.getLogger(name="fileLogger")
yaml 形式配置文件
在 loguser.yaml文件 中 配置相关信息
version: 1 disable_existing_loggers: False # formatters配置了日志输出时的样式 # formatters定义了一组formatID,有不同的格式; formatters: brief: format: "%(asctime)s - %(message)s" simple: format: "%(asctime)s - [%(name)s] - [%(levelname)s] :%(levelno)s: %(message)s" datefmt: '%F %T' # handlers配置了需要处理的日志信息,logging模块的handler只有streamhandler和filehandler handlers: console: class : logging.StreamHandler formatter: brief level : DEBUG stream : ext://sys.stdout info_file_handler: class : logging.FileHandler formatter: simple level: ERROR filename: ./logs/debug_test.log error_file_handler: class: logging.handlers.RotatingFileHandler level: ERROR formatter: simple filename: ./logs/errors.log maxBytes: 10485760 # 10MB #1024*1024*10 backupCount: 50 encoding: utf8 loggers: #fileLogger, 就是在代码中通过logger = logging.getLogger("fileLogger")来获得该类型的logger my_testyaml: level: DEBUG handlers: [console, info_file_handler,error_file_handler] # root为默认情况下的输出配置, 当logging.getLogger("fileLoggername")里面的fileLoggername没有传值的时候, # 就是用的这个默认的root,如logging.getLogger(__name__)或logging.getLogger() root: level: DEBUG handlers: [console]
同样的可以通过导入 yaml 文件加载配置
with open('./loguser.yaml', 'r', encoding='utf-8') as f: yaml_config = yaml.load(stream=f, Loader=yaml.FullLoader) config.dictConfig(config=yaml_config) root = logging.getLogger() # 子记录器的名字与配置文件中loggers字段内的保持一致 # loggers: # my_testyaml: # level: DEBUG # handlers: [console, info_file_handler,error_file_handler] my_testyaml = logging.getLogger("my_testyaml")
看起来logging要比print复杂多了,那么为什么推荐在项目中使用 logging 记录日志而不是使用print 输出程序信息呢。
相比与print logging 具有以下优点:
可以通过设置不同的日志等级,在 release 版本中只输出重要信息,而不必显示大量的调试信息;
print 将所有信息都输出到标准输出中,严重影响开发者从标准输出中查看其它数据;logging 则可以由开发者决定将信息输出到什么地方,以及怎么输出;
和 print 相比,logging 是线程安全的。(python 3中 print 也是线程安全的了,而python 2中的print不是)(线程安全是指在多线程时程序不会运行混乱;而python 2 中的print 分两步打印信息,第一打印字符串,第二打印换行符,如果在这中间发生线程切换就会产生输出混乱。这就是为什么python2的print不是原子操作,也就是说其不是线程安全的)印信息,第一打印字符串,第二打印换行符,如果在这中间发生线程切换就会产生输出混乱。这就是为什么python2的print不是原子操作,也就是说其不是线程安全的)
The above is the detailed content of How to use Python's built-in logging. For more information, please follow other related articles on the PHP Chinese website!