Logback on Jboss duplicates prefixes and new lines

2019-01-25 20:24发布

问题:

I'm working on java web project. I use Wildfly 10. I want to use it with logback. I did some configuration:

pom.xml

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.24</version>
</dependency>
<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
    <version>1.2.1</version>
</dependency>
<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-core</artifactId>
    <version>1.2.1</version>
</dependency>

logback.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="true">
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender" >
        <encoder>
            <pattern>[%date] [%thread] [%-5level] [%logger{36}] - %msg%n </pattern>
        </encoder>
    </appender>

    <logger name="com.pr" level="debug" additivity="false">
        <appender-ref ref="STDOUT" />
    </logger>

  <root level="debug">
    <appender-ref ref="STDOUT" />
  </root>
</configuration>

jboss-deployment-structure.xml

<jboss-deployment-structure>
    <deployment>
        <exclude-subsystems>
            <subsystem name="logging" />
        </exclude-subsystems>
</deployment>
</jboss-deployment-structure>

The problem is that I was expecting ouput like:

[2017-02-26 12:32:23,671] [ServerService Thread Pool -- 179] [DEBUG] [o.springframework.jndi.JndiTemplate] - Looking up JNDI

But I'm getting:

12:32:23,671 INFO [stdout] (ServerService Thread Pool -- 179) [2017-02-26 12:32:23,671] [ServerService Thread Pool -- 179] [DEBUG] [o.springframework.jndi.JndiTemplate] - Looking up JNDI

So it looks like jboss is adding something at the beginning. How to prevent it ?

回答1:

WildFly wraps both System.out and System.err in a logger. If you want to use an appender or handler that writes to either stream you need to either use the java.io.FileDescriptor.out (or err) or you need to create a logger category for stdout or stderr as well as a new console-handler to assign to the logger.

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

The above CLI script should remove the default pattern from the logger stdout.