我已经配置了 sysout-over-slf4j
SysOutOverSLF4J.sendSystemOutAndErrToSLF4J()
System.out
和 System.err
不再显示在控制台上,因此它似乎确实在捕获它们。但是,它没有出现在我的任何记录器中。
更新回复:配置:
我试过各种配置,但这是我试过的厨房水槽:
<configuration>
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>/Users/arne/log/sandbox.log</file>
<append>true</append>
<encoder>
<pattern>%d\t%level\t[%thread]\t%logger{35}\t%msg%n</pattern>
</encoder>
</appender>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d [%thread] %-5level %logger{35} - %msg%n</pattern>
</encoder>
</appender>
<root level="DEBUG">
<appender-ref ref="STDOUT"/>
<appender-ref ref="FILE"/>
</root>
</configuration>
因为 SysOutOverSLF4J
似乎默认转到 INFO 和 ERROR,我认为 DEBUG 应该捕获所有。我只是用 TRACE 试了一下,但还是一无所获。我的简单测试如下所示:
object LoggerTest extends App with LazyLogging {
SysOutOverSLF4J.sendSystemOutAndErrToSLF4J()
logger.debug("to log")
System.out.print("to System.out")
System.err.print("to System.err")
}
产生这个输出:
2015-04-24 09:23:16,859 [main] DEBUG u.o.l.s.c.SysOutOverSLF4JInitialiser - Your logging framework class ch.qos.logback.classic.Logger should not need access to the standard println methods on the console, so you should not need to register a logging system package.
2015-04-24 09:23:16,869 [main] INFO u.o.l.s.context.SysOutOverSLF4J - Replaced standard System.out and System.err PrintStreams with SLF4JPrintStreams
2015-04-24 09:23:16,872 [main] INFO u.o.l.s.context.SysOutOverSLF4J - Redirected System.out and System.err to SLF4J for this context
2015-04-24 09:23:16,873 [main] DEBUG sandbox.LoggerTest$ - to log
Your logging framework class ch.qos.logback.classic.Logger should not need access to the
我认为以上应该是对我的问题的提示,因为我不知道该怎么做,因为没有 SysOutOverSLF4J
logback 不会捕获 STDOUT
或 STDERR
要么,这就是我首先使用它的原因。
最佳答案
SysOutOverSLF4J.sendSystemOutAndErrToSLF4J(LogLevel.DEBUG, LogLevel.ERROR);
它应该有效!
关于java - sysout-over-slf4j 捕获 System.out 但不写入 logback,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29836330/