• python logging模块用法


    http://blog.csdn.net/zyz511919766/article/details/25136485/

    import logging  
    logging.debug('debug message')  
    logging.info('info message')  
    logging.warning('warning message')  
    logging.error('error message')  
    logging.critical('critical message')  

    这种,直接将log信息打开在屏幕上,而且只打印 warning及以上的信息,

    日志级别等级CRITICAL > ERROR > WARNING > INFO > DEBUG > NOTSET

    import logging  
    
    logging.basicConfig(level=logging.DEBUG,  
                        format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',  
                        datefmt='%a, %d %b %Y %H:%M:%S',  
                        filename='E:/tmp/test.log',  
                        filemode='w')  
    logging.debug('debug message')  
    logging.info('info message')  
    logging.warning('warning message')  
    logging.error('error message')  
    logging.critical('critical message')  

    这种 ,将日志输出到test.log中,

    另外 还可以创建一个logging的实例 logger

    #coding:utf-8  
    import logging  
      
    # 创建一个logger    
    logger = logging.getLogger()  
      
    logger1 = logging.getLogger('mylogger')  
    logger1.setLevel(logging.DEBUG)  
      
    logger2 = logging.getLogger('mylogger')  
    logger2.setLevel(logging.INFO)  
      
    logger3 = logging.getLogger('mylogger.child1')  
    logger3.setLevel(logging.WARNING)  
      
    logger4 = logging.getLogger('mylogger.child1.child2')  
    logger4.setLevel(logging.DEBUG)  
      
    logger5 = logging.getLogger('mylogger.child1.child2.child3')  
    logger5.setLevel(logging.DEBUG)  
      
    # 创建一个handler,用于写入日志文件    
    fh = logging.FileHandler('/tmp/test.log')  
      
    # 再创建一个handler,用于输出到控制台    
    ch = logging.StreamHandler()  
      
    # 定义handler的输出格式formatter    
    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')  
    fh.setFormatter(formatter)  
    ch.setFormatter(formatter)  
      
    #定义一个filter  
    #filter = logging.Filter('mylogger.child1.child2')  
    #fh.addFilter(filter)    
      
    # 给logger添加handler    
    #logger.addFilter(filter)  
    logger.addHandler(fh)  
    logger.addHandler(ch)  
      
    #logger1.addFilter(filter)  
    logger1.addHandler(fh)  
    logger1.addHandler(ch)  
      
    logger2.addHandler(fh)  
    logger2.addHandler(ch)  
      
    #logger3.addFilter(filter)  
    logger3.addHandler(fh)  
    logger3.addHandler(ch)  
      
    #logger4.addFilter(filter)  
    logger4.addHandler(fh)  
    logger4.addHandler(ch)  
      
    logger5.addHandler(fh)  
    logger5.addHandler(ch)  
      
    # 记录一条日志    
    logger.debug('logger debug message')  
    logger.info('logger info message')  
    logger.warning('logger warning message')  
    logger.error('logger error message')  
    logger.critical('logger critical message')  
      
    logger1.debug('logger1 debug message')  
    logger1.info('logger1 info message')  
    logger1.warning('logger1 warning message')  
    logger1.error('logger1 error message')  
    logger1.critical('logger1 critical message')  
      
    logger2.debug('logger2 debug message')  
    logger2.info('logger2 info message')  
    logger2.warning('logger2 warning message')  
    logger2.error('logger2 error message')  
    logger2.critical('logger2 critical message')  
      
    logger3.debug('logger3 debug message')  
    logger3.info('logger3 info message')  
    logger3.warning('logger3 warning message')  
    logger3.error('logger3 error message')  
    logger3.critical('logger3 critical message')  
      
    logger4.debug('logger4 debug message')  
    logger4.info('logger4 info message')  
    logger4.warning('logger4 warning message')  
    logger4.error('logger4 error message')  
    logger4.critical('logger4 critical message')  
      
    logger5.debug('logger5 debug message')  
    logger5.info('logger5 info message')  
    logger5.warning('logger5 warning message')  
    logger5.error('logger5 error message')  
    logger5.critical('logger5 critical message')  

    并绑定两个事件,分别输出的控制台和文件内

  • 相关阅读:
    了解下Mysql的间隙锁及产生的原因
    Mysql在InnoDB引擎下索引失效行级锁变表锁案例
    Mysql优化_慢查询开启说明及Mysql慢查询分析工具mysqldumpslow用法讲解
    Mysql优化_ORDER BY和GROUP BY 的优化讲解(单路排序和双路排序)
    Mysql优化原则_小表驱动大表IN和EXISTS的合理利用
    Mysql性能调优工具Explain结合语句讲解
    隐藏Apche、Nginx、PHP的版本号提高网站安全性
    Thinkphp在Lnmp环境下部署项目先后报错问题解决:_STORAGE_WRITE_ERROR_:./Application/Runtime/Cache/Home/...Access denied.
    html中返回上一页
    ecshop网站建设手机版wap版出现lib.debug.php on line 303
  • 原文地址:https://www.cnblogs.com/xqnq2007/p/8288766.html
Copyright © 2020-2023  润新知