Log4j ThreadContext多线程不会继承。

huangapple go评论88阅读模式
英文:

Log4j ThreadContext MultiThread won't inherit

问题

我遇到了这个帖子的问题
以下是我的代码示例:

public class StackOverflow implements RequestHandler<Map<String, String>, ApiGatewayResponse> {
    private static final Logger LOGGER = LogManager.getLogger(StackOverflow.class);

    public abstract class StreamGobbler extends Thread {
        // 使用this.print("message")的工作拦截器
        public abstract void print(String line);
    }

    private class ErrStreamGobbler extends StreamGobbler {
        ErrStreamGobbler(InputStream in) { super(in); }

        @Override
        public void print(String line) { LOGGER.error(line); }
    }

    private class OutStreamGobbler extends StreamGobbler {
        OutStreamGobbler(InputStream in) { super(in); }

        @Override
        public void print(String line) { LOGGER.info(line); }
    }


    // 主方法
    @Override
    public ApiGatewayResponse handleRequest(Map<String, String> params, Context context) {
        // ThreadContext 传播
        System.setProperty("isThreadContextMapInheritable", "true");
        ThreadContext.put("foo", "bar");

        LOGGER.info("Hello from main");

        // 我的进程将在 System.in 和 System.out 上返回值
        ProcessBuilder pb = new ProcessBuilder("sh", String.format("batchs/JOB_FOO/run.sh"));
        Process p = pb.start();
        // 拦截这些日志
        new ErrStreamGobbler(p.getErrorStream()).start();
        new OutStreamGobbler(p.getInputStream()).start();
        p.waitFor();
    }
}

LOGGER.info("Hello from main"); 中的 ThreadContext 是有效的,并且还会打印出 foo: bar
但是我的子线程 StreamGobbler 并未获得 ThreadContext,即使将 isThreadContextMapInheritable 设置为 true,也不会将 foo: bar 打印为 log4j 属性。

英文:

I encounter this post's problem
Here my sample of code

public class StackOverflow implements RequestHandler&lt;Map&lt;String, String&gt;, ApiGatewayResponse&gt; {
    private static final Logger LOGGER = LogManager.getLogger(StackOverflow.class);

    public abstract class StreamGobbler extends Thread {
        // Working Interceptor using this.print(&quot;message&quot;)
        public abstract void print(String line);
    }

    private class ErrStreamGobbler extends StreamGobbler {
        ErrStreamGobbler(InputStream in) { super(in); }

        @Override
        public void print(String line) { LOGGER.error(line); }
    }

    private class OutStreamGobbler extends StreamGobbler {
        OutStreamGobbler(InputStream in) { super(in); }

        @Override
        public void print(String line) { LOGGER.info(line); }
    }


    // MAIN METHOD
    @Override
    public ApiGatewayResponse handleRequest(Map&lt;String, String&gt; params, Context context) {
        // ThreadContext propagation
        System.setProperty(&quot;isThreadContextMapInheritable&quot;, &quot;true&quot;);
        ThreadContext.put(&quot;foo&quot;, &quot;bar&quot;);

        LOGGER.info(&quot;Hello from main&quot;);

        // My process will return value on System.in &amp; System.out
        ProcessBuilder pb = new ProcessBuilder(&quot;sh&quot;, String.format(&quot;batchs/JOB_FOO/run.sh&quot;));
        Process p = pb.start();
        // Intercept those logs
        new ErrStreamGobbler(p.getErrorStream()).start();
        new OutStreamGobbler(p.getInputStream()).start();
        p.waitFor();
    }
}

The ThreadContext from LOGGER.info(&quot;Hello from main&quot;); does work and also prints foo: bar.
But my child threads StreamGobbler doesn't get ThreadContext and won't print foo: bar as a log4j property event if isThreadContextMapInheritable is set to true.

答案1

得分: 2

刚刚遇到了这个问题。发现在应用程序启动时设置 -DisThreadContextMapInheritable=true,即使在代码中设置它并不起作用。

英文:

Just encountered this problem. Found setting -DisThreadContextMapInheritable=true at application startup works, even though setting it in the code does not.

答案2

得分: 0

使用自定义的ThreadPoolExecutor确实解决了我的问题。
在这个答案中找到了这个解决方案。

英文:

Using a custom ThreadPoolExecutor did fix my problem.
Found this solution using this answer.

huangapple
  • 本文由 发表于 2020年9月29日 21:51:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/64121046.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定