logging.basicConfig(
filename=f"{output_location}/log.txt",
format="{asctime} - {levelname} - {filename}: {message}",
datefmt = "%d %b %H:%M",
style="{",
level=logging.INFO
)
这是我的代码,它曾经工作得很好,但在我更改了一行代码后,它停止工作。停止工作之前的结果:
现在日志看起来像这样: WARNING:root 当格式参数另有说明时。此外,不会创建 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
。我不完全确定发生了什么,但它突然停止工作了。如有任何帮助,我们将不胜感激,谢谢。
原来答案就在this帖子中。我也在这里被警告过。
注意:仅当之前未配置根记录器时,调用 basicConfig() 来配置根记录器才有效。如果从未调用过 basicConfig(),则所有日志记录函数都会自动调用不带参数的 basicConfig()。因此,例如,一旦您调用logging.debug(),您将无法再使用basicConfig()配置根记录器。
来自 RealPython。原来我真的太粗心了