Wildfly 11中的Log4J2导致错误无法找到插件

时间:2017-10-30 22:48:11

标签: log4j2 wildfly-10 jboss-wildfly-11 wildfly-11

写了一个使用Log4J2程序化配置的简单Web应用程序。 (没有log4j2.xml)

配置程序代码段如下所示

ConfigurationBuilder<BuiltConfiguration> builder = ConfigurationBuilderFactory.newConfigurationBuilder();
builder.setStatusLevel(Level.ERROR);
builder.setConfigurationName("LogicLoggerConfigurations");
builder.setPackages("main.logging.config");

AppenderComponentBuilder appenderBuilderConsole = builder.newAppender("Stdout", "CONSOLE").addAttribute("target",
        ConsoleAppender.Target.SYSTEM_OUT);
AppenderComponentBuilder appenderBuilderFile = builder.newAppender("toFile", "FILE").addAttribute("fileName",
        "/pathto/logtest.log");
appenderBuilderConsole.add(builder.newLayout("PatternLayout")
        .addAttribute("pattern", "%d [%t] %-5level: %msg%n%throwable"));
appenderBuilderFile.add(builder.newLayout("PatternLayout")
        .addAttribute("pattern", "%d [%t] %-5level: %msg%n%throwable"));
AppenderComponentBuilder appenderBuilder = builder.newAppender("lacmem", "LogicMemoryAppender");
appenderBuilder.add(builder.newLayout("PatternLayout")
        .addAttribute("pattern", "%d [%t] %-5level: %msg%n%throwable"));
builder.add(appenderBuilder);
builder.add(appenderBuilderConsole);
builder.add(appenderBuilderFile);
builder.add(builder.newLogger("main", Level.TRACE)
        .add(builder.newAppenderRef("lacmem")).addAttribute("additivity", true));
builder.add(builder.newRootLogger(Level.ERROR).add(builder.newAppenderRef("Stdout"))
        .add(builder.newAppenderRef("toFile")));
logicLoggerContext = Configurator.initialize(builder.build());
logicLoggerContext.updateLoggers();

在main.logging.config包下面有一个自定义appender(插件),如下所示

@Plugin(name = "LogicMemoryAppender", category = "Core", elementType = "appender", printObject = true)
public class LogicMemoryAppender extends AbstractAppender {

    List<Map<String, Object>> logs = new CopyOnWriteArrayList<>();

    protected LogicMemoryAppender(String name, Filter filter, Layout<? extends Serializable> layout, boolean ignoreExceptions) {
        super(name, filter, layout, ignoreExceptions);
    }

    /*
     *
     * @see
     * org.apache.logging.log4j.core.Appender#append(org.apache.logging.log4j.
     * core.LogEvent)
     */
    public void append(LogEvent event) {
        Map<String, Object> logEntry = new HashMap<String, Object>();
        logEntry.put("logger", event.getLoggerName());
        if (null != event.getLevel())
            logEntry.put("level", event.getLevel());

        long millis = event.getTimeMillis();
        logEntry.put("ts", millis);
        logEntry.put("timestamp", "timestamp");

        String exceptionText = "";
        if (null != event.getThrown()) {
            exceptionText = event.getThrown().getMessage();
        }

        String msg = event.getMessage().toString();
        if (!msg.isEmpty()) {
            msg = msg.replace("\"", "\\\"");
            msg = msg.replace("\n", "\\n");
            if (!exceptionText.isEmpty()) {
                logEntry.put("message", msg + " : " + exceptionText);
            } else {
                logEntry.put("message", msg);
            }
        } else {
            logEntry.put("message", exceptionText);
        }
System.out.println("Log entry added "+logEntry);
        logs.add(logEntry);

    }


    /**
     * Factory method called by Log4j2 to initialize this appender.
     * @param name
     * @param layout
     * @param filter
     * @return
     */
    @PluginFactory
    public static LogicMemoryAppender createAppender(
            @PluginAttribute("name") String name,
            @PluginElement("Layout") Layout<? extends Serializable> layout,
            @PluginElement("Filter") final Filter filter) {
        if (name == null) {
            LOGGER.error("No name provided for LogicMemoryAppender");
            return null;
        }
        if (layout == null) {
            layout = PatternLayout.createDefaultLayout();
        }
        return new LogicMemoryAppender(name, filter, layout, true);
    }

}

该应用程序是一个基于servlet的应用程序,在doGet()

下有一个日志记录语句
soloLogger = LogManager.getLogger();
soloLogger.trace("What is up");

我还通过将以下配置添加到Web-Inf

来禁用日志记录子系统
<jboss-deployment-structure xmlns="urn:jboss:deployment-structure:1.2">
    <deployment>
        <exclude-subsystems>
            <subsystem name="logging"/>
        </exclude-subsystems>
    </deployment>
</jboss-deployment-structure>

应用程序部署为WAR文件,并通过管理控制台上传到WildFly。

访问网址后,我在Wildfly控制台中看到以下错误。代码按预期用于Tomcat8x和Weblogic 12.2.1.3,但不适用于Jboss。 :(

5:12:45,204 INFO  [stdout] (default task-1) 2017-10-30 15:12:45,203 default task-1 ERROR Unable to locate plugin type for LogicMemoryAppender
15:12:45,237 INFO  [stdout] (default task-1) 2017-10-30 15:12:45,237 default task-1 ERROR Unable to locate plugin for LogicMemoryAppender
15:12:45,247 INFO  [stdout] (default task-1) 2017-10-30 15:12:45,246 default task-1 ERROR Unable to invoke factory method in class org.apache.logging.log4j.core.config.AppendersPlugin for element Appenders: java.lang.NullPointerException java.lang.NullPointerException
15:12:45,247 INFO  [stdout] (default task-1)    at org.apache.logging.log4j.core.config.plugins.visitors.PluginElementVisitor.visit(PluginElementVisitor.java:52)
15:12:45,247 INFO  [stdout] (default task-1)    at org.apache.logging.log4j.core.config.plugins.util.PluginBuilder.generateParameters(PluginBuilder.java:248)
15:12:45,247 INFO  [stdout] (default task-1)    at org.apache.logging.log4j.core.config.plugins.util.PluginBuilder.build(PluginBuilder.java:135)
15:12:45,247 INFO  [stdout] (default task-1)    at org.apache.logging.log4j.core.config.AbstractConfiguration.createPluginObject(AbstractConfiguration.java:958)
15:12:45,247 INFO  [stdout] (default task-1)    at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:898)
15:12:45,247 INFO  [stdout] (default task-1)    at org.apache.logging.log4j.core.config.AbstractConfiguration.doConfigure(AbstractConfiguration.java:513)
15:12:45,247 INFO  [stdout] (default task-1)    at org.apache.logging.log4j.core.config.AbstractConfiguration.initialize(AbstractConfiguration.java:237)
15:12:45,247 INFO  [stdout] (default task-1)    at org.apache.logging.log4j.core.config.builder.impl.DefaultConfigurationBuilder.build(DefaultConfigurationBuilder.java:204)
15:12:45,247 INFO  [stdout] (default task-1)    at org.apache.logging.log4j.core.config.builder.impl.DefaultConfigurationBuilder.build(DefaultConfigurationBuilder.java:161)
15:12:45,247 INFO  [stdout] (default task-1)    at org.apache.logging.log4j.core.config.builder.impl.DefaultConfigurationBuilder.build(DefaultConfigurationBuilder.java:57)
15:12:45,247 INFO  [stdout] (default task-1)    at main.test.HanSolo.initConfig(HanSolo.java:63)
15:12:45,248 INFO  [stdout] (default task-1)    at main.test.HanSolo.<clinit>(HanSolo.java:23)
15:12:45,248 INFO  [stdout] (default task-1)    at main.test.TestLogging.doGet(TestLogging.java:31)
15:12:45,248 INFO  [stdout] (default task-1)    at javax.servlet.http.HttpServlet.service(HttpServlet.java:687)
15:12:45,248 INFO  [stdout] (default task-1)    at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
15:12:45,248 INFO  [stdout] (default task-1)    at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:85)
15:12:45,248 INFO  [stdout] (default task-1)    at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62)
15:12:45,248 INFO  [stdout] (default task-1)    at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
15:12:45,248 INFO  [stdout] (default task-1)    at org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)
15:12:45,248 INFO  [stdout] (default task-1)    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
15:12:45,248 INFO  [stdout] (default task-1)    at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:131)
15:12:45,248 INFO  [stdout] (default task-1)    at io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:57)
15:12:45,248 INFO  [stdout] (default task-1)    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
15:12:45,248 INFO  [stdout] (default task-1)    at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:46)
15:12:45,248 INFO  [stdout] (default task-1)    at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:64)
15:12:45,248 INFO  [stdout] (default task-1)    at io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:60)
15:12:45,248 INFO  [stdout] (default task-1)    at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:77)
15:12:45,248 INFO  [stdout] (default task-1)    at io.undertow.security.handlers.NotificationReceiverHandler.handleRequest(NotificationReceiverHandler.java:50)
15:12:45,249 INFO  [stdout] (default task-1)    at io.undertow.security.handlers.AbstractSecurityContextAssociationHandler.handleRequest(AbstractSecurityContextAssociationHandler.java:43)
15:12:45,249 INFO  [stdout] (default task-1)    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
15:12:45,249 INFO  [stdout] (default task-1)    at org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)
15:12:45,249 INFO  [stdout] (default task-1)    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
15:12:45,249 INFO  [stdout] (default task-1)    at org.wildfly.extension.undertow.deployment.GlobalRequestControllerHandler.handleRequest(GlobalRequestControllerHandler.java:68)
15:12:45,249 INFO  [stdout] (default task-1)    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
15:12:45,249 INFO  [stdout] (default task-1)    at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:292)
15:12:45,249 INFO  [stdout] (default task-1)    at io.undertow.servlet.handlers.ServletInitialHandler.access$100(ServletInitialHandler.java:81)
15:12:45,249 INFO  [stdout] (default task-1)    at io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:138)
15:12:45,249 INFO  [stdout] (default task-1)    at io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:135)
15:12:45,249 INFO  [stdout] (default task-1)    at io.undertow.servlet.core.ServletRequestContextThreadSetupAction$1.call(ServletRequestContextThreadSetupAction.java:48)
15:12:45,249 INFO  [stdout] (default task-1)    at io.undertow.servlet.core.ContextClassLoaderSetupAction$1.call(ContextClassLoaderSetupAction.java:43)
15:12:45,249 INFO  [stdout] (default task-1)    at org.wildfly.extension.undertow.security.SecurityContextThreadSetupAction.lambda$create$0(SecurityContextThreadSetupAction.java:105)
15:12:45,249 INFO  [stdout] (default task-1)    at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1508)
15:12:45,249 INFO  [stdout] (default task-1)    at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1508)
15:12:45,249 INFO  [stdout] (default task-1)    at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1508)
15:12:45,249 INFO  [stdout] (default task-1)    at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1508)
15:12:45,249 INFO  [stdout] (default task-1)    at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:272)
15:12:45,249 INFO  [stdout] (default task-1)    at io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:81)
15:12:45,249 INFO  [stdout] (default task-1)    at io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:104)
15:12:45,249 INFO  [stdout] (default task-1)    at io.undertow.server.Connectors.executeRootHandler(Connectors.java:326)
15:12:45,250 INFO  [stdout] (default task-1)    at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:812)
15:12:45,250 INFO  [stdout] (default task-1)    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
15:12:45,250 INFO  [stdout] (default task-1)    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
15:12:45,250 INFO  [stdout] (default task-1)    at java.lang.Thread.run(Thread.java:745)
15:12:45,250 INFO  [stdout] (default task-1) 
15:12:45,250 INFO  [stdout] (default task-1) 2017-10-30 15:12:45,250 default task-1 ERROR Unable to locate appender "Stdout" for logger config "root"
15:12:45,250 INFO  [stdout] (default task-1) 2017-10-30 15:12:45,250 default task-1 ERROR Unable to locate appender "toFile" for logger config "root"
15:12:45,251 INFO  [stdout] (default task-1) 2017-10-30 15:12:45,251 default task-1 ERROR Unable to locate appender "lacmem" for logger config "main"

Log4j2似乎无法识别WildFly中带注释的插件。不确定这是log4j2还是WildFly的问题。 JVM是Java 8x112

编辑:WildFly 10 Final上的错误相同。

1 个答案:

答案 0 :(得分:0)

我无法找到上述问题的解决方案,但我确实找到了解决方法。

我没有以编程方式进行配置,而是将其与XML配置文件(log4j2.xml)相结合。在XML内部,我初始化了我在代码中使用的所有记录器。

查看official doc,了解有关如何合并的更多信息。

然后进行编程配置  1.创建自己的ConfigurationFactory  2.返回您自己的XMLConfiguration的实例 - &gt;创建一个扩展XMLConfiguration的类,该类操作并将appender添加到上述记录器(LoggerConfig)。

更改完配置后,我调用myloggercontext.updateLoggers()加载更改。这里要注意的一件事是,要注册ConfigurationFactory,您必须将一个名为log4j2.component.properties的属性文件添加到类路径中。我的属性文件的内容如下所示

log4j.configurationFactory=main.logging.config.LogConfigurationFactory

这将确保在对应用程序内的记录器进行任何调用之前加载ConfigurationFactory。

上述更改适用于JBoss,因为它能够按预期加载插件。