【问题标题】:Problem with python logging to another file/directorypython记录到另一个文件/目录的问题
【发布时间】:2019-11-25 07:26:42
【问题描述】:

我正在尝试编写一个接口,我想登录到另一个文件或另一个目录中的文件不同的东西。我正在使用日志记录模块,由于某种原因它根本没有记录任何内容

我尝试将与程序位于同一目录中的文件恢复为 "filename = example.log"

蟒蛇

import logging
logging.basicConfig(filename= 'home/notcwd/example.log', 
                    format= '%(asctime)s %(levelname)s : %(message)s')

def example():
    logging.info('it works')
    return 

我希望它记录到 example.log 但它根本不记录任何东西

【问题讨论】:

    标签: python-3.x logging module


    【解决方案1】:

    所以默认情况下,只会记录 WARNING 日志。

    如果您可以尝试logging.warn() 而不是logging.info(),您可以在日志中看到该条目。

    不过,您也可以使用logging.basicConfig(level=logging.DEBUG)修改关卡。

    代码:

    import logging
    logging.basicConfig(level=logging.DEBUG,
                        filename= 'home/notcwd/example.log', 
                        format= '%(asctime)s %(levelname)s : %(message)s')
    
    def example():
        logging.info('it works')
        return
    
    example()
    

    【讨论】:

      猜你喜欢
      • 2021-01-22
      • 2020-09-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-25
      • 2017-07-20
      • 2012-02-15
      • 1970-01-01
      相关资源
      最近更新 更多