Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 473
user840930 Avatar asked Dec 02 '13 12:12

user840930


2 Answers

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");
like image 145
Chris Mantle Avatar answered Sep 30 '22 00:09

Chris Mantle


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>
like image 28
jos Avatar answered Sep 30 '22 01:09

jos