How to make a Logger for System.out
Asked Answered
A

4

20

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.

Aguila answered 22/12, 2011 at 13:6 Comment(1)
See also https://mcmap.net/q/662585/-alternate-slf4j-binding-or-config-for-unit-test => use slf4j + logback, and two different logback configuration files; one for test, one for mainAfternoons
F
21

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

Forbis answered 10/10, 2015 at 12:28 Comment(1)
Link is 404....Ectogenous
E
15

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.

Eardrop answered 7/2, 2016 at 14:9 Comment(1)
Also, it doesn't need to be simplerlogger.properties, it worked when I put this in my application.properties fileSteverson
O
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>
Organology answered 22/12, 2011 at 13:17 Comment(1)
I think logback (or log4j) is overkill for this purpose. slf4j-simple is intended for this use case.Cornet
P
0

This might help: sysout-over-slf4j

Polyphagia answered 22/12, 2011 at 13:15 Comment(1)
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 BriendRingsmuth

© 2022 - 2024 — McMap. All rights reserved.