在使用日志框架时,我遇到了一些问题。我有以下配置文件:
# 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
subsystem2Logger.setChannel(......);
。Logging presentation中有示例。 - M.M