log4j2 LoggerContext未显示在JMX Gui或JConsole中

时间:2013-11-06 15:48:56

标签: java logging log4j jmx log4j2

我的Google foo今天让我失望了!我正在尝试配置log4j2,以便我可以通过JMX访问记录器并更改其日志级别。

然而,当我挂钩所有内容时,没有任何记录器出现在JMX树(或log4j-jmx-gui)中,如log4j2 JMX docs所示。只显示JConsole属性树中的log4j2 ContextSelector和StatusLogger以及log4j-jmx-gui中的StatusLogger选项卡(独立或作为插件)。没有LoggerContext元素。

我用Google搜索,没有想出任何东西!这可能意味着我在某处遗漏了某些东西。任何帮助都会受到我和墙壁的极大赞赏,我正在敲打着我的脑袋:)

我在win7上使用Log4j2 beta9和Java 1.6.0_45-b06。我在Java 7上尝试过它会导致相同的行为。

正在使用的log4j2依赖项:

log4j-api-2.0-beta9.jar
log4j-core-2.0-beta9.jar

我的log4j2.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<Configuration  status="debug">
    <Appenders>
        <Console name="log-test" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{DATE} %-5p LJX2 %c{1}: %m%n" />
        </Console>
    </Appenders>
    <Loggers>
        <Logger name="top.Log4J2TestApp" level="debug"  additivity="false">
            <AppenderRef ref="log-test" />
        </Logger>
        <Root level="trace">
            <AppenderRef ref="log-test" />
        </Root>
    </Loggers>
</Configuration>

我的测试程序:

package top;   
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class Log4J2TestApp { 
    public static void main(String[] args) {
        System.out.println("******** Started ZZZ ****************");
        Logger log = LogManager.getLogger(Log4J2TestApp.class);
        int cnt = 0;
        while (true) {
            log.warn("=========== Test message: {} ================", cnt++);
            Thread.sleep(1000);
        }
    }
}

测试程序的输出如下所示:

******** Started ZZZ ****************
2013-11-06 15:07:29,720 DEBUG Generated plugins in 0.000020959 seconds
2013-11-06 15:07:29,733 DEBUG Calling createLayout on class org.apache.logging.log4j.core.layout.PatternLayout for element PatternLayout with params(pattern="%d{DATE} %-5p LJX2 %c{1}: %m%n", Configuration(log4j2.xml), null, charset="null",alwaysWriteExceptions="null")
2013-11-06 15:07:29,735 DEBUG Generated plugins in 0.000013687 seconds
2013-11-06 15:07:29,737 DEBUG Calling createAppender on class org.apache.logging.log4j.core.appender.ConsoleAppender for element Console with params(PatternLayout(%d{DATE} %-5p LJX2 %c{1}: %m%n), null, target="SYSTEM_OUT", name="log-test",follow="null", ignoreExceptions="null")
2013-11-06 15:07:29,739 DEBUG Jansi is not installed, cannot find org.fusesource.jansi.WindowsAnsiOutputStream2013-11-06 15:07:29,740 DEBUG Calling createAppenders on class org.apache.logging.log4j.core.config.plugins.AppendersPlugin for element Appenders with params(Appenders={log-test})
2013-11-06 15:07:29,742 DEBUG Generated plugins in 0.000012832 seconds
2013-11-06 15:07:29,743 DEBUG Calling createAppenderRef on class org.apache.logging.log4j.core.config.AppenderRef for element AppenderRef with params(ref="log-test", level="null", null)
2013-11-06 15:07:29,746 DEBUG Calling createLogger on class org.apache.logging.log4j.core.config.LoggerConfig for element Logger with params(additivity="false", level="debug", name="top.Log4J2TestApp", includeLocation="null", AppenderRef={log-test}, Properties={}, Configuration(log4j2.xml), null)
2013-11-06 15:07:29,749 DEBUG Calling createAppenderRef on class org.apache.logging.log4j.core.config.AppenderRef for element AppenderRef with params(ref="log-test", level="null", null)
2013-11-06 15:07:29,751 DEBUG Calling createLogger on class org.apache.logging.log4j.core.config.LoggerConfig$RootLogger for element Root with params(additivity="null", level="trace", includeLocation="null", AppenderRef={log-test}, Properties={}, Configuration(log4j2.xml), null)
2013-11-06 15:07:29,754 DEBUG Calling createLoggers on class org.apache.logging.log4j.core.config.plugins.LoggersPluginfor element Loggers with params(Loggers={top.Log4J2TestApp, root})
2013-11-06 15:07:29,755 DEBUG Reconfiguration completed
06 Nov 2013 15:07:29,763 WARN  LJX2 Log4J2TestApp: =========== Test message: 0 ================
....
06 Nov 2013 15:07:36,766 WARN  LJX2 Log4J2TestApp: =========== Test message: 7 ================

2013-11-06 15:07:51,450 DEBUG ServletContext not present - WebLookup not added
2013-11-06 15:07:51,451 DEBUG Shutting down OutputStreamManager SYSTEM_OUT

log4j-jmx-gui中的StatusLogger选项卡确实有一个例外,但它似乎与记录器无关。

2013-11-06 15:41:01,403 WARN Multiple logging implementations found: 
Factory: org.apache.logging.log4j.core.impl.Log4jContextFactory, Weighting: 10
Using factory: org.apache.logging.log4j.core.impl.Log4jContextFactory
2013-11-06 15:41:01,505 WARN JmDNS or serviceInfo class not found java.lang.ClassNotFoundException: javax.jmdns.JmDNS
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:171)
    at org.apache.logging.log4j.core.net.MulticastDNSAdvertiser.initializeJMDNS(MulticastDNSAdvertiser.java:228)
    at org.apache.logging.log4j.core.net.MulticastDNSAdvertiser.<clinit>(MulticastDNSAdvertiser.java:41)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:171)
    at org.apache.logging.log4j.core.config.plugins.PluginManager.decode(PluginManager.java:241)
    at org.apache.logging.log4j.core.config.plugins.PluginManager.collectPlugins(PluginManager.java:152)
    at org.apache.logging.log4j.core.config.plugins.PluginManager.collectPlugins(PluginManager.java:130)
    at org.apache.logging.log4j.core.pattern.PatternParser.<init>(PatternParser.java:116)
    at org.apache.logging.log4j.core.pattern.PatternParser.<init>(PatternParser.java:102)
    at org.apache.logging.log4j.core.layout.PatternLayout.createPatternParser(PatternLayout.java:183)
    at org.apache.logging.log4j.core.layout.PatternLayout.<init>(PatternLayout.java:115)
    at org.apache.logging.log4j.core.layout.PatternLayout.createLayout(PatternLayout.java:219)
    at org.apache.logging.log4j.core.config.DefaultConfiguration.<init>(DefaultConfiguration.java:51)
    at org.apache.logging.log4j.core.LoggerContext.<init>(LoggerContext.java:63)
    at org.apache.logging.log4j.core.selector.ClassLoaderContextSelector.locateContext(ClassLoaderContextSelector.java:217)
    at org.apache.logging.log4j.core.selector.ClassLoaderContextSelector.getContext(ClassLoaderContextSelector.java:114)
    at org.apache.logging.log4j.core.selector.ClassLoaderContextSelector.getContext(ClassLoaderContextSelector.java:81)
    at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:83)
    at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:34)
    at org.apache.logging.log4j.LogManager.getLogger(LogManager.java:387)
    at org.apache.logging.log4j.LogManager.getLogger(LogManager.java:332)
    at top.Log4J2TestApp.main(Log4J2TestApp.java:10)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)

2013-11-06 15:41:01,612 DEBUG Generated plugins in 0.000017537 seconds
2013-11-06 15:41:01,625 DEBUG Calling createLayout on class org.apache.logging.log4j.core.layout.PatternLayout for element PatternLayout with params(pattern="%d{DATE} %-5p LJX2 %c{1}: %m%n", Configuration(log4j2.xml), null, charset="null", alwaysWriteExceptions="null")
2013-11-06 15:41:01,627 DEBUG Generated plugins in 0.000011977 seconds
2013-11-06 15:41:01,629 DEBUG Calling createAppender on class org.apache.logging.log4j.core.appender.ConsoleAppender for element Console with params(PatternLayout(%d{DATE} %-5p LJX2 %c{1}: %m%n), null, target="SYSTEM_OUT", name="log-test", follow="null", ignoreExceptions="null")
2013-11-06 15:41:01,631 DEBUG Jansi is not installed, cannot find org.fusesource.jansi.WindowsAnsiOutputStream
2013-11-06 15:41:01,632 DEBUG Calling createAppenders on class org.apache.logging.log4j.core.config.plugins.AppendersPlugin for element Appenders with params(Appenders={log-test})
2013-11-06 15:41:01,633 DEBUG Generated plugins in 0.000011977 seconds
2013-11-06 15:41:01,635 DEBUG Calling createAppenderRef on class org.apache.logging.log4j.core.config.AppenderRef for element AppenderRef with params(ref="log-test", level="null", null)
2013-11-06 15:41:01,637 DEBUG Calling createLogger on class org.apache.logging.log4j.core.config.LoggerConfig for element Logger with params(additivity="false", level="debug", name="top.Log4J2TestApp", includeLocation="null", AppenderRef={log-test}, Properties={}, Configuration(log4j2.xml), null)
2013-11-06 15:41:01,640 DEBUG Calling createAppenderRef on class org.apache.logging.log4j.core.config.AppenderRef for element AppenderRef with params(ref="log-test", level="null", null)
2013-11-06 15:41:01,642 DEBUG Calling createLogger on class org.apache.logging.log4j.core.config.LoggerConfig$RootLogger for element Root with params(additivity="null", level="trace", includeLocation="null", AppenderRef={log-test}, Properties={}, Configuration(log4j2.xml), null)
2013-11-06 15:41:01,644 DEBUG Calling createLoggers on class org.apache.logging.log4j.core.config.plugins.LoggersPlugin for element Loggers with params(Loggers={top.Log4J2TestApp, root})
2013-11-06 15:41:01,646 DEBUG Reconfiguration completed

2 个答案:

答案 0 :(得分:1)

您的配置看起来不错,唯一有点可疑的是错误。这个错误似乎确实发生在LoggerContext构造函数中,所以它可能毕竟是相关的。

您可能发现了一个错误。您是否介意在log4j2问题跟踪器中报告此问题,以便log4j团队可以查看它?

答案 1 :(得分:1)

Log4j2默认使用ClassLoaderContextSelector作为ContextSelector。在某些情况下(我不清楚,可能是像你这样的简单应用程序)它没有返回getLoggerContexts()方法调用结果。没有记录器上下文 - 没有任何显示,因此树中不存在LoggerContext类别。可以通过选择另一个ContextSelector实现来修复它。我为此目的使用了org.apache.logging.log4j.core.selector.BasicContextSelector。可以使用名为“Log4jContextSelector”的系统属性进行设置。有关详细信息,请参阅docs

它解决了我的问题,我希望它也有助于解决你的问题。