Saturday, December 2, 2017

log4j SiftAppender doesn't generate files. How to solve this issue ?

Question:

I followed this tutorial
I have this code, but no log file was written.
What am i missing?
    import org.apache.log4j.Logger;
    import org.slf4j.LoggerFactory;


public class SiftExampleLog4j {

    static org.apache.log4j.Logger logger = Logger.getLogger(SiftExampleLog4j.class);

    public void log() {
        BasicConfigurator.configure();

        org.apache.log4j.MDC.put("session_id","MyGooApp");

        logger.error("example1");

        org.apache.log4j.MDC.put("session_id","MyFooApp");

        logger.error("example2");
    }
}
log4j.properties
    log4j.rootLogger=INFO, sift, osgi:VmLogAppender

# Sift appender
log4j.appender.sift=org.apache.log4j.sift.MDCSiftingAppender
log4j.appender.sift.key=session_id
log4j.appender.sift.default=no_session_id
log4j.appender.sift.appender=org.apache.log4j.FileAppender
log4j.appender.sift.appender.layout=org.apache.log4j.PatternLayout
log4j.appender.sift.appender.layout.ConversionPattern=%d{ISO8601} | %-5.5p | %-16.16t | %-32.32c{1} | %m%n
log4j.appender.sift.appender.file=$\\{session_id\\}.log
log4j.appender.sift.appender.append=true

Answer:

step by step tutorial is given here. Please follow the link.

UPDATE:

You have missed to set up a simple configuration that logs on the console. You have to add BasicConfigurator.configure(); in SiftExampleLog4j.java class.
SiftExampleLog4j.java
package com.waze.rr_logger;

import org.apache.log4j.Logger;
import org.apache.log4j.BasicConfigurator;

public class SiftExampleLog4j {

    static org.apache.log4j.Logger logger = Logger
            .getLogger(SiftExampleLog4j.class);

    public void log() {
        // Set up a simple configuration that logs on the console.
        BasicConfigurator.configure(); // You have missed to add this

        org.apache.log4j.MDC.put("session_id", "MyGooApp");

        logger.error("example1");

        org.apache.log4j.MDC.put("session_id", "MyFooApp");

        logger.error("example2");
    }
}

Console Output:

0 [main] ERROR com.waze.rr_logger.SiftExampleLog4j - example1
0 [main] ERROR com.waze.rr_logger.SiftExampleLog4j - example2
For more, you can follow the tutorial


No comments:

Post a Comment