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

Fel*_*ipe 6 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
Run Code Online (Sandbox Code Playgroud)

我的程序使用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()))),
        ...
Run Code Online (Sandbox Code Playgroud)

这适用于日志记录.这很好,因为我从属性文件继承了配置,每个子系统都有一个不同的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
Run Code Online (Sandbox Code Playgroud)

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

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

我错过了什么吗?谢谢.

Poco Library版本:1.5.1

Jam*_*esD 4

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

记录器 Core Core.Subsystem1 Core.Subsystem2

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

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

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

  • 要解决此问题,请为每个记录器提供单独的通道。使用“subsystem2Logger.setChannel(......);”和一个新通道进行设置。[日志演示](http://pocoproject.org/slides/110-Logging.pdf)中有示例。 (3认同)