zoukankan      html  css  js  c++  java
  • logging库基本使用(官方文档搬运)

    # A simple example
    import logging
    logging.warning('Watch out!') # will print a message to the console
    logging.info('I told you so') # will not print anything

    #################################
    # Logging to a file
    import logging
    logging.basicConfig(filename='example.log',level=logging.DEBUG)
    logging.debug('This message should go to the log file')
    logging.info('So should this')
    logging.warning('And this, too')

    # 通过file_mode指定参数
    logging.basicConfig(filename='example.log', filemode='w', level=logging.DEBUG)


    ##################################
    # Logging from multiple modules
    # 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')


    ##################################
    # Displaying the date/time in messages
    import logging
    logging.basicConfig(format='%(asctime)s %(message)s')
    logging.warning('is when this event was logged.')

    # 自定义时间输出格式
    import logging
    logging.basicConfig(format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p')
    logging.warning('is when this event was logged.')


    ##################################
    # 进阶
    #Configuring Logging
    import logging

    # create logger
    logger = logging.getLogger('simple_example')
    logger.setLevel(logging.DEBUG)

    # create console handler and set level to debug
    ch = logging.StreamHandler()
    ch.setLevel(logging.DEBUG)

    # create formatter
    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')

    # add formatter to ch
    ch.setFormatter(formatter)

    # add ch to logger
    logger.addHandler(ch)

    # 'application' code
    logger.debug('debug message')
    logger.info('info message')
    logger.warning('warn message')
    logger.error('error message')
    logger.critical('critical message')

    ################################
    # 使用配置文件实现上述功能
    # class logging.FileHandler(filename, mode='a', encoding=None, delay=False)
    import logging
    import logging.config

    logging.config.fileConfig('logging.conf')

    # create logger
    logger = logging.getLogger('simpleExample')

    # 'application' code
    logger.debug('debug message')
    logger.info('info message')
    logger.warning('warn message')
    logger.error('error message')
    logger.critical('critical message')

    # Here is the logging.conf file:
    [loggers]
    keys=root,simpleExample

    [handlers]
    keys=consoleHandler

    [formatters]
    keys=simpleFormatter

    [logger_root]
    level=DEBUG
    handlers=consoleHandler

    [logger_simpleExample]
    level=DEBUG
    handlers=consoleHandler
    qualname=simpleExample
    propagate=0

    [handler_consoleHandler]
    class=StreamHandler
    level=DEBUG
    formatter=simpleFormatter
    args=(sys.stdout,)

    [formatter_simpleFormatter]
    format=%(asctime)s - %(name)s - %(levelname)s - %(message)s
    datefmt=



  • 相关阅读:
    雨天拍照技巧
    was8.5和aop的问题:JVMVRFY013 违反类装入约束o
    Docker Swarm
    Docker compose
    docker 网络
    dockerFile
    docker容器数据卷
    docker容器数据卷
    dokcer镜像详解
    Portainer可视化面板安装
  • 原文地址:https://www.cnblogs.com/lisi01/p/11157902.html
Copyright © 2011-2022 走看看