'Wildfly not picking log4j2.xml configuration file and logging not working
I have upgraded my log4j from 1.2.7 to log4j 2.12.4. I have three WAR files for which I want all the logs (from all WAR files) in a same log file "ABC.log". I have created log4j2.xml for each WAR file and have placed it under classpath "WEB-INF/classes/log4j2" but the logging is not working as required. Even the file (ABC.log) is not creating at all in the specified path (c:/logs/ABC.log).
I am using Wildfly 9.0.2 as my application server. Here are my POM file dependencies for log4j2:
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.12.4</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-to-slf4j</artifactId>
<version>2.12.4</version>
</dependency>
And here are my log4j2.xml configurations:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
<Appenders>
<Console name="LogToConsole" target="SYSTEM_OUT">
<PatternLayout pattern="[%d{HH:mm:ss,SSS}] [%p] %c{1}:%L - %m%n"/>
</Console>
<RollingFile name="LogToRollingFile" fileName="c:/logs/ABC.log"
filePattern="c:/logs/ABC-%d{yyyy-MM-dd}-%i.log.gz">
<PatternLayout>
<Pattern>[%d{HH:mm:ss,SSS}] [%p] %c{1}:%L - %m%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="500 KB"/>
</Policies>
<DefaultRolloverStrategy max="20"/>
</RollingFile>
</Appenders>
<Loggers>
<Logger name="com.example" level="info" additivity="false">
<AppenderRef ref="LogToConsole"/>
<AppenderRef ref="LogToRollingFile"/>
</Logger>
</Logger>
<Logger name="org.jboss" level="error" additivity="false">
<AppenderRef ref="LogToConsole"/>
</Logger>
</Logger>
<Root level="error">
<AppenderRef ref="LogToConsole"/>
</Root>
</Loggers>
</Configuration>
One more thing is that logging is working fine on CONSOLE but for file, it is not working at all.
Please guide me how to make log4j2 configurable with Wildfly so that I can get all three WAR files logs into a single log file. Any help would be really appreciated!
I tried by adding jboss-deployment-structure.xml with following configurations but it didn't worked either:
<?xml version="1.0" encoding="UTF-8"?>
<jboss-deployment-structure>
<deployment>
<exclusions>
<module name="org.apache.commons.logging" />
<module name="org.apache.log4j" />
<module name="org.jboss.logging" />
<module name="org.jboss.logging.jul-to-slf4j-stub" />
<module name="org.jboss.logmanager" />
<module name="org.jboss.logmanager.log4j" />
<module name="org.slf4j" />
<module name="org.slf4j.impl" />
</exclusions>
</deployment>
</jboss-deployment-structure>
Solution 1:[1]
The configuration you provide looks correct to me. You need to ensure the jboss-deployment-structure.xml is in the WAR/WEB-INF directory of each WAR.
That said, using the same log4j configuration in 3 different WAR files is a bad idea. The log manager will NOT share the writer so you will be opening 3 writers to write to the same file. This is not a good idea as the results are unpredictable. You may end up with mixed log messages.
One options is to not use log42 and use a logging-profile in WildFly. This would allow your WAR's to log into the same file as a single writer is used. Since you're using slf4j as your logging facade, it doesn't seem as if log4j2 is required. In this case you'd also not need the jboss-deployment-structure.xml.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | James R. Perkins |
