How to make a Logger for System.out

2019-06-15 18:39发布

I am wondering how to get org.slf4j.Logger for System.out. I know this is not good, but I need it for testing purposes.

Thank you so much.

5条回答
爷的心禁止访问
2楼-- · 2019-06-15 19:15

It is possible to use slf4j-simple and make it write to the standard output by setting a system property when the program starts:

System.setProperty("org.slf4j.simpleLogger.logFile", "System.out");

More information at http://www.slf4j.org/api/org/slf4j/impl/SimpleLogger.html

查看更多
贼婆χ
3楼-- · 2019-06-15 19:18

This might help: sysout-over-slf4j

查看更多
虎瘦雄心在
4楼-- · 2019-06-15 19:22

Put simplelogger.properties in your classpath and put this line in it:

org.slf4j.simpleLogger.logFile=System.out

This will cause SLF4J Simple Logger to log to Standard Output instead of Standard Error.

查看更多
虎瘦雄心在
5楼-- · 2019-06-15 19:32

Consider slf4j-simple, but it logs to stderr instead of stdout. It's the bare-minimum implementation. Source code browsable here: http://grepcode.com/file/repo1.maven.org/maven2/org.slf4j/slf4j-simple/1.6.1/org/slf4j/impl/SimpleLogger.java?av=f

查看更多
爱情/是我丢掉的垃圾
6楼-- · 2019-06-15 19:38

SLF4J is a logging facade. You need a logging implementation.

Nowadays, Logback is the recommanded logging framework.

To log to System.out, you have to use the ConsoleAppender in Logback configuration file.

Example :

<appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
  <target>System.out</target>
  <encoder>
    <pattern>%-40.40c [%5.5thread] %-5p %X - %m%n</pattern>
  </encoder>
</appender>
查看更多
登录 后发表回答