zoukankan      html  css  js  c++  java
  • python

    为什么要打印日志?

    代码需要经历开发、调试、审查、测试或者上线等不同阶段,在开发时想要打印的信息类型可能和上线后想看到的信息类型完全不同,
    通过打印的信息可以快速查找到代码哪里出了问题。

    使用logging日志模块,可以打印出使用的模块以及代码运行的时间。


    import logging, time, os
    
    BASE_PATH = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
    # 定义日志文件路径
    LOG_PATH = os.path.join(BASE_PATH, "log")
    if not os.path.exists(LOG_PATH):
        os.mkdir(LOG_PATH)
    
    
    class Logger():
    
        def __init__(self):
            self.logname = os.path.join(LOG_PATH, "{}.log".format(time.strftime("%Y%m%d")))
            self.logger = logging.getLogger("log")
            self.logger.setLevel(logging.DEBUG)
    
            self.formater = logging.Formatter(
                '[%(asctime)s][%(filename)s %(lineno)d][%(levelname)s]: %(message)s')
    
            self.filelogger = logging.FileHandler(self.logname, mode='a', encoding="UTF-8")
            self.console = logging.StreamHandler()
            self.console.setLevel(logging.DEBUG)
            self.filelogger.setLevel(logging.DEBUG)
            self.filelogger.setFormatter(self.formater)
            self.console.setFormatter(self.formater)
            self.logger.addHandler(self.filelogger)
            self.logger.addHandler(self.console)
    
    
    logger = Logger().logger
    
    if __name__ == '__main__':
        logger.info("---测试开始---")
        logger.debug("---测试结束---")
    
    
  • 相关阅读:
    每天一个Linux命令(3): cd
    每天一个Linux命令(2): ls
    scala学习笔记(2)
    jmeter性能测试 套路二
    selenium实战2 登陆博客园
    jmeter响应断言
    Python验证码通过pytesser识别
    selenium实战学习第一课
    appium的webdriver执行swipe
    APPIUM 输入中文 之套路
  • 原文地址:https://www.cnblogs.com/wwho/p/15505400.html
Copyright © 2011-2022 走看看