3

我正在尝试根据设置为 ThreadContext 的键记录 Java 应用程序的事件以分隔日志文件。但是即使在系统属性中将“log4j2.isThreadContextMapInheritable”设置为“true”后,我的密钥也没有到达子线程(在 MouseEvent 上创建)。有人请帮我解决这个问题。

我的主要方法:

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 );
}

}

按钮框架类:

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 文件:

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
4

2 回答 2

5

您可以将log4j2.component.properties文件放在类路径中以控制 Log4j 2 行为的各个方面。
例如内容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

这优先于系统属性,但它可以被环境变量覆盖,LOG4J_IS_THREAD_CONTEXT_MAP_INHERITABLE文档中所述。

于 2019-05-30T10:14:43.070 回答
2

添加 OP 的评论作为答案

通过将系统属性设置为,可以将ThreadContext地图配置为使用。InheritableThreadLocalisThreadContextMapInheritabletrue

在我们启动应用程序时设置系统属性-DisThreadContextMapInheritable=true,或者在应用程序代码中使用以下代码:
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

于 2018-06-20T09:52:16.850 回答