The use of Python's logging module

Source: Internet
Author: User

Python's logging module is used to write logs and is a standard Python module.

Structure of the logging
    • View logging python source code, there are four main classes to realize the function;
    1. Loggers: Provides interfaces directly used by the application, such as related configuration settings;
    2. Handlers: The log generated by the loggers to the specified location, set the location of the log save;
    3. Filters: Filtering operation of output log;
    4. Formatters: The output format of the control log;
Level of logging
    • Debug: Priority 10, record debugging details, only open during debugging;

    • INFO: Priority 20, record common messages, including errors and warnings, etc.;

    • WARNING: Priority 30, record the relevant warning information;

    • Error: Priority 40, log errors message, program crashes;

    • CRITICAL: Priority 50, log error message;

If you do not set the level, the default is warning, and the system records log information that is set at the log level above the priority level.

Formatters defining the output format of the log
%(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 用户输出的消息
# 定义一个输出格式的对象formatter = logging.Formatter(‘%(asctime)s %(levelname)s: %(message)s‘)
Log processor
    • There are two common types of processors:

Streamhandler: Used to print logs to the console;

Filehandler: Used to print logs to log files;

    • Other processors
RotatingHandler:logging.handlers.RotatingHandler;日志回滚方式,支持日志文件最大数量和日志文件回滚TimeRotatingHandler:logging.handlers.TimeRotatingHandler;日志回滚方式,在一定时间区域内回滚日志文件SocketHandler:logging.handlers.SocketHandler;远程输出日志到TCP/IP socketsDatagramHandler:logging.handlers.DatagramHandler;远程输出日志到UDP socketsSMTPHandler:logging.handlers.SMTPHandler;远程输出日志到邮件地址SysLogHandler:logging.handlers.SysLogHandler;日志输出到syslogNTEventLogHandler:logging.handlers.NTEventLogHandler;远程输出日志到Windows NT/2000/XP的事件日志MemoryHandler:logging.handlers.MemoryHandler;日志输出到内存中的指定bufferHTTPHandler:logging.handlers.HTTPHandler;通过"GET"或者"POST"远程输出到HTTP服务器
Basic use of Logger
import loggingimport sysdef get_logger(appname):    # 获取logger实例,如果参数为空则返回root logger    logger = logging.getLogger(appname)    # 指定logger输出格式    formatter = logging.Formatter(‘%(asctime)s %(levelname)s: %(message)s‘)    # 设置文件处理器,加载处理格式    file_handler = logging.FileHandler("test.log")    file_handler.setFormatter(formatter)    # 控制台日志    console_handler = logging.StreamHandler(sys.stdout)    console_handler.formatter = formatter    # 为logger添加的日志处理器    logger.addHandler(file_handler)    logger.addHandler(console_handler)    # 指定日志的最低输出级别,默认为WARN级别    logger.setLevel(logging.INFO)if __name__  == "__main__":    logger = get_logger(‘test‘)    logger.debug(‘this is debug info‘)    logger.info(‘this is information‘)    logger.warn(‘this is warning message‘)    logger.error(‘this is error message‘)    logger.fatal(‘this is fatal message, it is same as logger.critical‘)    logger.critical(‘this is critical message‘)
How to configure Logger
    1. Through the code to complete the configuration, mainly through the GetLogger method implementation, but not good modification;
    2. Through the Basicconfig method, this approach is fast but not sufficiently structured;
    3. File configuration via Logging.config.fileConfig (filepath).
Detailed file Configuration
# logging.conf[loggers]    # 定义日志的对象名称是什么,注意必须定义root,否则报错。keys=root,main[handlers]  # 定义处理器的名称是什么,可以有多个,用逗号隔开keys=consoleHandler[formatters]  # 定义输出格式对象的名称,可以有多个,用逗号隔开keys=simpleFormatter[logger_root]  # 配置root对象的日志记录级别和使用的处理器level=INFOhandlers=consoleHandler[logger_main] # 配置main对象的日志记录级别和使用的处理器,qualname值得就是日志对象的名字level=INFOhandlers=consoleHandlerqualname=mainpropagate=0  # logger对象把日志记录传递给所有相关的handler的时候,会(逐级向上)寻找这个logger和它所有的父logger的全部handler,propagate=1表示会继续向上搜寻;propagate=0表示停止搜寻,这个参数涉及重复打印的坑。[handler_consoleHandler] # 配置处理器consoleHandlerclass=StreamHandler      level=WARNINGformatter=simpleFormatterargs=(sys.stdout,)[formatter_simpleFormatter]  # 配置输出格式过滤器simpleFormatterformat=%(asctime)s - %(name)s - %(levelname)s - %(message)s
    • Note: You can see that both logger and handler can set the log level, and the log output is the highest level.
Some details
    1. Inheritance of Logger

Logging.getlogger (appname), the Logger object that is acquired by the same appname is the same, and Appname.name named Logger inherits all of the properties of appname and can not be reconfigured logger.

    1. Ways to output error messages
try:    raiseexcept:    logger.error(‘this is error message‘,exc_info = True)    # 或    logger.exception("Failed to open sklearn.txt from logger.exception")# 设置exc_info为True后,会自动将错误的信息也发送到日志中;# 直接使用exception实现一样的功能。
Monitoring Logger configuration changes
    • The Logging.config.listen (port) function allows the application to listen for new configuration information on a socket, changing the configuration at run time without restarting the application.
import logging.config  import logging  import timelogging.config.fileConfig("logging.conf")  logger = logging.getLogger("test.listen")t = logging.config.listen(9999)  t.setDaemon(True)  t.start()try:      while True:        logger.info(‘running.‘)        time.sleep(3)except (KeyboardInterrupt, SystemExit, Exception):      logging.config.stopListening()

The use of Python's logging module

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.