Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
490 views
in Technique[技术] by (71.8m points)

java - Logback on Jboss duplicates prefixes and new lines when logging

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 ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

The corresponding representation in standalone.xml looks like this:

<console-handler name="stdout" autoflush="true">
  <level name="ALL"/>
  <formatter>
    <pattern-formatter pattern="%s%n"/>
  </formatter>
</console-handler>
<logger category="stdout" use-parent-handlers="false">
  <level name="ALL"/>
  <handlers>
    <handler name="stdout"/>
  </handlers>
</logger>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...