日志配置不生效

问题描述 投票:0回答:1
    logging.basicConfig(
        filename=f"{output_location}/log.txt",
        format="{asctime} - {levelname} - {filename}: {message}",
        datefmt = "%d %b %H:%M",
        style="{",
        level=logging.INFO
    )

这是我的代码,它曾经工作得很好,但在我更改了一行代码后,它停止工作。停止工作之前的结果:

  1. 具有格式参数指定的更新的日志记录格式。
  2. 创建包含日志的文件
    log.txt

现在日志看起来像这样:

WARNING:root
format
参数另有说明时。此外,未创建
log.txt
文件。

我更改的代码行是

output_location = os.getcwd() + r"/../output"
,它从
r"/output"
更改为
r"/../output"
。这行代码就在这个函数中:

def create_output_dir() -> Path:
    """Creates the output directory where all the log files and user facing sheets will go."""
    output_location = os.getcwd() + r"/output"
    path = Path(output_location)
    if not path.exists():
        msg = "A folder named 'output' has been created. All files generated will go there."
        print(msg)
        logging.info(msg)
        path.mkdir()

    return path

在我恢复代码以删除

/..
部分后,日志记录格式保留为默认值
WARNING:root
。我不完全确定发生了什么,但它突然停止工作了。

python logging python-logging
1个回答
0
投票

原来答案就在this帖子中。我也在这里被警告过。

注意:仅当之前未配置根记录器时,调用 basicConfig() 来配置根记录器才有效。如果从未调用过 basicConfig(),则所有日志记录函数都会自动调用不带参数的 basicConfig()。因此,例如,一旦您调用logging.debug(),您将无法再使用basicConfig()配置根记录器。

来自 RealPython。原来我真的太粗心了

© www.soinside.com 2019 - 2024. All rights reserved.