snark
snark

Reputation: 2897

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.)

Upvotes: 7

Views: 8339

Answers (1)

DwB
DwB

Reputation: 38300

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);
    }
}

Upvotes: 11

Related Questions