Dennis G.
Dennis G.

Reputation: 73

Wrong logging in Jboss Wildfly logfile when logging with Log4J 2

im trying to use Log4J 2.0 in my project as logging framework.

If I log something with Log4J inside my code, I see something like that in my Jboss log file:

11:04:07,606 INFO [stdout] (default task-1) 11:04:07.606 [default task-1] ERROR de.housekeepingbook.services.ClientService - This is a error test message

but I expected something like that:

11:04:07.606 [default task-1] ERROR de.housekeepingbook.services.ClientService - This is a error test message

How can I delete the additional

11:04:07,606 INFO [stdout] (default task-1)

My Log4j2.xml:

    <?xml version="1.0" encoding="UTF-8"?>
<configuration status="WARN">
    <appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
        </Console>

        <File name="MyFile" fileName="logs/app.log">
            <PatternLayout pattern="%d{yyyy-mm-dd HH:mm:ss.SSS} [%t] %-5level   %logger{36} - %msg%n" />
        </File>

    </appenders>

    <loggers>
        <root level="debug">
            <appender-ref ref="Console" level="debug" />
            <appender-ref ref="MyFile" level="debug" />
        </root>
    <Logger name="org.hibernate.SQL" level="debug"/>
    </loggers>
    </configuration>

And my standalone.xml (logging part only):

   <subsystem xmlns="urn:jboss:domain:logging:2.0">
            <console-handler name="CONSOLE">
                <level name="INFO"/>
                <formatter>
                    <named-formatter name="COLOR-PATTERN"/>
                </formatter>
            </console-handler>
            <size-rotating-file-handler name="FILE" autoflush="true">
                <formatter>
                    <named-formatter name="PATTERN"/>
                </formatter>
                <file relative-to="jboss.server.log.dir" path="server.log"/>
                <rotate-size value="10M"/>
            </size-rotating-file-handler>

            <logger category="de.housekeepingbook">
                <level name="TRACE"/>
            </logger>
            <root-logger>
                <level name="INFO"/>
                <handlers>
                    <handler name="CONSOLE"/>
                    <handler name="FILE"/>
                </handlers>
            </root-logger>
            <formatter name="PATTERN">
                <pattern-formatter pattern="%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p [%c] (%t) %s%E%n"/>
            </formatter>
            <formatter name="COLOR-PATTERN">
                <pattern-formatter pattern="%K{level}%d{HH:mm:ss,SSS} %-5p [%c] (%t) %s%E%n"/>
            </formatter>
        </subsystem>

Thanks in advance!

Upvotes: 5

Views: 13182

Answers (3)

Andrea Volani
Andrea Volani

Reputation: 111

I had the same problem since jboss / wildfly doesn't provide a wrapper for log4j2 to logging subsystem (but has for log4j1 and slf4j).

So my solution was add the log4j-to-slf4j adapter to my classpath, see here for details:

Log4j to SLF4J Adapter

So now i can manage logging direclty from the server configuration.

Note 1: Pay attention to NOT include in the classpath also the reverse adapter SLF4J bridge (log4j-slf4j-impl-2.x.jar) or you will go in a infinite loop between the adapters!

Note 2: As described in the docs, the adapter may cause some loss of performance due to the format conversion.

Upvotes: 5

Johannes
Johannes

Reputation: 868

Your console logger output goes through the console handler of the logging module within WildFly and it prefixes the output from your logging framework with that part you want gone (time stamp, task etc.).

I was struggling with that, too, I still would love to know how this can be configured per-deployment.

Although I excluded the WildFly logging system in my jboss-deployment-structure.xml like this:

<?xml version="1.0" encoding="UTF-8"?>

<jboss-deployment-structure xmlns="urn:jboss:deployment-structure:1.2">
    <deployment>
        <exclude-subsystems>
            <subsystem name="logging"/>
        </exclude-subsystems>
    </deployment>
</jboss-deployment-structure>

It still uses the console handler formatter pattern of the logging subsystem. Why? Could this be a bug?

You can still change this formatter pattern globally though within your standalone.xml or via management console like this:

  1. Open the WildFly management console (localhost:9990)
  2. Go to ConfigurationCore (under Subsystems) – Logging and click on Handler
  3. Click Edit under Details to edit the settings of CONSOLE
  4. This is probably the formatter pattern you want: %s%E%n
  5. Paste it into Formatter's input box and click save

The effect is immediate, no need to restart. You could also add a new console handler, if you want to preserve the pattern.

About the pattern sting:

%s is the actual message from your web application.

"The %E in the default pattern is for a Throwable. Meaning it will print the stacktrace if an exception is being logged.” (James Perkins, https://developer.jboss.org/message/648961#648961)

%n does a line break.

If someone knows how to do it per-deployment, please let us know.

Upvotes: 5

Federico Sierra
Federico Sierra

Reputation: 5208

If you choose log4j.xml (log4j2.xml) instead of using the logging subsystem configuration, you have add some configuration in your application.

Create a jboss-deployment-structure.xml with the following content and place it in the META-INF/ directory if you are deploying an EAR or in either the META-INF/ or WEB-INF/ directory if you are deploying a WAR.

<jboss-deployment-structure xmlns="urn:jboss:deployment-structure:1.2">
    <deployment>
        <!-- Exclusions allow you to prevent the server from automatically adding some dependencies -->
        <exclusions>
            <module name="org.apache.log4j" />
        </exclusions>
    </deployment>
</jboss-deployment-structure>

REF: How do I use log4j.properties or log4j.xml instead of using the logging subsystem configuration?

Upvotes: 2

Related Questions