在不使用线程上下文的情况下在log4j2中记录PID [英] Logging the PID in log4j2 without using thread context

查看:94
本文介绍了在不使用线程上下文的情况下在log4j2中记录PID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在我的log4js日志中包含PID.我看到许多使用线程上下文的示例.但是,需要在创建的每个单独线程上设置这些值.我受此限制.

我需要一种解决方案,要么不使用线程上下文,要么可以在所有线程上下文中针对任何可以从任何任意类创建的任何线程设置PID.

解决方案

请在Log4j2上创建功能请求问题跟踪器以使其成为内置功能.<​​/p>

现在,您可以创建一个自定义插件.请参阅下面的代码.这将允许您在样式布局(类似于%m的消息).

import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.config.plugins.Plugin;

@Plugin(name = "ProcessIdPatternConverter", category = "Converter")
@ConverterKeys({ "pid", "processId" })
public final class ProcessIdPatternConverter extends LogEventPatternConverter {
    private final String pid;

    private ProcessIdPatternConverter(String[] options) {
        super("Process ID", "pid");
        String temp = options.length > 0 ? options[0] : "???";
        try {
            // likely works on most platforms
            temp = ManagementFactory.getRuntimeMXBean().getName().split("@")[0];
        } catch (final Exception ex) {
            try {
                // try a Linux-specific way
                temp = new File("/proc/self").getCanonicalFile().getName();
            } catch (final IOException ignoredUseDefault) {}
        }
        pid = temp;
    }

    /**
     * Obtains an instance of ProcessIdPatternConverter.
     *
     * @param options users may specify a default like {@code %pid{NOPID} }
     * @return instance of ProcessIdPatternConverter.
     */
    public static ProcessIdPatternConverter newInstance(final String[] options) {
        return new ProcessIdPatternConverter(options);
    }

    @Override
    public void format(final LogEvent event, final StringBuilder toAppendTo) {
        toAppendTo.append(pid);
    }
}

有关如何使用Log4j2的更多详细信息,请参见手册.插件工作.

让Log4j2识别您的插件的一种方法是通过在配置的packages属性中指定插件类的软件包名称:

<Configuration status="trace" 
     packages="com.myorg.mypluginpackage">

(跟踪可在Log4j2内部调试上切换,以帮助进行故障排除.)

I need to include the PID in my log4js logs. I see many examples which use the thread context. However, these need to be set on each individual thread created. I am constrained against doing this.

I need a solution that, either, does not use the thread context, or, can set the PID on all thread contexts, for any thread that may be created, from any arbitrary class.

解决方案

Please create a feature request on the Log4j2 issue tracker to make this a built-in feature.

For now, you can create a custom plugin. See code below. This will allow you to specify %pid in the pattern layout (similar to %m for the message).

import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.config.plugins.Plugin;

@Plugin(name = "ProcessIdPatternConverter", category = "Converter")
@ConverterKeys({ "pid", "processId" })
public final class ProcessIdPatternConverter extends LogEventPatternConverter {
    private final String pid;

    private ProcessIdPatternConverter(String[] options) {
        super("Process ID", "pid");
        String temp = options.length > 0 ? options[0] : "???";
        try {
            // likely works on most platforms
            temp = ManagementFactory.getRuntimeMXBean().getName().split("@")[0];
        } catch (final Exception ex) {
            try {
                // try a Linux-specific way
                temp = new File("/proc/self").getCanonicalFile().getName();
            } catch (final IOException ignoredUseDefault) {}
        }
        pid = temp;
    }

    /**
     * Obtains an instance of ProcessIdPatternConverter.
     *
     * @param options users may specify a default like {@code %pid{NOPID} }
     * @return instance of ProcessIdPatternConverter.
     */
    public static ProcessIdPatternConverter newInstance(final String[] options) {
        return new ProcessIdPatternConverter(options);
    }

    @Override
    public void format(final LogEvent event, final StringBuilder toAppendTo) {
        toAppendTo.append(pid);
    }
}

See the manual for more details on how Log4j2 plugins work.

One way to let Log4j2 recognize your plugin is by specifying the package name of the plugin class in the packages attribute of the configuration:

<Configuration status="trace" 
     packages="com.myorg.mypluginpackage">

(Trace switches on Log4j2 internal debugging to help with troubleshooting.)

这篇关于在不使用线程上下文的情况下在log4j2中记录PID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆