Search code examples
logginglog4jlog4cplus

log4cplus - how to configure logger child level to depend on its parent


Hey I'm trying the log4cplus (which is similar to log4j and therefore my tags) library.

From the property file, is it possible to make a loggers level depend on its parents like this:

  • If parent=INFO & child=WARN , child should log messages >= "WARN"
  • If parent=OFF & child=WARN , child shouldn't log any messages.
  • If parent.parent=ALL & parent=WARN & child=INFO, child should log messages >=WARN
  • If parent.parent=OFF & parent=ALL & child=WARN , no one should log messages cause parent.parent.

With my code below the result is following (and wrong!)

  • parent=WARN & child=FATAL , child is logging WARN messages
  • parent=OFF & child=WARN , child is logging messages.

my property file - log.properties:

    log4cplus.logger.cpuLoad=WARN, FILEAPPENDER
    log4cplus.logger.cpuLoad.child=FATAL, FILEAPPENDER
    log4cplus.additivity.cpuLoad.child=false

    log4cplus.appender.FILEAPPENDER=log4cplus::RollingFileAppender
    log4cplus.appender.FILEAPPENDER.File=./cpuLoad.log
    log4cplus.appender.FILEAPPENDER.MaxFileSize=1MB
    log4cplus.appender.FILEAPPENDER.layout=log4cplus::PatternLayout
    log4cplus.appender.FILEAPPENDER.layout.ConversionPattern=%d{%m/%d/%y %H:%M:%S} %-5p %c{2} – %m%n

My c++ code - main.cpp:

    Logger loggerCpu = Logger::getInstance("cpuLoad");
    Logger loggerCpuChild = Logger::getInstance("cpuLoad.child");

    LOG4CPLUS_WARN(loggerCpu, "hello from loggerCpu");
    LOG4CPLUS_WARN(loggerCpuChild, "hello from loggerCpuChild" );

I want this to work from the log4cplus library itself, and mostly from the property file. I also need this parent/child relation so I can use getParent(). Thanks in advance!


Solution

  • Try to change this line

    log4cplus.logger.cpuLoad.child=FATAL, FILEAPPENDER
    

    into this

    log4cplus.logger.cpuLoad.child=INHERITED, FILEAPPENDER
    

    That should do the trick.

    EDIT:

    @jaguzu: I do not know log4j that well but it seems to me that what you are trying to achieve is outside the model that both log4j and log4cplus use: Either you set your log level explicitly, or you inherit it from parent logger. You can implement more filtering using logger thresholds and appender filters, but neither of the two are inherited from parent loggers, AFAIK.