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

折月煮酒 提交于 2019-12-22 06:11:17

问题


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


回答1:


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


来源:https://stackoverflow.com/questions/18854797/slf4j-with-simplelogger-is-it-possible-to-log-to-a-file-and-system-out

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!