Cannot find the log4j siftAppender output logs

偶尔善良 提交于 2019-12-10 23:55:24

问题


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:


回答1:


I have done it using logback. I am now sharing with you.

SiftExample.java

package chapters.appenders.sift;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.joran.JoranConfigurator;
import ch.qos.logback.core.joran.spi.JoranException;

public class SiftExample {
    public static void main(String[] args) throws JoranException {
        String configFile = "F://logbackfile/logback.xml";

        LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
        JoranConfigurator configurator = new JoranConfigurator();

        Logger logger = LoggerFactory.getLogger(SiftExample.class);
        logger.debug("Application started");

        lc.reset();
        lc.putProperty("appname", "MyGooApp");
        configurator.setContext(lc);
        configurator.doConfigure(configFile);
        logger.debug("Alice says hello");

        lc.reset();
        lc.putProperty("appname", "MyFooApp");
        configurator.setContext(lc);
        configurator.doConfigure(configFile);
        logger.debug("Bob says hi");
    }

    static void usage(String msg) {
        System.err.println(msg);
        System.err.println("Usage: java " + SiftExample.class.getName() + " configFile\n" + "   configFile a logback configuration file");
        System.exit(1);
    }
}

logback.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <property name="DEV_HOME" value="F://logbackfile" />
    <appender name="FILE-AUDIT"
        class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${DEV_HOME}/${appname}.log</file>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <Pattern>
                %d{yyyy-MM-dd HH:mm:ss} - %msg%n
            </Pattern>
        </encoder>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!-- rollover daily -->
            <fileNamePattern>${DEV_HOME}/archived/debug.%d{yyyy-MM-dd}.%i.log
                        </fileNamePattern>
            <timeBasedFileNamingAndTriggeringPolicy
                class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>10MB</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
        </rollingPolicy>
    </appender>

    <logger name="chapters.appenders.sift" level="debug"
        additivity="false">
        <appender-ref ref="FILE-AUDIT" />
    </logger>
    <root level="error">
        <appender-ref ref="FILE-AUDIT" />
    </root>
</configuration>

Output:

MyGooApp.log

2016-05-09 01:35:57 - Alice says hello

MyFooApp.log

2016-05-09 01:35:57 - Bob says hi

N.B: Please don't forget to do that

  1. Package name chapters.appenders.sift is added in logback.xml as <logger name="chapters.appenders.sift" level="debug" ...



回答2:


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:

setFile called: "+fileName+"

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:

log4j.appender.sift.appender.file=./$\\{session_id\\}.log

. represents the current folder (usually the project root folder).




回答3:


There are multiple problems here.

  1. First move your log4j.properties from root of project to "src/resources". Then you will see a world of relevant errors.

  2. Log4j jar does not have this package org.apache.log4j.sift, add supporting pax-logging jar to your classpath.

  3. 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.



来源:https://stackoverflow.com/questions/36944733/cannot-find-the-log4j-siftappender-output-logs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!