Search code examples
loggingpoco-libraries

Using Poco library, how to set different log levels per channel in a splitter channel logger?


What I have now:

  • Windows Event channel
  • Simple File channel
  • Splitter channel to route to both channels
  • Root logger set with PRIO_INFORMATION level

.

//Windows Event Log
Poco::EventLogChannel* elChannel = new Poco::EventLogChannel("App");

//Simple file Log
Poco::SimpleFileChannel* sfChannel = new Poco::SimpleFileChannel();
sfChannel->setProperty("path", "log.txt");
sfChannel->setProperty("rotation", "10 M");

//Splitter Channel 
Poco::SplitterChannel* sChannel = new Poco::SplitterChannel();
sChannel->addChannel(sfChannel);
sChannel->addChannel(elChannel);

logger().root().setChannel(sChannel);
logger().root().setLevel(Poco::Message::PRIO_INFORMATION);

I would like to have different log levels per channel in the splitter:

  • Windows Event channel level: WARNING
  • File channel level: INFORMATION

This way only messages above WARNING would go to Windows Event Viewer.

Could this be achieved in some way using standard Poco::Logger?


Solution

  • Logging level is per Logger, not per Channel, so you will have to have two loggers. See Logger example. To avoid inconvenience of having to log the same thing twice, you can write your own "splitter" function that wraps the loggers and logs the same messages to both.