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=



  • 相关阅读:
    使用Fiddler工具在夜神模拟器或手机上抓包
    typedef & #defiine & struct
    int main (int argc, const char * argv[0]) 中参数的含义;指针数组和数组指针
    sql语句查询结果合并union all用法_数据库技巧
    jsp html 实现隐藏输入框,点击可以取消隐藏&&弹出输入框
    php弹出确认框
    mysql 插入string类型变量时候,需要注意的问题,妈的,害我想了好几个小时!!
    PHP页面跳转传值的三种常见方式
    Ubuntu&Mac下使用alias简化日常操作
    php mysql 中文乱码解决,数据库显示正常,php调用不正常
  • 原文地址:https://www.cnblogs.com/lisi01/p/11157902.html
Copyright © 2011-2022 走看看