Poco Logging Framework的日志记录器层次存在问题

6

在使用日志框架时,我遇到了一些问题。我有以下配置文件:

# 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")方法获得。我正在尝试使用日志层次结构,将上面配置文件中的“core”日志作为我的日志层次结构的根。以下代码说明我在每个子系统中所做的事情:

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类的头文件中,它声明“一旦创建了一个记录器并且继承了其祖先的通道和级别,它就会失去与其祖先的连接。所以对记录器级别或通道的更改不会影响其后代”。

顺便说一下,我的根记录器(core)也会受到这种更改的影响。

我错过了什么吗? 谢谢。

Poco库版本:1.5.1

1个回答

4

我认为您对Logger和Channel的概念有些混淆。

这些Loggers:

Core

Core.Subsystem1

Core.Subsystem2

它们都附属于同一个channel c1,因为它们在创建时是Core的一个副本。

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

如果这些loggers附属于不同的channels,那么您的方法就可以奏效。


3
为了解决这个问题,给每个记录器分配一个单独的通道。使用新通道设置它,例如:subsystem2Logger.setChannel(......);Logging presentation中有示例。 - M.M

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接