zoukankan      html  css  js  c++  java
  • 三十七、python中的logging介绍

    A、单文件日志

    import logging
    #定义日志文件
    #文件格式
    logging.basicConfig(filename='log.log',
    format='%(asctime)s-%(name)s-%(levelname)s-%(module)s:%(message)s',
    datefmt='%Y-%m-%d %H:%M:%S %p',
    level=logging.INFO #相当于level=20
    )


    '''
    logging.ERROR
    CRITICAL = 50
    FATAL = CRITICAL
    ERROR = 40
    WARNING = 30
    WARN = WARNING
    INFO = 20
    DEBUG = 10
    NOTSET = 0
    '''

    '''
    注:只有【当前写等级】大于【日志等级】时,日志文件才被记录。
    '''
    logging.error('e')
    #2018-08-08 11:56:42 AM-root-ERROR-test01:zhangyu
    logging.warning('w')
    logging.critical('c')
    logging.fatal('f')
    logging.info('i')
    logging.debug('d')
    logging.log(logging.INFO,'333')
    B、多文件日志

    import logging

    # 定义文件
    file1 = logging.FileHandler('log1.log', 'a', encoding='utf-8')
    #创建格式
    fmt = logging.Formatter(fmt="%(asctime)s-%(name)s-%(levelname)s-%(module)s:%(message)s")
    #将文件应用该格式
    file1.setFormatter(fmt)

    file2 = logging.FileHandler('log2.log', 'a', encoding='utf-8')
    fmt = logging.Formatter()
    file2.setFormatter(fmt)

    # 定义日志
    logger1 = logging.Logger('aa', level=logging.ERROR)
    logger1.addHandler(file1)
    logger1.addHandler(file2)

    # 写日志
    logger1.critical('1111')
     
  • 相关阅读:
    CMU Database Systems
    Calcite分析
    CMU Database Systems
    CMU Advanced DB System
    笔记
    MyBatis Generator中文文档
    Run Test Case on Spark
    Flex报错Error #2048: 安全沙箱冲突
    看看这个超级有用的一种类型——匿名类型
    Java实战_手把手编写记事本
  • 原文地址:https://www.cnblogs.com/chushujin/p/9444053.html
Copyright © 2011-2022 走看看