How to use Python's built-in logging

王林
Release: 2023-05-10 10:55:05
forward
1360 people have browsed it

The main function of logging

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 Log level

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;

The basic function of logging

logging.basicConfig(**kwargs)
Copy after login

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')
Copy after login

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:

ParametersFunction
filenameSpecify 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
formatSpecify the format and content of the output. The default is levelname, name and message separated by colons
datefmtUse The specified date/time format, the same as that accepted by time.strftime().
levelSpecify 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')
Copy after login

The four major components (classes) of logging

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

##Logger.exception( )Output stack trace informationLogger.log()Set a custom level parameter to create a log record

logger can achieve the following functions in combination with the other three components to be introduced later:

  • Logger needs to output log information to the target location through the handler, and the target location can be sys. stdout and files etc. (this is not consistent with the logging.basicConfig(**kwargs) setting).

  • A Logger can set different Handlers, and different Handlers can output logs to different locations (different log files), and each Handler can set its own filter to Implement log filtering and retain the logs needed in actual projects. At the same time, each Handler can also set up different Formatter, and each Formatter can realize that the same log can be output to different places in different formats.

Handle

Processor; it can control where the recorded log is output (standard output/file/...), and the processor can also add filters Filter and formatter are used to control the output content and output format.

It has several common processors:

  • logging.StreamHandler standard stream processor, which sends messages to the standard output stream and error stream--> logging .StreamHandler(sys.stdout) # sys.stdout means pointing to the console, that is, standard output; when we call print obj to print an object in Python, we actually call sys.stdout.write(obj '\n') .

  • print prints the content you need to the console, and then appends a newline character

  • logging.FileHandler file handler, the message Send to file --> logging.FileHandler(log_path)

  • logging.RotatingFileHandler file handler, after the file reaches the specified size, enable a new file to store the log

  • logging.TimedRotatingFileHandler file handler, the log rotates the log file at a specific time interval

Some common methods of the handle class

MethodFunction 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
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")
Copy after login

基本过滤器类只允许低于指定的日志记录器层级结构中低于特定层级的事件,例如 这个用 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)sLogger的名字
%(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
Copy after login

datefmt 参数 有以下选项:

参数含义
%y两位数的年份表示(00-99)
%Y四位数的年份表示(000-9999)
%m月份(01-12)
%d月内中的一天(0-31)
%H24小时制小时数(0-23)
%I12小时制小时数(01-12)
%M分钟数(00=59)
%S 秒(00-59)

例如:

formatter = logging.Formatter('%(asctime)s %(levelname)-8s: %(message)s')		# -表示右对齐 8表示取8位
handler.formatter = formatter
Copy after login

datefmt 参数 有以下选项:

参数含义
%y两位数的年份表示(00-99)
%Y四位数的年份表示(000-9999)
%m月份(01-12)
%d月内中的一天(0-31)
%H24小时制小时数(0-23)
%I12小时制小时数(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
Copy after login

logging 的配置

  • 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
Copy after login
  • 在使用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")
Copy after login
  • 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]
Copy after login

同样的可以通过导入 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")
Copy after login

logging 和 print 的区别

看起来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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template