我在使用记录框架时遇到了一些问题。我有一个配置文件如下:Poco 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
我的程序使用波科::的Util:ServerApplication框架,从子系统架构中获益。我有多个子系统,每个子系统都存储对使用Poco :: Logger :: get(“logger name”)方法获得的Poco :: Logger对象的引用。我试图使用日志层次结构,将上面配置文件中显示的“核心”日志作为我的日志记录层次结构的根目录。下面的代码举例说明了我在每个系统中所做的工作:
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
我很困惑,因为它是在波苏:: Logger类的头文件中指出,“一旦记录器已创建并继承了通道和水平从它的祖先开始,它就失去了与它的联系,所以记录器级别或通道的改变不会影响它的后代“。
顺便说一句,我的根记录器(核心)也受到变化的影响。
我错过了什么吗? 谢谢。
波科库版本:1.5.1
要解决这个问题,给每个记录器一个单独的通道。使用'subsystem2Logger.setChannel(......);'设置一个新频道。 [记录介绍]中有例子(http://pocoproject.org/slides/110-Logging.pdf)。 –