Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SLF4J with SimpleLogger: Is it possible to log to a file AND System.out?

I'm using the SimpleLogger binding for SLF4J 1.7.5. As per the docs I can use the org.slf4j.simpleLogger.logFile property in my simplelogger.properties file to specify a log file OR System.out OR System.err.

However I want to send log messages to BOTH System.out AND a log file. Does anyone know how to achieve this using SimpleLogger please? (I'm using Windows so cannot use tail -f simply to follow the log file in a console window; nor do I want to get a third party utility which emulates 'tail -f' in Windows.)

like image 428
snark Avatar asked Sep 17 '13 16:09

snark


People also ask

Does SLF4J use log4j internally?

SLF4J supports popular logging frameworks, namely log4j, java. util. logging, Simple logging and NOP. The logback project supports SLF4J natively.

How do you write log in SLF4J?

We create the Logger instance by using the LoggerFactory class and its getLogger method and providing the name of the class. That way we bind the logger to the class name which gives us the context of the log message. Once we have that, we can use the Logger methods to create LogRecord on a given level.

How does SLF4J work?

SLF4J standardized the logging levels, which are different for the particular implementations. It drops the FATAL logging level (introduced in Log4j) based on the premise that in a logging framework we should not decide when to terminate an application. The logging levels used are ERROR, WARN, INFO, DEBUG and TRACE.

What is SLF4J binding?

Bindings are basically implementations of a particular SLF4J class meant to be extended to plug in a specific logging framework. By design, SLF4J will only bind with one logging framework at a time. Consequently, if more than one binding is present on the classpath, it will emit a warning.


1 Answers

Short Answer

You can't do that with SimipleLogger.

More Answer

Give up on SimpleLogger and move on to something else. You have options:
  1. Instead of using slf4j-simple-1.x.x.jar get logback (logback-classic.jar and logback-core.jar). With logback you can define two appenders; one for the file output and one for console (also-known-as System.out) output.

  2. Instead of using slf4j-simple.1.x.x.jar get xxx (substitute any logging system supported by slf4j) and blah blah blah (do the same as in 1 obove).

  3. SLF4j is open source; derive your own logger (lets call it TeeLogger) that logs to System.out and a file.

  4. Create a logging class that that sits in front of SLF4j (in your application). Have it take a Logger and a messasge then have it write the message to System.out and the Logger.

  5. Something that I have not though about.

Here is a (super simplistic) example of #4 above:

import org.slf4j.Logger;

public class LoggyLoo
{
    public static void logZoreInfo(
        final Logger logger,
        final String message)
    {
        System.out.println(message);
        logger.info(message);
    }
}
like image 105
DwB Avatar answered Oct 09 '22 06:10

DwB