我的log4j SiftAppender没有生成文件

时间:2016-04-13 14:48:17

标签: java logging log4j

我遵循了tutorial

我有这段代码,但没有写入日志文件。

我缺少什么?

这是我的codehttps://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

1 个答案:

答案 0 :(得分:0)

这里给出step by step tutorial。请点击链接。

Github项目链接:https://github.com/jbonofre/blog-mdc

UPDATE:

您错过了设置登录控制台的简单配置。您必须在BasicConfigurator.configure();课程中添加SiftExampleLog4j.java

<强> 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");
    }
}

控制台输出:

  

0 [main] ERROR com.waze.rr_logger.SiftExampleLog4j - example1

     

0 [main] ERROR com.waze.rr_logger.SiftExampleLog4j - example2

有关详情,请按照tutorial

进行操作