18

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.

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
Anton K.
  • 933
  • 3
  • 9
  • 22
  • See also http://stackoverflow.com/a/5903253/127971 => use slf4j + logback, and two different logback configuration files; one for test, one for main – michael Mar 15 '14 at 03:01

4 Answers4

18

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

14

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.

Sam
  • 334
  • 3
  • 7
  • 1
    Also, it doesn't need to be simplerlogger.properties, it worked when I put this in my application.properties file – user1751825 Feb 08 '21 at 03:19
6

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>
Jean-Philippe Briend
  • 3,455
  • 29
  • 41
0

This might help: sysout-over-slf4j

Alex K.
  • 3,294
  • 4
  • 29
  • 41
  • 8
    This answers the wrong question. The OP asked how to write slf4j messages to System.out. sysout-over-slf4j sends messages written to System.out to the slf4j framework. These are 2 different things. Correct answer is by Jean-Philippe Briend – Dave B Oct 25 '16 at 09:58