Unable to use splitterChannel with advanced xml configuration with poco
-
26-12-2019 - |
Question
I would like to configure an advance logger using poco and its configuration file. I create a config.xml file like that :
<?xml version="1.0" ?>
<Application>
<logging>
<channels>
<c1>
<class>ColorConsoleChannel</class>
<formatter>
<class>PatternFormatter</class>
<pattern>%Y-%m-%d %H:%M:%S : %s : [%p] : %t</pattern>
</formatter>
<traceColor>lightBlue</traceColor>
<debugColor>blue</debugColor>
<informationColor>green</informationColor>
<noticeColor>green</noticeColor>
<warningColor>yellow</warningColor>
<errorColor>red</errorColor>
<criticalColor>lightMagenta</criticalColor>
<fatalColor>lightMagenta</fatalColor>
</c1>
<c2>
<class>FileChannel</class>
<path>logs/traceApplication.log</path>
<rotation>1 M</rotation>
<archive>number</archive>
<purgeCount>5</purgeCount>
<formatter>
<class>PatternFormatter</class>
<pattern>%Y-%m-%d %H:%M:%S : %T : [%p] : %t</pattern>
</formatter>
</c2>
</channels>
<loggers>
<consoleLogger>
<channel>c1</channel>
<level>information</level>
</consoleLogger>
<traceFileLogger>
<channel>c2</channel>
<level>trace</level>
</traceFileLogger>
</loggers>
<channels>
<cSplitter>
<class>SplitterChannel</class>
<channels>consoleLogger,traceFileLogger,mainFileLogger</channels>
</cSplitter>
</channels>
<loggers>
<root>
<channel>cSplitter</channel>
<level>trace</level>
</root>
</loggers>
</logging>
</Application>
I use a Poco::Util::ServerApplication class and in the initialize method I put :
void CBS2AudioVideo::initialize(Poco::Util::Application& self)
{
loadConfiguration("config.xml");
Poco::Util::ServerApplication::initialize(self);
}
Before adding the splitterChannel my logging works well but with it, it doesn't any more.
I got the error message :
Not found: logging channel: consoleLogger
My goal is to have only one root logger and when I use it, it log in information level into the console and in trace level into the file.
When I set channels in channels.cSplitter.channels it works but all channels are logged to the same level. And if I take the logging configuration slide (http://pocoproject.org/slides/185-LoggingConfiguration.pdf) they use loggers and not channels in the logging.channels.splitter.channels attribute area. So I think it is possible. More over the Logger class inherit from Channel too.
Someone has already done this kind of work or have an idea ?
Solution
I've finally found the solution. I post it here if it interest someone.
This file works well.
<?xml version="1.0" ?>
<Application>
<logging>
<channels>
<cScreen>
<class>ColorConsoleChannel</class>
<formatter>
<class>PatternFormatter</class>
<pattern>%H:%M:%S : %T : [%p] : %t</pattern>
</formatter>
<traceColor>lightBlue</traceColor>
<debugColor>blue</debugColor>
<informationColor>white</informationColor>
<noticeColor>green</noticeColor>
<warningColor>yellow</warningColor>
<errorColor>red</errorColor>
<criticalColor>lightMagenta</criticalColor>
<fatalColor>lightMagenta</fatalColor>
</cScreen>
<cFile>
<class>FileChannel</class>
<path>logs/application.log</path>
<rotation>1 M</rotation>
<archive>number</archive>
<purgeCount>5</purgeCount>
<formatter>
<class>PatternFormatter</class>
<pattern>%H:%M:%S : %T : [%p] : %t</pattern>
</formatter>
</cFile>
</channels>
<loggers>
<root>
<name></name>
<channel>cFile</channel>
<level>trace</level>
</root>
<main>
<name>main</name>
<channel>cScreen</channel>
<level>trace</level>
</main>
</loggers>
</logging>
To log on the screen and in the file make :
Poco::Logger::get("main").trace(msg);
To log only into file make
Poco::Logger::get("").trace(msg);