user840930
user840930

Reputation: 5578

org.slf4j.Logger logs to console, how do I log to file?

I am using the org.slf4j.Logger to log output. Output is going to console. How do I get logging logged to a log file?

 private static final Logger LOG = LoggerFactory.getLogger(ClassName.class );
 LOG.info("Logging output to console");

I am not using a log4j.properties file. I am assuming I will need one.

I added the following log4j.properties file and placed it in different parts of my eclipse project.

 # Define the file appender
 log4j.appender.FileAppender=org.apache.log4j.RollingFileAppender
 log4j.appender.FileAppender.File=logger.log
 log4j.appender.FileAppender.layout = org.apache.log4j.PatternLayout
 log4j.appender.FileAppender.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n

 # Direct all messages there
 log4j.rootLogger = INFO, FileAppender

I even used

 PropertyConfigurator.configure("log4j.properties");

But no logging file appears. log4j.properties doesn't seem to have an effect.

Upvotes: 13

Views: 48529

Answers (2)

jos
jos

Reputation: 1092

You can create a log4j.xml in the resource folder.

  1. Import log4j package in the class.
  2. Inside the class, instantiate a logger object using Logger.getLogger( ) static method.
  3. Instantiate layouts (readymade or user-defined) to be assigned to appenders.
  4. Instantiate appenders and assign desired layout to them by passing the layout object as parameter to their constructors.
  5. Assign the instatiated appenders to the Logger object by invoking its addAppender( ) method with desired appender as parameter.
  6. Invoke appropriate printing methods on Logger object to perform logging.

.

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

    <appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%p [%t] %c{1}.%M(%L) | %m%n" />
        </layout>
    </appender>
    <appender name="FILE" class="org.apache.log4j.rolling.RollingFileAppender">
        <rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
            <param name="fileNamePattern" value="/yourfolder/debug_%d{dd-MM-yy}.log" />

        </rollingPolicy>
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%p [%t] %c{1}.%M(%L) | %m%n" />
        </layout>

    </appender>
    <root>
        <level value="WARN" />
        <appender-ref ref="CONSOLE" />
        <appender-ref ref="FILE" />
    </root>
</log4j:configuration>

Upvotes: 2

Chris Mantle
Chris Mantle

Reputation: 6693

The simplest way, I think, is to define a FileAppender in a log4j.properties file:

# Define the file appender
log4j.appender.FileAppender=org.apache.log4j.RollingFileAppender
log4j.appender.FileAppender.File=[log filename].log
log4j.appender.FileAppender.layout = org.apache.log4j.PatternLayout
log4j.appender.FileAppender.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n

# Direct all messages there
log4j.rootLogger = INFO, FileAppender

Just replace [log filename] with some relevant filename. I think Log4j is able to automatically locate the file when you run the project from Eclipse if the file is in your project directory, but I'm not 100% sure. You can use PropertyConfigurator at the start of your application to tell Log4j where to find the properties file, e.g.:

PropertyConfigurator.configure("log4j.properties");

Upvotes: 6

Related Questions