zoukankan      html  css  js  c++  java
  • logging的基本使用

    logging模块打印log的时候主要有一下几个,级别顺序:CRITICAL>ERROR>WARNING>INFO>DEBUG;

    1.日志输出到file:

    import logging
    logger = logging.getLogger(__name__)
    logger.setLevel(level = logging.INFO)
    handler = logging.FileHandler("log.txt")
    handler.setLevel(logging.INFO)
    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    handler.setFormatter(formatter)
    logger.addHandler(handler)
    
    logger.info("Start print log")
    logger.debug("Do something")
    logger.warning("Something maybe fail.")
    logger.info("Finish")
    

      打印的log生成在代码目录下:

    2018-09-12 15:43:25,516 - __main__ - INFO - Start print log
    2018-09-12 15:43:25,516 - __main__ - WARNING - Something maybe fail.
    2018-09-12 15:43:25,516 - __main__ - INFO - Finish
    

      如果要同时兼顾到输出到控制台和file:

    import logging
    
    logger = logging.getLogger(__name__)
    logger.setLevel(level = logging.INFO)
    
    handler = logging.FileHandler("log.txt")
    handler.setLevel(logging.INFO)
    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    handler.setFormatter(formatter)
    
    console = logging.StreamHandler()
    console.setLevel(logging.INFO)
    console.setFormatter(formatter)
    
    logger.addHandler(handler)
    logger.addHandler(console)
    
    logger.info("Start print log")
    logger.debug("Do something")
    logger.warning("Something maybe fail.")
    logger.info("Finish")
    

     同样在log文件和控制台输出:

    2018-09-12 16:02:52,266 - __main__ - INFO - Start print log
    2018-09-12 16:02:52,290 - __main__ - WARNING - Something maybe fail.
    2018-09-12 16:02:52,299 - __main__ - INFO - Finish
    

     

    封装成类

    # -*- coding:utf-8 -*-
    import logging
    log_name = "auto_updated.log"
    
    class Logger:
        def __init__(self, path,clevel = logging.DEBUG,Flevel = logging.DEBUG):
            self.logger = logging.getLogger(path)
            self.logger.setLevel(logging.DEBUG)
            fmt = logging.Formatter('[%(asctime)s] [%(levelname)s] %(message)s', '%Y-%m-%d %H:%M:%S')
            #set CMD log
            sh = logging.StreamHandler()
            sh.setFormatter(fmt)
            sh.setLevel(clevel)
            #set log file
            fh = logging.FileHandler(path)
            fh.setFormatter(fmt)
            fh.setLevel(Flevel)
            self.logger.addHandler(sh)
            self.logger.addHandler(fh)
    
        def debug(self,message):
            self.logger.debug(message)
    
        def info(self,message):
            self.logger.info(message)
    
        def war(self,message):
            self.logger.warn(message)
    
        def error(self,message):
            self.logger.error(message)
    
        def cri(self,message):
            self.logger.critical(message)
    
    
    
    logger = Logger(log_name, logging.DEBUG, logging.DEBUG)
    logger.cri("this is critical log")

     

  • 相关阅读:
    二分图匹配初步
    动态规划初步
    一些排日程的经典方法
    petri网初步
    笔记:美国院士教你写论文
    Ubuntu18.04彻底删除MySQL数据库
    ubuntu18.04 安装 wps2019
    ubuntu18.04 阿里镜像源
    Ubuntu 18.04 使用标准Ubuntu 仓库进行自动化安装NVIDIA驱动
    linux maven环境变量配置
  • 原文地址:https://www.cnblogs.com/nevermore29/p/9635379.html
Copyright © 2011-2022 走看看