How to make a logger that avoids the root logger in Fuse 7

Solution In Progress - Updated -

Environment

  • Fuse 7.x

Issue

  • A Camel application logger, defined on the logging configuration, is taking a lot of time on CPU processing
  • As advised, I have to define a logger that skips any invocation to the root logger

Resolution

A new logger should be defined on the logging configuration,

  • The new logger should not be defined from the root logger, it may be defined independently
  • On the new logger definition, set the attribute additivity = false, this is for not inheriting properties from root logger

Following a example, logging configuration file org.ops4j.pax.logging.cfg and code examples are attached

#New logger appended
log4j2.appender.mylogger.name = MyLogger
log4j2.appender.mylogger.type = RollingRandomAccessFile
log4j2.appender.mylogger.fileName = ${karaf.data}/log/example.log
log4j2.appender.mylogger.filePattern = ${karaf.data}/log/example.log.%i
log4j2.appender.mylogger.append = true
log4j2.appender.mylogger.layout.type = PatternLayout
log4j2.appender.mylogger.layout.pattern = ${log4j2.pattern}
log4j2.appender.mylogger.policies.type = Policies
log4j2.appender.mylogger.policies.size.type = SizeBasedTriggeringPolicy
log4j2.appender.mylogger.policies.size.size = 16MB
log4j2.appender.mylogger.strategy.type = DefaultRolloverStrategy
log4j2.appender.mylogger.strategy.max = 20


#New logger definition
log4j2.logger.app.name = com.redhat.example
log4j2.logger.app.level = info
log4j2.logger.app.additivity = false
log4j2.logger.app.appenderRef.MyLogger.ref = MyLogger
  • On the camel route: (MyRouteBuilder): For the .log() eip to work with the new logger, you must set the following, before setting up the routes:
    @Override
    public void configure() throws Exception {

        //set MyLogger as global logger, for camel ".log()"
        this.getContext().getGlobalOptions().put(Exchange.LOG_EIP_NAME, "com.redhat.example");
  • For the class itself, as this is has the package com.redhat.example, it automatically takes this log for the class logging outside the camel route.
    This also applies to the processor MyProcessor, on the same package
public class MyRouteBuilder extends RouteBuilder {

    //this matches to "com.redhat.example" logger (MyLogger)
    private static final Logger logger = LogManager.getLogger(MyRouteBuilder.class);
  • How do I know this is not using the root logger?
  1. You can change root logger to DEBUG, and check that the example logger only INFO statements are logged to the file example.log

  2. Root logger uses console; example logger only uses example.log file (this is only for example purposes, this could change)

Attachments

This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.

Comments