登录时,Jboss 上的 Logback 会重复前缀和新行 [英] Logback on Jboss duplicates prefixes and new lines when logging
问题描述
我正在开发 Java Web 项目.我使用 Wildfly 10.我想将它与 logback 一起使用.我做了一些配置:
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
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.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 线程池 -- 179] [DEBUG] [o.springframework.jndi.JndiTemplate] - 查找 JNDI
[2017-02-26 12:32:23,671] [ServerService Thread Pool -- 179] [DEBUG] [o.springframework.jndi.JndiTemplate] - Looking up JNDI
但我得到:
12:32:23,671 INFO [stdout] (ServerService 线程池 -- 179) [2017-02-26 12:32:23,671] [ServerService 线程池 -- 179] [DEBUG] [o.springframework.jndi.JndiTemplate] - 查找 JNDI
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
所以看起来 jboss 在开头添加了一些东西.如何预防?
So it looks like jboss is adding something at the beginning. How to prevent it ?
推荐答案
WildFly 将 System.out
和 System.err
包装在一个记录器中.如果您想使用写入任一流的附加程序或处理程序,您需要使用 java.io.FileDescriptor.out
(或 err
),或者您需要创建stdout
或 stderr
的记录器类别以及分配给记录器的新 console-handler
.
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)
上面的 CLI 脚本应该从记录器 stdout
中删除默认模式.
The above CLI script should remove the default pattern from the logger stdout
.
standalone.xml 中的对应表示如下:
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>
这篇关于登录时,Jboss 上的 Logback 会重复前缀和新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!