Wildfly 18 adds server logging pattern before log4j loggers

1.8k Views Asked by At

I have configured my WildFly 18 server in domain mode format and we use log4j logger. When I checked in the server.log file, All logs from log4j are getting logged after wildfly's own logger format. Like below line 1st date and log level is from server logs and next date format and log level and log message is comming from log4j.

2021-03-19 00:13:06,623 INFO  2021-03-19 00:13:06,601 INFO  [com.app.connection.service] CurrentThreadID=436 On

I search a lot...I found so many configurations related to standalone mode and some said these configurations can be done in domain.xml but nothing worked

I have the following configuration in domain.xml

          <subsystem xmlns="urn:jboss:domain:logging:8.0">
                <custom-handler name="CUSTOM-FILE" class="org.jboss.logmanager.handlers.PeriodicSizeRotatingFileHandler" module="org.jboss.logmanager">
                    <formatter>
                        <named-formatter name="MY-PATTERN"/>
                    </formatter>
                    <properties>
                        <property name="maxBackupIndex" value="24"/>
                        <property name="rotateSize" value="10000000"/>
                        <property name="suffix" value=".yyyy-MM-dd-HH"/>
                        <property name="append" value="true"/>
                        <property name="fileName" value="${jboss.server.log.dir}/server.log"/>
                    </properties>
                </custom-handler>
                <logger category="com.arjuna">
                    <level name="WARN"/>
                </logger>
                <logger category="io.jaegertracing.Configuration">
                    <level name="WARN"/>
                </logger>
                <logger category="org.jboss.as.config">
                    <level name="DEBUG"/>
                </logger>
                <logger category="sun.rmi">
                    <level name="WARN"/>
                </logger>
                <root-logger>
                    <level name="INFO"/>
                    <handlers>
                        <handler name="CUSTOM-FILE"/>
                    </handlers>
                </root-logger>
                <formatter name="PATTERN">
                    <pattern-formatter pattern="%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p %s%e%n"/>
                </formatter>
                <formatter name="MY-PATTERN">
                    <pattern-formatter pattern="%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p %s%e%n"/>
                </formatter>
                <formatter name="COLOR-PATTERN">
                    <pattern-formatter pattern="%K{level}%d{HH:mm:ss,SSS} %-5p %s%e%n"/>
                </formatter>
            </subsystem>

In log4j.xml, I have a console appender like below, and this log4j.xml present inside modules directory at path wildfly18.0.1\modules\org\apache\logging\log4j\main\log4j2-props

<Console name="Console">
    <PatternLayout pattern="%d %-5p [%c] %m\n" />
</Console>

Following things I tried :

  1. Added tags inside logging subsystem

    <add-logging-api-dependencies value="false"/>
    <use-deployment-logging-config value="false"/>
    
  2. Added loggers

 <logger category="stdout" use-parent-handlers="false">
        <level name="INFO"/>
        <handlers>
             <handler name="CUSTOM-FILE"/>
         </handlers>
  </logger>
  1. Excluded subsystems and modules from deployments. Added following in META-INF\jboss-deployment-structure.xml

We use EAR deployment file

<deployment>
        <exclude-subsystems>
           <subsystem name="logging"/>
        </exclude-subsystems>
        <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>

So, please can somebody help me with the right configuration for domain mode setup?

Thanks in advance...

2

There are 2 best solutions below

3
James R. Perkins On

I'm not sure it's an option, but WildFly 22 has log4j2 support so you can configure logging with WildFly and don't need to include a log4j2.xml.

It looks like you want to redirect stdout to a file. If you want your log4j2 configuration to control the format, you need to change the format for your MY-FORMATTER. Below are some CLI commands to configure the server.

# Add the pattern for the stdout logger
/subsystem=logging/pattern-formatter=MY-PATTERN:add(pattern="%s%n")
# Create a file to direct stdout to
/subsystem=logging/periodic-size-rotating-file-handler=CUSTOM-FILE:add(named-formatter=MY-PATTERN, max-backup-index=24, rotate-size="10m", suffix=".yyyy-MM-dd-HH", append=true, file={relative-to=jboss.server.log.dir, path=server-stdout.log})
# Create the stdout logger, direct it to the file only
/subsystem=logging/logger=stdout:add(handlers=[CUSTOM-FILE], use-parent-handlers=false)

Note that I renamed the log file to server-stdout.log as to not override the default server.log. The reason for this is the server itself will log to this file and if you override it with that pattern then the logs from the server itself will not be formatted.

Also note that anything that is written to System.out will end up in the server-stout.log as well.

0
Nilesh Kumar On

As written in many other comments, Jboss adds it's own pattern and treats pattern of application log4j2.xml as the "message" to its own pattern. To resolve this add following to the logging subsystem of your Jboss Standalone.xml

   <subsystem xmlns="urn:jboss:domain:logging:8.0">
    ....
    <console-handler name="stdout-console" autoflush="true">
        <level name="ALL"/>
        <formatter>
            <pattern-formatter pattern="%s%n"/>
        </formatter>
    </console-handler>
    <logger category="stdout" use-parent-handlers="false">
        <handlers>
            <handler name="stdout-console"/>
        </handlers>
    </logger>
    ....
   </subsystem>

This will cause the default log pattern of Jboss to be overwritten and you log will have what you have configured in log4j2.xml console appender.