Elad Benda2 Elad Benda2 - 7 months ago 33
Java Question

my log4j SiftAppender doesn't generate files

I followed this tutorial

I have this code, but no log file was written.

What am i missing?

here is my code: https://github.com/elad2109/log4j_sift/blob/master/src/main/java/com/waze/rr_logger/SiftExampleLog4j.java

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

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

Github Project Link: https://github.com/jbonofre/blog-mdc


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