2

I have implemented log4j 2.12.4 as my logging framework. The application on which I am working consists of four WAR files. Three WAR files are directly related to my team while the other team is working on fourth WAR file. I have configured log4j2.xml in three of my WAR files while the fourth one have log4j.xml - means log4j 1.x.

My three WAR files logs are writing to a single log file (ABC.log) - I have defined and want in this way. And the logs of fourth WAR file are writing to another log file (XYZ.log). Now, the logging for ABC.log is working fine but on Console the logs pattern is very haphazard.

I am using Wildfly 9.0.2 as my application server. On console the logs are printing in a way like:

INFO : stdout - [23:17:05,641] [INFO] ClassA:177 - DateString: 2022-01-01

But in actual, I want to display it as:

[23:17:05,641] [INFO] ClassA:177 - DateString: 2022-01-01

Why the additional text in start of the log message is appearing? My POM file have following dependencies for log4j2:

   <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>2.12.4</version>       
   </dependency>        
   <dependency>
        <groupId>org.jboss.logmanager</groupId>
        <artifactId>log4j2-jboss-logmanager</artifactId>
        <version>1.0.0.Final</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>

Besides this wrong logging format on Console, one other issue is that the logs of my three WAR files are writing in the log file of fourth WAR also which is "XYZ.log" and in the wrong format too.

I don't want to get write my WAR files logs into "XYZ.log" and I have not configured that anywhere but it is writing.

I tried by adding jboss-deployment-structure.xml with following configurations but it didn't get fixed:

<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>

Please let me know what can be the issue of wrong logging format on Console and writing the logs in another log file which I haven't defined. Any help would be really appreciated!

  • Have a look at https://stackoverflow.com/a/62784573/152794. – James R. Perkins Mar 07 '22 at 20:08
  • Thanks for the reply. I tried to exclude the logging subsystem as well using a jboss-deployment-structure but it didn't worked either. And the command line option, I can't do those as I will not have access to Production Environment and resources. Is there any other workaround? – Muhammad Hassan Mar 07 '22 at 20:13
  • Having 3 different deployments have their own logging configuration that writes to the same file is not really a good idea. Your logs could get jumbled because it's not using a shared writer. As to the console format issue you'd need to exclude the logging subsystem in your `jboss-deployment-structure.xml`. – James R. Perkins Mar 08 '22 at 02:16
  • I tested it. The logging for three WAR files in a single log file is working fine but on Console, the format is that which I shared above. I tried to exclude the logging subsystem as well but it didn't worked also. – Muhammad Hassan Mar 08 '22 at 05:36
  • While it might look like it's working there is a high potential that logs cross each other and you get parts of each message mixed in the file. That said the `stdout` issue seems to be getting discussed https://stackoverflow.com/q/71378338/152794 so it should remain in one place. – James R. Perkins Mar 08 '22 at 16:01
  • James, as I mentioned before that I tried to exclude the logging subsytem jboss-deployment-structure.xml but it didn't worked either. Can you please guide me how I can do all configurations from dev end by not doing any changes in standalone/CLI because I will not have access to that on production server. – Muhammad Hassan Mar 08 '22 at 17:25
  • TBH the simplest solution is to not use the console appender. The other option is to not use a log4j2 configuration. Short of that, you'll have to use a combo of using a logging.properties file and NOT excluding the logging subsystem from your deployment. – James R. Perkins Mar 08 '22 at 21:51
  • What is the actual reason of log4j2 issues with Wildfly 9.0.2? Is log4j2 not supported for Wildfly older versions? Or, is there any way using which we can configure log4j2 properly with Wildfly (by adding any dependencies or any other configurations like adding something in Standalone.xml)? – Muhammad Hassan Mar 09 '22 at 06:32
  • I just tried out to removing the console appender and it started working fine. Is there any other way using which we can print logs on console? – Muhammad Hassan Mar 09 '22 at 13:46
  • In this case, logging on console is not working if I removed that. I want to have logging on Console also. – Muhammad Hassan Mar 09 '22 at 14:52
  • It's working, it's just not working the way you want it to. WildFly wraps `System.out` and `System.err` in loggers for cases when users do `System.out.println()` and there is no console it can be captured in a log file. If you want to use another handler/appender which writes to `System.out` you've got to either write to the file descriptor or configure WldFly to not use any format for the `stdout` logger. – James R. Perkins Mar 09 '22 at 15:50
  • Is there any way through which I can only use the log4j2 imports/classes and use the logging subsytem of Wildfly as I am not able to find a way through which I can configure the log4j2 with Wildfly properly. – Muhammad Hassan Mar 09 '22 at 17:45
  • Yes, but you said you won't have control over the WildFly configuration so it won't do you much good. You'd only be able to use types form the log4j-api library. – James R. Perkins Mar 09 '22 at 21:02
  • One thing that I tried is that if I use the old configuration file (log4j.xml with old tags), and log4j2 dependencies and imports/classes then it is working also (logging in file and console with desired format). What can be this behavior? – Muhammad Hassan Mar 10 '22 at 09:40
  • The difference is that WildFly 9 supports log4j configuration files and provides a `ConsoleAppender` which writes to the stdout file descriptor. There is no support for this in WildFly 9 and log4j2. – James R. Perkins Mar 10 '22 at 14:58
  • Is there something like log4j2 to jboss which can configure the logs configuration properly? – Muhammad Hassan Mar 11 '22 at 20:16
  • Just the logging subsystem :) If you want to use the log manager provided by WildFly you could use a `logging.properties`. Here's an example of one https://gist.github.com/jamezp/38e329f814239a4d40c9e1ddb12de79e. – James R. Perkins Mar 11 '22 at 20:52
  • How I can define my loggers (e.g com.example, com.demo) in this file configuration with a separate log file name like "abc.log". Please guide me about this in detail. – Muhammad Hassan Mar 12 '22 at 14:04
  • Your best option is to use the logging subsystem for configuring logging if you want 3 different WAR's to write to the same file. If you cannot manage the logging subsystem, then you should not write to the same file. – James R. Perkins Mar 14 '22 at 14:51
  • Which logging subsystem you are referring here? The one the log4j2 has to the Wildfly existing one? – Muhammad Hassan Mar 16 '22 at 18:55
  • There is only one and that is how WildFly configures logging. Like any subsystem in WildFly/JBoss EAP. https://docs.wildfly.org/26/Admin_Guide.html#Logging – James R. Perkins Mar 16 '22 at 22:03

0 Answers0