zoukankan      html  css  js  c++  java
  • python 日志

    logger.conf 配置文件

    #logger.conf
    
    ###############################################
    
    [loggers]
    keys=root,example01,example02
    
    [logger_root]
    level=DEBUG
    handlers=hand01,hand02
    
    [logger_example01]
    handlers=hand01,hand02
    qualname=wms
    propagate=0
    
    [logger_example02]
    handlers=hand01,hand03
    qualname=example02
    propagate=0
    
    ###############################################
    #handlers主要设置使用哪个handler,日志级别
    # 默认情况下,日志级别为WARNING;
    # 日志级别大小关系为:CRITICAL > ERROR > WARNING > INFO > DEBUG > NOTSET,当然也可以自己定义日志级别。
    [handlers]
    keys=hand01,hand02,hand03
    
    [handler_hand01]
    class=StreamHandler
    #这里设置info,比其级别高的日志都会被打印到控制台
    level=INFO
    formatter=form02
    args=(sys.stderr,)
    
    [handler_hand02]
    class=FileHandler
    #这里设置debug,比其级别高的日志都会被存放到指定文件中
    level=DEBUG
    formatter=form01
    # a表示追加,w表示覆盖
    args=('my.log', 'a')
    
    [handler_hand03]
    class=handlers.RotatingFileHandler
    level=INFO
    formatter=form02
    args=('myapp.log', 'a', 10*1024*1024, 5)
    
    ###############################################
    # formaters 主要设置输出格式,日期格式
    
    [formatters]
    keys=form01,form02
    
    [formatter_form01]
    format=%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s
    #datefmt=%a, %d %b %Y %H:%M:%S
    datefmt=%Y-%m-%d %H:%M:%S
    
    [formatter_form02]
    # 此处的-8个空白字符
    format=%(asctime)s %(name)s: %(levelname)-8s %(message)s
    datefmt=%Y-%m-%d %H:%M:%S
    # #encoding=utf-8
    import logging
    
    # logging.basicConfig函数各参数:
    # filename: 指定日志文件名
    # filemode: 和file函数意义相同,指定日志文件的打开模式,'w'或'a'。w:覆盖、a:追加
    # format: 指定输出的格式和内容,format可以输出很多有用信息,如上例所示:
    #  %(levelno)s: 打印日志级别的数值
    #  %(levelname)s: 打印日志级别名称
    #  %(pathname)s: 打印当前执行程序的路径,其实就是sys.argv[0]
    #  %(filename)s: 打印当前执行程序名
    #  %(funcName)s: 打印日志的当前函数
    #  %(lineno)d: 打印日志的当前行号
    #  %(asctime)s: 打印日志的时间
    #  %(thread)d: 打印线程ID
    #  %(threadName)s: 打印线程名称
    #  %(process)d: 打印进程ID
    #  %(message)s: 打印日志信息
    # datefmt: 指定时间格式,同time.strftime()
    # level: 设置日志级别,默认为logging.WARNING
    # stream: 指定将日志的输出流,可以指定输出到sys.stderr,sys.stdout或者文件,默认输出到sys.stderr,当stream和filename同时指定时,stream被忽略
    logging.basicConfig(level=logging.DEBUG,
                    #[20][INFO][D:/data/python workspace/CheckURLtest/Test.py][Test.py][<module>][29][Tue, 12 Apr 2016 11:31:02][9904][MainThread][5144][This is info]
                    format='[%(levelno)s][%(levelname)s][%(pathname)s][%(filename)s][%(funcName)s][%(lineno)d][%(asctime)s]'
                           '[%(thread)d][%(threadName)s][%(process)d][%(message)s]',
                    datefmt='%a, %d %b %Y %H:%M:%S',
                    filename='myapp.log',
                    filemode='w')
    
    
    #################################################################################################
    #定义一个StreamHandler,将INFO级别或更高的日志信息打印到控制台,并将其添加到当前的日志处理对象#
    #日志级别大小关系为:CRITICAL > ERROR > WARNING > INFO > DEBUG > NOTSET,当然也可以自己定义日志级别。
    console = logging.StreamHandler()
    console.setLevel(logging.INFO)
    formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s')
    console.setFormatter(formatter)
    logging.getLogger('').addHandler(console)
    #################################################################################################
    
    logging.info("This is info")
    logging.debug("This is debug")
    logging.warn("This is warn")
    import logging
    import logging.config
    
    logging.config.fileConfig("logger.conf")
    logger = logging.getLogger("example01")
    
    logger.debug('This is debug message')
    logger.info('This is info message')
    logger.warning('This is warning message')

     原文链接:http://www.cnblogs.com/dkblog/archive/2011/08/26/2155018.html

  • 相关阅读:
    Apache 阿帕奇 配置运行环境
    2019年6月多校联训b层——搜索算法 Problem A 宽搜 营救
    西安集训B Day1 test 问题 C: 小明的城堡
    西安集训B层Day1 test 问题 A: 旅行日记
    二分答案—洛谷P1182 数列分段`Section II`
    2019.5.25 Noip模拟测试2 T2题解
    2019.5.25 Noip模拟测试2 T1题解
    DP专题练习 toasting
    2019.5.1 DP专题训练 山峰数(hill)
    React 点击按钮显示div与隐藏div
  • 原文地址:https://www.cnblogs.com/wangmingshun/p/5388786.html
Copyright © 2011-2022 走看看