zoukankan      html  css  js  c++  java
  • 日志输出:控制台和log文件输出日志

    self_log.py 中

    import os
    import logging
    import time
    
    # 如果日志文件夹不存在,则创建
    log_dir = "log"  # 日志存放文件夹名称
    log_path = os.getcwd() + os.sep + log_dir
    if not os.path.isdir(log_path):
        os.makedirs(log_path)
    
    # 设置logging
    logger = logging.getLogger()
    logger.setLevel(logging.DEBUG)
    main_log_handler = logging.FileHandler(
        log_dir + "/email_main_%s.log" % time.strftime("%Y-%m-%d_%H-%M-%S", time.localtime(time.time())), mode="w+",
        encoding="utf-8")
    main_log_handler.setLevel(logging.DEBUG)
    formatter = logging.Formatter("%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s")
    main_log_handler.setFormatter(formatter)
    logger.addHandler(main_log_handler)
    
    # 控制台打印输出日志
    console = logging.StreamHandler()  # 定义一个StreamHandler,将INFO级别或更高的日志信息打印到标准错误,并将其添加到当前的日志处理对象
    console.setLevel(logging.INFO)  # 设置要打印日志的等级,低于这一等级,不会打印
    formatter = logging.Formatter("%(asctime)s - %(levelname)s: %(message)s")
    console.setFormatter(formatter)
    logging.getLogger('').addHandler(console)
    

      

    其他文件要使用时,在其他文件中导入:如

    import self_log
    from self_log import logger
    

      

  • 相关阅读:
    java,控制台输入输出,switch等值比较
    关于utf-8(网上查阅)
    Java,基础语法(网上查阅)
    Java,背景,组成
    《DSP using MATLAB》Problem5.16
    《DSP using MATLAB》Problem 5.15
    《DSP using MATLAB》Problem 5.14
    《DSP using MATLAB》Problem 5.13
    《DSP using MATLAB》Problem 5.12
    《DSP using MATLAB》Problem 5.11
  • 原文地址:https://www.cnblogs.com/andy9468/p/9676782.html
Copyright © 2011-2022 走看看