【问题标题】:Problems with loggers hierarchy in Poco Logging FrameworkPoco Logging Framework 中记录器层次结构的问题
【发布时间】:2013-05-09 05:34:15
【问题描述】:

我在使用 Logging Framework 时遇到了一些问题。我有一个配置文件如下:

# core channel
logging.channels.c1.class = FileChannel
logging.channels.c1.path = <somePath>/core.log
logging.channels.c1.archive = timestamp
logging.channels.c1.times = utc
logging.channels.c1.rotation = daily
logging.channels.c1.formatter.class = PatternFormatter
logging.channels.c1.formatter.pattern = %Y-%m-%d %H:%M:%S %s: [%p] %t

# core logger
logging.loggers.l1.name = core
logging.loggers.l1.level = information
logging.loggers.l1.channel = c1

我的程序使用 Poco::Util:ServerApplication 框架,受益于子系统架构。我有多个子系统,每个子系统都存储一个对 Poco::Logger 对象的引用,该对象是通过使用 Poco::Logger::get("logger name") 方法获得的。我正在尝试使用日志层次结构,将“核心”日志(如上面的配置文件中所示)作为我的日志层次结构的根。以下代码举例说明了我在每个子系统中所做的事情:

Subsystem1::Subsystem1() :
   Poco::Util::Subsystem(),      
   logger_(Poco::Logger::get("core." + std::string(name()))),
        ...

Subsystem2::Subsystem2() :
   Poco::Util::Subsystem(),      
   logger_(Poco::Logger::get("core." + std::string(name()))),
        ...

这适用于日志记录。这很好,因为我从属性文件中继承了配置,并且每个子系统都有不同的 Poco::Message 源名称,从而可以轻松识别日志条目来自哪个子系统。

当我尝试更改记录器实例的属性时(例如,从 Subsystem1 的记录器),问题就出现了。例如,如果我更改它的通道路径,则更改会传播到整个层次结构。下面的代码演示了这个问题:

Poco::Logger& subsystem1Logger = Poco::Logger::get("core.Subsystem1");
Poco::Logger& subsystem2Logger = Poco::Logger::get("core.Subsystem2");
subsystem1Logger.getChannel()->close(); //without this, the change to the channel's   path does nothing
subsystem1Logger.getChannel()->setProperty("path", <someOtherPath>/core2.log); // Ok, it's changed
poco_information(subsystem1Logger "some message"); // Ok, it logs to  <someOtherPath>/core2.log
poco_information(subsystem2Logger "some message"); // NOT OK, it also logs to  <someOtherPath>/core2.log instead of  <somePath>/core.log

我很困惑,因为它在 Poco::Logger 类的头文件中声明“一旦创建了一个记录器并且它从其祖先那里继承了通道和级别,它就会失去与它的连接。因此,更改记录器的级别或通道不会影响其后代”。

顺便说一句,我的根记录器(核心)也受到更改的影响。

我错过了什么吗? 谢谢。

Poco 库版本:1.5.1

【问题讨论】:

    标签: c++ logging poco-libraries


    【解决方案1】:

    我认为您对记录器和通道感到困惑。

    记录器 核 核心子系统1 核心子系统2

    都附加到同一个通道 c1,因为它们在创建时是 Core 的副本。

    这是您通过 Logger.getChannel() 函数更改的通道 c1。

    如果记录器连接到不同的通道,那么您的方法将有效。

    【讨论】:

    • 要解决这个问题,给每个记录器一个单独的通道。使用带有新频道的subsystem2Logger.setChannel(......); 设置它。 Logging presentation中有例子。
    猜你喜欢
    • 1970-01-01
    • 2010-09-10
    • 2023-04-09
    • 2013-08-16
    • 1970-01-01
    • 2012-03-01
    • 2011-01-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多