Python 在运行时更改日志文件位置

Luk*_*uke 3 python logging

我有一个logging.config 文件,它指定将日志输出到控制台和文件的处理程序。我将如何在运行时更改文件处理程序的文件位置但保留所有格式等。

Luk*_*uke 5

以下是我如何找到如何做到这一点的方法。[handler_file]在输出文件中添加以下行:

args=('%(logfilename)s',)
Run Code Online (Sandbox Code Playgroud)

然后在初始化记录器时指定日志文件位置,如下所示:

logging.config.fileConfig('logging.config',
                          defaults={'logfilename': '/path/to/log/file'})
Run Code Online (Sandbox Code Playgroud)