编写了一个使用 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 下有一个自定义附加程序(插件),如下所示
@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。
访问 URL 后,我在 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 出现同样的错误。