Search code examples
jbosswildflylog4j2slf4j

Duplicately formatted logger output insid JBoss/ Wildfly AS


It seems, that I cannot exclude the JBoss/ Wildfly Logging Subsystem.

I have an Java EE ear and would like to use slf4j API together with log4J2 Implementation.

My intention was to disable JBoss logging subsystem to let my slf4j log4j2 implementation handle the logging. But I still get duplicate handling of stdout:

12:55:00,820 INFO [stdout] (Thread-316) 12:55:00 INFO

the ear structure is as follows:

d-----       03.07.2020     12:53                lib
d-----       03.07.2020     12:53                META-INF
-a----       03.07.2020     12:53          13238 org.example-helloworld-ejb-1.0-SNAPSHOT.jar
-a----       03.07.2020     12:53        3598717 org.example-helloworld-web-1.0-SNAPSHOT.war

the lib directory:

-a----       03.07.2020     12:53          26586 javax.annotation-javax.annotation-api-1.3.2.jar
-a----       03.07.2020     12:53          63679 javax.ejb-javax.ejb-api-3.2.2.jar
-a----       03.07.2020     12:53          28016 javax.transaction-javax.transaction-api-1.3.jar
-a----       03.07.2020     12:53         126898 javax.ws.rs-javax.ws.rs-api-2.1.1.jar
-a----       03.07.2020     12:53         292301 org.apache.logging.log4j-log4j-api-2.13.3.jar
-a----       03.07.2020     12:53        1714164 org.apache.logging.log4j-log4j-core-2.13.3.jar
-a----       03.07.2020     12:53          23590 org.apache.logging.log4j-log4j-slf4j-impl-2.13.3.jar
-a----       03.07.2020     12:53           1526 org.example-helloworld-config-1.0-SNAPSHOT.jar
-a----       03.07.2020     12:53           6071 org.example-helloworld-model-1.0-SNAPSHOT.jar
-a----       03.07.2020     12:53          41203 org.slf4j-slf4j-api-1.7.25.jar

the META-INF directory:

-a---- 03.07.2020 12:53 470 jboss-deployment-structure.xml

content of jboss-deployment-structure.xml:

<jboss-deployment-structure
        xmlns="urn:jboss:deployment-structure:1.2"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">


    <deployment>

        <dependencies>
            <module name="config"/>
        </dependencies>

    <exclude-subsystems>
        <subsystem name="logging" />
    </exclude-subsystems>

    </deployment>

</jboss-deployment-structure>

Furthermore I modified the standalone.xml:

[..]
 <profile>
        <subsystem xmlns="urn:jboss:domain:logging:8.0">
            <add-logging-api-dependencies value="false"/>
            <console-handler name="CONSOLE">
                <formatter>
[..]

My log4j2.xml:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration monitorInterval="3" status="INFO">
  <Appenders>
    <Console name="Console1" target="SYSTEM_OUT">
      <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{1.}} - %msg%n"/>
    </Console>
  </Appenders>
  <Loggers>
    <Root level="all">
      <AppenderRef ref="Console1"/>
    </Root>
  </Loggers>
</Configuration>

NOTE: I also tried using logback as the logging implementation. This also produces duplicately formatted output to stdout.#

Any hints to solve/ workaround this highly appreciated!


Solution

  • If you want to use log4j-core, the log4j2 log manager implementation, in your deployment with slf4j you'll need to include the correct dependencies in your deployment which you appear to be doing.

    You'll then need to tell WildFly to not add the logging dependencies to your deployment. The following options will exclude the logging dependencies from being added to your deployment

    1. Use a jboss-deployment-structure.xml to exclude the logging subsystem
    <?xml version="1.0" encoding="UTF-8"?>
    <jboss-deployment-structure>
        <deployment>
            <exclude-subsystems>
                <subsystem name="logging"/>
            </exclude-subsystems>
        </deployment>
    </jboss-deployment-structure>
    
    1. Change the add-logging-api-dependencies attribute to false. Note this affects all deployments.
    /subsystem=logging:write-attribute(name=add-logging-api-dependencies, value=false)
    
    1. Use a jboss-deployment-structure.xml to exclude each logging module. Option 1 is a lot easier than this though.

    The reason for the duplicate output is WildFly wraps System.out and System.err in a logger with the respective logger names of stdout and stderr. To work around this you'd need to add a new formatter, console-handler and logger to remove the default console handlers format.

    /subsystem=logging/pattern-formatter=stdout:add(pattern="%s%n")
    /subsystem=logging/console-handler=stdout:add(named-formatter=stdout, autoflush=true, target=System.out)
    /subsystem=logging/logger=stdout:add(use-parent-handlers=false, handlers=[stdout])