【问题标题】:How to use environment variable in Poco:FileChannel "Path" property如何在 Poco:FileChannel "Path" 属性中使用环境变量
【发布时间】:2019-08-01 06:06:30
【问题描述】:

我使用 Poco::LoggingConfigurator 库进行日志记录,使用 Poco::XMLConfiguration 配置日志记录属性。我想在日志记录配置 xml 文件的路径值中使用环境变量。如何在xml配置文件中做到这一点?

这是我现在使用的xml配置文件。

<logging>
<channels>
  <logFileChannel>
    <class>FileChannel</class>
    <path>/logs/agent-xfs.log</path>
    <rotation>1 M</rotation>
    <archive>timestamp</archive>
    <compress>true</compress>
    <purgeCount>60</purgeCount>
  </logFileChannel>
</channels>

<loggers>
  <root>
    <channel>logFileChannel</channel>
    <level>debug</level>
  </root>
</loggers>

我想使用如下环境变量定义路径变量值,

${ENV_SAMPLE_VARAIBLE}/logs/agent-xfs.log

【问题讨论】:

    标签: poco-libraries


    【解决方案1】:

    您可以使用${system.env.NAME}引用环境变量,例如:

    <path>${system.env.LOGPATH}/agent-xfs.log</path>
    

    为了解决${system.env.LOGPATH},您需要有一个包含Poco::Util::SystemConfiguration 和Poco::Util::XMLConfiguration 的Poco::Util::LayeredConfiguration:

    Poco::AutoPtr<Poco::Util::LayeredConfiguration> pConfig = new Poco::Util::LayeredConfiguration;
    pConfig->add(new Poco::Util::SystemConfiguration, 100, false, false);
    pConfig->add(new Poco::Util::XMLConfiguration(configFilePath.toString()), 0, false, false);
    
    LoggingConfigurator loggingConfigurator; loggingConfigurator.configure(pConfig);
    

    请注意,如果您使用Poco::Util::Application 来处理配置,它会自动执行此操作。

    【讨论】:

    • 您好 Günter Obiltschnig 感谢您的回复。我尝试了您的建议,但没有创建日志。如果我给出完整路径而不是引用环境变量,那么它可以正常工作。我加载xml文件并配置如下, AutoPtr configuration = new XMLConfiguration(configFilePath.toString());记录配置器loggingConfigurator.configure(配置);
    • 你需要创建一个 Poco::Util::LayeredConfiguration 包含你的Poco::Util::XMLConfiguration 和一个Poco::Util::SystemConfiguration,这样${system.env.NAME} 才能真正被解析。我已经更新了我的回复以说明如何。
    • 感谢 Günter Obiltschnig 的帮助。在尝试了您的建议后,它现在可以工作了。谢谢。
    猜你喜欢
    • 2021-02-09
    • 1970-01-01
    • 2015-01-21
    • 2019-06-30
    • 1970-01-01
    • 1970-01-01
    • 2019-05-15
    • 2021-05-06
    • 2015-09-29
    相关资源
    最近更新 更多