Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 800
Dennis G. Avatar asked Oct 11 '14 09:10

Dennis G.


People also ask

Does JBoss logging use Log4j?

JBoss AS uses log4j as logging framework.

What does WildFly use for logging?

By default WildFly uses a periodic log handler and will create a log for every calendar day. This is different than WebSphere or Jboss, and may not be the desired format for many customers. Luckily WildFly has highly customizable logging. Open the WildFly Admin Console (http://<host>:9990/console by default).

How do I change the logging level in JBoss?

Note: You need not restart the site for JBoss to pick up these changes. To change log levels as a root user, perform the following: To enable debug logging, run the following command: /subsystem=logging/root-logger=ROOT:change-root-log-level(level=DEBUG)


2 Answers

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.

like image 66
Johannes Avatar answered Nov 15 '22 08:11

Johannes


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.

like image 37
Andrea Volani Avatar answered Nov 15 '22 09:11

Andrea Volani