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
I expect to see 2 output log files: MyGooApp.log
and MyFooApp.log
. However I cannot find them anywhere.
Update
I have tried this:
log4j.appender.sift.appender.file=./$\\{session_id\\}.log
and yet I see now output files:
There are multiple problems here.
First move your log4j.properties from root of project to "
src/resources
". Then you will see a world of relevant errors.Log4j jar does not have this package
org.apache.log4j.sift
, add supporting pax-logging jar to your classpath.It will also help to use either Karaf OSGi MDC or Camel MDCbecause Apache Karaf uses Pax Logging as logging system. (I am not sure how it will work without the OSGi though)
Your problem is not with this line
log4j.appender.sift.appender.file=./$\\{session_id\\}.log
, as far as the path goes./$\\{session_id\\}.log
should create the log file in your project root once you have everything else configured properly.I have done it using logback. I am now sharing with you.
SiftExample.java
logback.xml
Output:
MyGooApp.log
MyFooApp.log
N.B: Please don't forget to do that
chapters.appenders.sift
is added in logback.xml as<logger name="chapters.appenders.sift" level="debug" ...
It seems like you are using a relative path as filename (
log4j.appender.sift.appender.file=$\\{session_id\\}.log
).If you set the log level for log4j to debug you should see within the generated logs a statement as follows:
You can enable log4j internal logging by defining the log4j.configDebug variable. All log4j internal debug calls go to System.out where as internal error messages are sent to System.err. All internal messages are prepended with the string "log4j: ".
As test you could also change your file property into something like:
.
represents the current folder (usually the project root folder).