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=



  • 相关阅读:
    arcgis中根据坐标文件生成要素类
    论C#及.net缺点及发展
    测绘类投稿指南
    .NET 4.0 Beta2中的BigInteger和Complex类(转)
    沈阳招聘.NET(C#)高级软件工程师
    测树学weibull拟合matlab程序,难者不会,会者不难
    景观生态专用软件
    使用匿名函数在后台线程中设置窗体控件属性 ()转
    matlab如何统计矩阵各元素的出现次数
    科学计算与开发环境
  • 原文地址:https://www.cnblogs.com/lisi01/p/11157902.html
Copyright © 2011-2022 走看看