• logging of python


    Configuration functions

    https://docs.python.org/3/library/logging.config.html#module-logging.config

    三种配置加载方式,

    (1)加载配置单额词典变量

    (2)加载配置文件

    (3)从监听端口中读取配置,并生效。

    The following functions configure the logging module. They are located in the logging.config module. Their use is optional — you can configure the logging module using these functions or by making calls to the main API (defined in logging itself) and defining handlers which are declared either in logging or logging.handlers.

    logging.config.dictConfig(config)

    Takes the logging configuration from a dictionary. The contents of this dictionary are described in Configuration dictionary schema below.

    logging.config.fileConfig(fname, defaults=None, disable_existing_loggers=True)

    Reads the logging configuration from a configparser-format file. The format of the file should be as described in Configuration file format. This function can be called several times from an application, allowing an end user to select from various pre-canned configurations (if the developer provides a mechanism to present the choices and load the chosen configuration).

    logging.config.listen(port=DEFAULT_LOGGING_CONFIG_PORT, verify=None)

    Starts up a socket server on the specified port, and listens for new configurations. If no port is specified, the module’s default DEFAULT_LOGGING_CONFIG_PORT is used. Logging configurations will be sent as a file suitable for processing by dictConfig() or fileConfig(). Returns a Thread instance on which you can call start() to start the server, and which you can join() when appropriate. To stop the server, call stopListening().

    When to use logging

    https://docs.python.org/3/howto/logging.html#logging-from-multiple-modules

    如下是各种日志打印方法 和 调用时机的介绍。

    然后是 各种等级的 划分, 其级别从上到下一次地增幅, 如果设置为WARNING级别, 则其和其以上级别的日志都将会输出。

    Logging provides a set of convenience functions for simple logging usage. These are debug(), info(), warning(), error() and critical(). To determine when to use logging, see the table below, which states, for each of a set of common tasks, the best tool to use for it.

    Task you want to perform

    The best tool for the task

    Display console output for ordinary usage of a command line script or program

    print()

    Report events that occur during normal operation of a program (e.g. for status monitoring or fault investigation)

    logging.info() (or logging.debug() for very detailed output for diagnostic purposes)

    Issue a warning regarding a particular runtime event

    warnings.warn() in library code if the issue is avoidable and the client application should be modified to eliminate the warning

    logging.warning() if there is nothing the client application can do about the situation, but the event should still be noted

    Report an error regarding a particular runtime event

    Raise an exception

    Report suppression of an error without raising an exception (e.g. error handler in a long-running server process)

    logging.error(), logging.exception() or logging.critical() as appropriate for the specific error and application domain

    The logging functions are named after the level or severity of the events they are used to track. The standard levels and their applicability are described below (in increasing order of severity):

    Level

    When it’s used

    DEBUG

    Detailed information, typically of interest only when diagnosing problems.

    INFO

    Confirmation that things are working as expected.

    WARNING

    An indication that something unexpected happened, or indicative of some problem in the near future (e.g. ‘disk space low’). The software is still working as expected.

    ERROR

    Due to a more serious problem, the software has not been able to perform some function.

    CRITICAL

    A serious error, indicating that the program itself may be unable to continue running.

    The default level is WARNING, which means that only events of this level and above will be tracked, unless the logging package is configured to do otherwise.

    Events that are tracked can be handled in different ways. The simplest way of handling tracked events is to print them to the console. Another common way is to write them to a disk file.

    Logging from multiple modules

    在多个模块中使用 logging 文件,

    (1)应该在app文件中,设置日志等级

    (2)在模块文件中, 仅仅调用日志接口。

    If your program consists of multiple modules, here’s an example of how you could organize logging in it:

    # myapp.py
    import logging
    import mylib
    
    def main():
        logging.basicConfig(filename='myapp.log', level=logging.INFO)
        logging.info('Started')
        mylib.do_something()
        logging.info('Finished')
    
    if __name__ == '__main__':
        main()
    # mylib.py
    import logging
    
    def do_something():
        logging.info('Doing something')

    If you run myapp.py, you should see this in myapp.log:

    INFO:root:Started
    INFO:root:Doing something
    INFO:root:Finished

    10+ practical examples to use python logging() in detail

    Python Script Example: Write messages to console and log file both

    https://www.golinuxcloud.com/python-logging/#Python_Script_Example_Write_messages_to_console_and_log_file_both

    We learned about two different logging.handlers() separately. We can combine both of them in a single script to be able to write messages on console terminal as well in a log file.
    In this script we will use both FileHandler and Streamhandler inside basicConfig()

    #!/usr/bin/env python3
    
    import logging
    import sys
    
    # Log file location
    logfile = '/tmp/debug.log'
    # Define the log format
    log_format = (
        '[%(asctime)s] %(levelname)-8s %(name)-12s %(message)s')
    
    # Define basic configuration
    logging.basicConfig(
        # Define logging level
        level=logging.DEBUG,
        # Declare the object we created to format the log messages
        format=log_format,
        # Declare handlers
        handlers=[
            logging.FileHandler(logfile),
            logging.StreamHandler(sys.stdout),
        ]
    )
    
    # Define your own logger name
    logger = logging.getLogger("my_logger")
    
    # Write messages with all different types of levels
    logger.debug('debug')
    logger.info('info')
    logger.warning('warning')
    logger.error('error')
    logger.critical('critical')

    Logging in Python 3, How To Output Logs to File and Console

    https://pythonhowtoprogram.com/logging-in-python-3-how-to-output-logs-to-file-and-console/

    日志工作流

    (1)python程序调用不同等级的API,记录日志

    (2)日志流入日志模块。日志模块预先已经加载配置 (理解 这里应该是随着程序启动的时候加载的, 不是每个日志API调用时候加载的

    (3)日志模块将日志分发到每一个Handler处理器中, 此处是一个文件 Handler, 将对应日志输出到文件中

    (4)过滤器进行过滤处理,决定哪些日志可以记录到日志文件中。 此处过滤器是挂载到Handler上。

    (5)添加日志输出的格式。

    (6)输出到文件中。

    Logging is an essential tool for development as it helps us in keeping track of events, monitoring the program behavior, and debugging; and without it making big projects and running them is unimaginable. It is a vital tool to understand what’s happening in your code during development and testing, but also becomes an essential tool once your code goes into production

     

    例子

    输出到三个目标

    file 记录warning及以上的日志

    cric_file 记录critical级别的日志

    stream 显示所有日志到控制台

    Now we are going to create 3 handlers namely file , cric_file , stream .
    file will be storing all the logs of level warning and above in the file sample.log
    cric_file will be storing all the logs of critical level in the file Critical.log
    stream will be showing all the logs in the console.
    So, as I said about handlers they can be configured differently as you want; like having different levels and all other configs. Dont worry everything is explained below.

    import logging
    
    # Creating logger
    mylogs = logging.getLogger(__name__)
    mylogs.setLevel(logging.DEBUG)
    
    # Handler - 1
    file = logging.FileHandler("Sample.log")
    fileformat = logging.Formatter("%(asctime)s:%(levelname)s:%(message)s")
    file.setLevel(logging.WARNING)
    file.setFormatter(fileformat)
    
    # Handler - 2
    cric_file = logging.FileHandler("Critical.log")
    cric_file.setLevel(logging.CRITICAL)
    cric_file.setFormatter(fileformat)
    # format we can use it anywhere.
    
    # Handler - 3
    stream = logging.StreamHandler()
    streamformat = logging.Formatter("%(levelname)s:%(module)s:%(message)s")
    stream.setLevel(logging.DEBUG)
    stream.setFormatter(streamformat)
    
    # Adding all handlers to the logs
    mylogs.addHandler(file)
    mylogs.addHandler(stream)
    mylogs.addHandler(cric_file)
    
    # Some demo codes
    mylogs.debug("debug")
    mylogs.info("info")
    mylogs.warning("warn")
    mylogs.critical("critical")
    mylogs.error("error")
    出处:http://www.cnblogs.com/lightsong/ 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。
  • 相关阅读:
    Git常用命令
    更新CentOS内核
    VMware虚拟机安装Ubuntu系统步骤详解
    Ubuntu安装遇到的问题
    IOT OS and OTA
    gcc c asm,C程序内嵌汇编
    makefile and make tips
    RTEMS目录梳理Sparc
    关于FreeRTOS的信号量、队列
    FreeRTOS任务源码分析以及程序堆栈与任务堆栈的关系
  • 原文地址:https://www.cnblogs.com/lightsong/p/14395221.html
Copyright © 2020-2023  润新知