1

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>  
  • Do you have a `jboss-deployment-structure.xml` where you exclude the `org.slf4j` module and include your own slf4j-api.jar? – James R. Perkins Mar 01 '22 at 22:43
  • Yes, I tried that also but not worked. Please see the configs in post (I have edited). – Muhammad Hassan Mar 02 '22 at 06:06
  • @JamesR.Perkins Please guide on it. – Muhammad Hassan Mar 02 '22 at 12:12
  • Are the WAR files all part of an EAR or are they individually deployed? – James R. Perkins Mar 02 '22 at 19:21
  • Individually deployed. They interact with each other. – Muhammad Hassan Mar 02 '22 at 19:34
  • Another point I found is that if I include log4j.xml (the old one) in the WAR files, the logs are getting generated in log file. So I guess that it is still using log4j 1.x from somewhere but the WAR files does not contain any log4j 1.x Maven dependencies. – Muhammad Hassan Mar 02 '22 at 20:12
  • I want to start by saying it's bad practice to have 3 different handlers logging into the same file. You could get very unpredictable results. That said make sure each WAR has the `log4j-api.jar` and `log4j-core.jar` as well as what ever implementation for the logging facade you're using which looks like slf4j. – James R. Perkins Mar 02 '22 at 23:03
  • Please suggest a solution, how I can implement log4j2 with Wildfly to get a separate log file rather than the conventional one (server.logs). I need a single log file for all WAR files. What can be the feasible solution to acheive it? And yes, I have used slf4j on few places with adapter. But the thing is, logging is working perfect for Console but for file, nothing working. – Muhammad Hassan Mar 03 '22 at 10:03
  • On Console logs, I figured out one more thing is that the logs are not in the format which I have defined in log4j2.xml but they are in the default format of Wildfly/Standalone configs. – Muhammad Hassan Mar 03 '22 at 10:30

1 Answers1

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.

James R. Perkins
  • 16,800
  • 44
  • 60
  • Thanks for answering this useful info. You figured out correctly that using the same log4j configuration for three WAR files will create mess up. I already faced this issue previously when I had log4j 1.2.7 so I decided to upgrade my log4j to version 2+. I thought there will be some way to handle this scenario. And as far as slf4j is concern, I can remove that and just use log4j2 coz I have to go with log4j2 at all. Please guide me how I can just go with log4j2 with Wildfly or what can be the best solution for my case? – Muhammad Hassan Mar 03 '22 at 18:18
  • And it is EAP based server (many products with different WAR files under same server with multiple NODES). How I can create a logging-profile in Wildfly as I don't think so that it will be possible depending on the environment nature. – Muhammad Hassan Mar 03 '22 at 18:20
  • I mean how I can create/provide a logging-profile from my end (dev-side). Let's assume, I don't have access to standalone configurations. Is there any way? – Muhammad Hassan Mar 03 '22 at 18:29
  • You cannot get WildFly or JBoss EAP itself to use log4j2, only your application can use the log4j2-core log manager. In WildFly 22 and JBoss EAP 7.4 you can use the log4j-api with the logging subsystem. Versions older than that you'd need to include the org.jboss.logmanager:log4j2-jboss-logmanager in your deployment. For logging profiles those are in JBoss EAP 7+ and IIRC all versions of WildFly. – James R. Perkins Mar 03 '22 at 22:05
  • I have Wildfly 9.0.2. How I can include org.jboss.logmanager:log4j2-jboss-logmanager in my deployment and will the log4j2 will work with this as required? Please write a solution for it. Really appreciate your help! – Muhammad Hassan Mar 04 '22 at 05:53
  • The log4j2-jboss-logmanager is available in Maven Central. It's an implementation of the log4j-api that uses the jboss-logmanager. It will not work with your log4j.xml file. You'd have to use the logging subsystem in WildFly to configure your logging. https://github.com/jboss-logging/log4j2-jboss-logmanager – James R. Perkins Mar 04 '22 at 15:00
  • What if I don't have the option to add logging subsystem in Wildfly? I mean, is there no way to do a workaround to have log4j2 support with Wildfly 9.0.2? Can I implement log4j2 to work with Wildfly and with my configurations in log4j2.xml? Please guide on it! – Muhammad Hassan Mar 05 '22 at 14:57
  • Hi James, I added "log4j2-jboss-logmanager" as dependency in my pom file and removed the log4j-to-slf4j dependency and the logging starts working as required. Thanks a lot for letting this know. But, still some issues. I found that log messages are printing in two files (one is mine "ABC.log" for which have log4j2 implemented and other is "XYZ.log" which is getting generated using logj.xml version 1.x.). The "ABC.log" is correct but I don't want my messages in "XYZ.log" coz that one is related to an independent and separate WAR file. – Muhammad Hassan Mar 05 '22 at 21:10
  • And my WAR files "SYSTEM_OUT" statements are also getting printing in "XYZ.log" which is not correct, I want those in my log file: "ABC.log". – Muhammad Hassan Mar 05 '22 at 21:12
  • You should probably remove the `log4j.xml`. WildFly will attempt to configuring log4j 1.x if it finds a log4j configuration file in your deployment. If you can't use the logging subsystem, then you'll need to include log4j-api, log4j-core and any other logging dependencies you need. It would probably be easiest to exclude the logging subsystem from your deployments if you go with a log4j2.xml configuration. – James R. Perkins Mar 07 '22 at 16:23
  • Please see this my another post in which I have explained it in detail. https://stackoverflow.com/questions/71373802/wrong-logging-in-jboss-wildfly-with-log4j2 – Muhammad Hassan Mar 07 '22 at 17:56