gpt4 book ai didi

java - 找不到 log4j siftAppender 输出日志

转载 作者:行者123 更新时间:2023-11-30 08:02:25 25 4
gpt4 key购买 nike

我关注了这个tutorial

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

我错过了什么?

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

我希望看到 2 个输出日志文件:MyGooApp.logMyFooApp.log。但是我到处都找不到它们。

更新

我试过这个:

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

但我现在看到了输出文件:

enter image description here

最佳答案

我已经使用 logback 完成了。现在分享给大家。

筛选示例.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>

输出:

MyGooApp.log

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

MyFooApp.log

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

注意:请不要忘记这样做

  1. 包名称 chapters.appenders.siftlogback.xml 中添加为 <logger name="chapters.appenders.sift" level="debug" ...

关于java - 找不到 log4j siftAppender 输出日志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36944733/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com