Where does the slf4j log file get saved?

user2763361 picture user2763361 · Feb 19, 2014 · Viewed 110.9k times · Source

I have the followed imports:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

and the following instantiation:

private static Logger logger = LoggerFactory.getLogger(Test.class);

and the following in my Main method:

logger.info("SOME MESSAGE: ");

However, I'm not able to find the output anywhere. All I see is that in my console there is:

21:21:24.235 [main] INFO  some_folder.Test - SOME MESSAGE: 

How do I locate the log file?

Note that the following are on my build path:

slf4j-api-1.7.5.jar

slf4j-log4j12-1.6.4.jar

I read the answer to similar questions but nobody actually says how to fix the problem.

Answer

Arnaud Denoyelle picture Arnaud Denoyelle · Feb 19, 2014

slf4j is only an API. You should have a concrete implementation (for example log4j). This concrete implementation has a config file which tells you where to store the logs.

enter image description here

When slf4j catches a log messages with a logger, it is given to an appender which decides what to do with the message. By default, the ConsoleAppender displays the message in the console.

The default configuration file is :

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">

  <Appenders>
    <!-- By default => console -->
    <Console name="Console" target="SYSTEM_OUT">
      <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
    </Console>
  </Appenders>

  <Loggers>
    <Root level="error">
      <AppenderRef ref="Console"/>
    </Root>
  </Loggers>
</Configuration>

If you put a configuration file available in the classpath, then your concrete implementation (in your case, log4j) will find and use it. See Log4J documentation.

Example of file appender :

<Appenders>
<File name="File" fileName="${filename}">
  <PatternLayout>
    <pattern>%d %p %C{1.} [%t] %m%n</pattern>
  </PatternLayout>
</File>

...
</Appenders>

Complete example with a file appender :

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">

  <Appenders>
    <File name="File" fileName="${filename}">
      <PatternLayout>
        <pattern>%d %p %C{1.} [%t] %m%n</pattern>
      </PatternLayout>
    </File>
  </Appenders>

  <Loggers>
    <Root level="error">
      <AppenderRef ref="File"/>
    </Root>
  </Loggers>

</Configuration>