python logging:如何确保创建日志文件目录? [英] python logging: how to ensure logfile directory is created?
问题描述
我想在我的应用程序中使用python的日志记录框架,并且我希望允许我的应用程序的最终用户指定日志文件. (通过Python日志记录框架的配置机制(在我的情况下是其中的一部分)最终用户可以编辑以指定日志记录行为的YAML文件).
I would like to use python's logging framework in my application, and I'd like to allow the end user of my application to specify the log file. (Via the Python logging framework's configuration mechanisms which in my case is a section of a YAML file that the end user can edit to specify how logging behaves.)
是否可以通过创建日志来获取日志框架以确保目录存在?因为日志文件名的确切路径嵌入在最终用户指定的配置信息中,因此对于我来说,作为应用程序编写者来解析此信息以确定应创建哪个目录对我来说并不难.
Is there a way to get the logging framework to ensure that a directory exists by creating it? Because the exact path to the logging filename is embedded in the configuration information specified by the end user, it is nontrivial for me as the application writer to parse this information to determine which directory should be created.
如果最终用户指定"foo/bar/baz.log",我想确保已创建foo/bar目录.
If the end user specifies "foo/bar/baz.log", I would like to make sure that the foo/bar directory is created.