log4j2 isThreadContextMapInheritable property usage
Asked Answered
A

2

5

I am trying to log events of a Java application to separate log files based on a key set to the ThreadContext. But my key is not reaching the child thread (created on MouseEvent) even after setting "log4j2.isThreadContextMapInheritable" to "true" in system properties. Someone please help me to get this resolved.

My main method:

public class Application {
static {
    System.setProperty("log4j2.isThreadContextMapInheritable","true");
}

private final static Logger LOGGER = LogManager.getLogger(Application.class);

public static void main(String[] args) throws Exception
{
    ThreadContext.put("cfg","RLS");
    LOGGER.info("New window opening!!!"+ThreadContext.get("cfg"));
    newWindow();
}

private static void newWindow() throws Exception {
    ButtonFrame buttonFrame = new ButtonFrame("Button Demo");
    buttonFrame.setSize( 350, 275 );     
    buttonFrame.setVisible( true );
}

}

ButtonFrame class:

public class ButtonFrame extends JFrame{
private final static Logger LOGGER = LogManager.getLogger(NewWindow.class);
JButton bChange;
JFrame frame = new JFrame("Our JButton listener example");

  public ButtonFrame(String title)
  {
    super( title );
    setLayout( new FlowLayout() );
    bChange = new JButton("Click Me!");
      bChange.addMouseListener(new MouseListener() {
          @Override
          public void mouseClicked(MouseEvent e) {
              try {
                  LOGGER.info("Mouse clicked!!!"+ThreadContext.get("cfg"));
                  JDialog d = new JDialog(frame, "HI", true);
                  d.setLocationRelativeTo(frame);
                  d.setVisible(true);
              } catch (Exception e1) {
                  e1.printStackTrace();
              }
          }

          @Override
          public void mousePressed(MouseEvent e) {}

          @Override
          public void mouseReleased(MouseEvent e) {}

          @Override
          public void mouseEntered(MouseEvent e) {}

          @Override
          public void mouseExited(MouseEvent e) {}
      });
    add( bChange );                    
    setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );   
  }

}

log4j2.properties file:

appenders = rls,otr,routing

appender.rls.type = RollingFile
appender.rls.name = RollingFile_Rls
appender.rls.fileName = D:\\RLS\\rls_%d{MMdd}.log
appender.rls.filePattern = D:\\RLS\\rls_%d{MMdd}.log
appender.rls.layout.type = PatternLayout
appender.rls.layout.pattern = %d{ABSOLUTE} %level{length=1} 
%markerSimpleName [%C{1}:%L] %m%n
appender.rls.policies.type = Policies
appender.rls.policies.time.type = TimeBasedTriggeringPolicy
appender.rls.policies.time.interval = 1
appender.rls.policies.time.modulate = true
appender.rls.policies.size.type = SizeBasedTriggeringPolicy
appender.rls.policies.size.size = 100MB
appender.rls.strategy.type = DefaultRolloverStrategy
appender.rls.strategy.max = 5
appender.otr.type = RollingFile
appender.otr.name = RollingFile_Otr
appender.otr.fileName = D:\\RLS\\otr_%d{MMdd}.log
appender.otr.filePattern = D:\\RLS\\otr_%d{MMdd}.log
appender.otr.layout.type = PatternLayout
appender.otr.layout.pattern = %d{ABSOLUTE} %level{length=1} 
%markerSimpleName [%C{1}:%L] %m%n
appender.otr.policies.type = Policies
appender.otr.policies.time.type = TimeBasedTriggeringPolicy
appender.otr.policies.time.interval = 1
appender.otr.policies.time.modulate = true
appender.otr.policies.size.type = SizeBasedTriggeringPolicy
appender.otr.policies.size.size = 100MB
appender.otr.strategy.type = DefaultRolloverStrategy
appender.otr.strategy.max = 5

appender.routing.type = Routing
appender.routing.name = Route_Finder
appender.routing.routes.type = Routes
appender.routing.routes.pattern = $${ctx:cfg}
appender.routing.routes.route1.type = Route
appender.routing.routes.route1.ref = RollingFile_Rls
appender.routing.routes.route1.key = RLS
appender.routing.routes.route2.type = Route
appender.routing.routes.route2.ref = RollingFile_Otr
appender.routing.routes.route2.key = $${ctx:cfg}

loggers = rls,otr

logger.rls.name = logging
logger.rls.level = info
logger.rls.additivity = false
logger.rls.appenderRefs=rls
logger.rls.appenderRef.rls.ref = Route_Finder

logger.rls.name = logging
logger.rls.level = info
logger.rls.additivity = false
logger.rls.appenderRefs=rls
logger.rls.appenderRef.rls.ref = Route_Finder

logger.otr.name = other
logger.otr.level = info
logger.otr.additivity = false
logger.otr.appenderRefs=otr
logger.otr.appenderRef.otr.ref = Route_Finder

rootLogger.level = trace
rootLogger.appenderRefs = stdout
rootLogger.appenderRef.stdout.ref = stdout
Allusion answered 9/3, 2018 at 6:51 Comment(1)
Found out the solution: The property key should be 'isThreadContextMapInheritable' and not 'log4j2.isThreadContextMapInheritable'Allusion
R
8

You can put log4j2.component.properties file in the classpath to control various aspects of Log4j 2 behavior.
For example content of log4j2.component.properties:

# https://logging.apache.org/log4j/2.x/manual/configuration.html#SystemProperties

# If true use an InheritableThreadLocal to implement the ThreadContext map.
# Otherwise, use a plain ThreadLocal. 
# (Maybe ignored if a custom ThreadContext map is specified.)
# Default is false

# Modern 2.10+
log4j2.isThreadContextMapInheritable=true

# Legacy for pre-2.10
isThreadContextMapInheritable=true

This has priority over system properties, but it can be overridden by environment variable LOG4J_IS_THREAD_CONTEXT_MAP_INHERITABLE as described in the documentation.

Ruffner answered 30/5, 2019 at 10:14 Comment(0)
P
5

Adding OP's comment as answer

The ThreadContext map can be configured to use an InheritableThreadLocal by setting system property isThreadContextMapInheritable to true.

Set the system property as -DisThreadContextMapInheritable=true when we start our application, or in application code using the following piece of code:
System.setProperty("isThreadContextMapInheritable", "true");

https://logging.apache.org/log4j/2.x/manual/thread-context.html https://logging.apache.org/log4j/2.x/manual/configuration.html#SystemProperties

Palmitate answered 20/6, 2018 at 9:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.