Poco Logging Framework中记录器层次结构的问题

时间:2013-05-14 19:45:46

标签: c++ logging poco-libraries

使用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”)方法获得的。我正在尝试使用日志层次结构,具有“核心”日志,如上面的配置文件中所示,作为我的日志记录层次结构的根。以下代码举例说明了我在每个susbsystem中所做的事情:

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 Library版本:1.5.1

1 个答案:

答案 0 :(得分:4)

我认为你在记录器和频道之间感到困惑。

记录器 核心 Core.Subsystem1 Core.Subsystem2

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

您正在通过Logger.getChannel()函数更改通道c1。

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

相关问题