Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect System.out and System.err to slf4j

I needed to redirect System.out/err.println outputs to slf4j.

I know that this is not the way to do logging properly but there is an external library, which logs to System.out

like image 875
itshorty Avatar asked Jun 25 '12 10:06

itshorty


People also ask

Can system ERR can be redirected to a file?

Generally, stderr and stdout both send data to the console, whatever that is. However, stdout and stderr can be redirected to different places. For instance, output can be redirected to a file while error messages still appear on the console. Finished programs shouldn't have much need for System.

Can SLF4J work without log4j?

Conclusion. So essentially, SLF4J does not replace Log4j, Both work together. SLF4j removes the tight coupling between the application and logging frameworks. It makes it easy to replace with any other logging framework in the future with a more capable library.

Is SLF4J same as log4j?

Comparison SLF4J and Log4j Unlike log4j, SLF4J (Simple Logging Facade for Java) is not an implementation of logging framework, it is an abstraction for all those logging frameworks in Java similar to log4J. Therefore, you cannot compare both.

What does log4j over SLF4J do?

log4j-over-slf4j. SLF4J ship with a module called log4j-over-slf4j. It allows log4j users to migrate existing applications to SLF4J without changing a single line of code but simply by replacing the log4j. jar file with log4j-over-slf4j.


1 Answers

You can use sysout-over-slf4j.

The sysout-over-slf4j module allows a user to redirect all calls to System.out and System.err to an SLF4J defined logger with the name of the fully qualified class in which the System.out.println (or similar) call was made, at configurable levels.

If you are not using Maven, download the jar and add it to your classpath.

Alternatively, add it as a Maven dependency:

<dependency>     <groupId>uk.org.lidalia</groupId>     <artifactId>sysout-over-slf4j</artifactId>     <version>1.0.2</version> </dependency> 

Then at application startup, call:

SysOutOverSLF4J.sendSystemOutAndErrToSLF4J(); 
like image 166
flavio.donze Avatar answered Sep 22 '22 08:09

flavio.donze