Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a Logger for System.out

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.

like image 622
Anton K. Avatar asked Dec 22 '11 13:12

Anton K.


People also ask

Why logger is better than system out?

logger allows you to log debugging information with a log level, later you can use that level as filtering criteria, I mean, you can disable messages belongs to one particular log level like you would be more concerned to see WARN messages than DEBUG messages in production.


3 Answers

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

like image 190
aditsu quit because SE is EVIL Avatar answered Oct 19 '22 22:10

aditsu quit because SE is EVIL


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.

like image 42
Sam Avatar answered Oct 20 '22 00:10

Sam


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>
like image 20
Jean-Philippe Briend Avatar answered Oct 19 '22 23:10

Jean-Philippe Briend