我正在研究java web项目.我使用Wildfly 10.我想将它与logback一起使用.我做了一些配置:
的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的部署,structure.xml
<jboss-deployment-structure>
<deployment>
<exclude-subsystems>
<subsystem name="logging" />
</exclude-subsystems>
</deployment>
</jboss-deployment-structure>
问题是我期待输出像:
[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 Thread Pool — 179) [2017-02-26 12:32:23,671] [ServerService Thread Pool — 179] [DEBUG] [o.springframework.jndi.JndiTemplate] – Looking up JNDI
所以看起来jboss在开头添加了一些内容.怎么预防呢?
解决方法:
WildFly将System.out和System.err包装在记录器中.如果要使用写入任一流的appender或处理程序,则需要使用java.io.FileDescriptor.out(或err),或者需要为stdout或stderr以及新控制台创建记录器类别 – 分配给记录器的处理程序.
/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脚本应该从logger stdout中删除默认模式.