使用 boost::log 配置文件变量

Using boost::log configuration file variables

我在应用程序中使用 Boost 1.63.0 日志库。为了初始化各种日志文件,我使用 boost::log::init_from_stream(...) 方法从配置 INI 文件配置接收器。

我最初对 boost 日志的文件轮换行为有疑问。该文件是在当前工作目录中创建的,然后在文件轮换触发器或应用程序关闭时移动到 "Target" 目录。期望的行为是直接在 "Target" 目录中创建文件。我在 post 的帮助下更正了这个问题:boost::log add_file_log not writing if app exits with CTRL_CLOSE_EVENT

简而言之,它说要为 "FileName" 关键字提供完整路径信息,而不仅仅是名称。这行得通,但我想知道是否有 "clean up" 配置文件的好方法。

例如,INI 文件当前要求您指定 "Target" 和 "FileName" 关键字,如下所示:

Target="C:\ProgramData\MyApplication\Logs"
FileName="C:\ProgramData\MyApplication\Logs\AppLog_%N.log"

如果能够只指定 "Target" 目录,然后在 "FileName" 变量定义中使用该变量,那就太好了。只需更改一件事,这将使任何未来的更改变得更加简单。类似于:

#Define Target Directory for file rotation
Target="C:\ProgramData\MyApplication\Logs"

#Define File Name pattern
FileName="%Target%\AppLog_%N.log"
     ---  OR ---
FileName="$(Target)\AppLog_%N.log"

这可以吗?

Boost.Log 不执行变量扩展,你必须自己做。您可以将设置文件加载到 settings by calling parse_settings. Then you can operate on that container and expand the variables. When you're done, you can use it to initialize the logging library by calling init_from_settings.