log4j2 不适用于 JAR
log4j2 not working for JAR
我正在尝试将 log4j2
与动态日志目录位置一起使用。我为此编写了代码:
DateFormat dateFormat = new SimpleDateFormat(Constants.DATE_FORMAT);
Date date = new Date();
String currentDir = System.getProperty("user.dir") + "/" + Constants.OUTPUT_FOLDER_NAME + "/" + dateFormat.format(date);
System.setProperty("logDir", currentDir);
final LoggerContext ctx = (LoggerContext)LogManager.getContext(false);
ctx.reconfigure();
log4j2.xml 看起来像:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="trace">
<Appenders>
<File name="file" fileName="${sys:logDir}/log.log">
<PatternLayout>
<Pattern>%d{ISO8601} [%t] %C %M - %p: %m%n</Pattern>
</PatternLayout>
</File>
<File name="report" fileName="${sys:logDir}/report.txt">
<PatternLayout>
<Pattern>%m%n</Pattern>
</PatternLayout>
</File>
<Console name="console" target="SYSTEM_OUT">
<PatternLayout>
<Pattern>%d{ISO8601} - %p: %m%n</Pattern>
</PatternLayout>
</Console>
</Appenders>
<Loggers>
<Logger name="Report" level="info" additivity="false">
<AppenderRef ref="report" level="INFO"/>
</Logger>
<Root level="info">
<AppenderRef ref="file" level="INFO" />
<AppenderRef ref="console" level="INFO" />
</Root>
</Loggers>
</Configuration>
当我在 Eclipse 中 运行 时,文件生成正常,但是当我 运行 我的程序的主要 class 使用包含编译的主要 [=26] 的 JAR 文件时=] 在 class 路径上为:
java -cp CLASSPATH -Dlog4j.configuration=file:XMLFILE ClassName args
它只将 'error' 消息打印到控制台。目录结构和文件也不会创建。我做错了什么?
谢谢!
显然 Logger 找不到您的配置。它是否在 JAR 中导出(使用存档浏览器查看)?试着找出为什么它不在它必须在的地方并更新你的程序。
必须使用 -Dlog4j.configurationFile
指定位置,而不是 -Dlog4j.configuration
。
见http://logging.apache.org/log4j/2.x/faq.html#config_location
我正在尝试将 log4j2
与动态日志目录位置一起使用。我为此编写了代码:
DateFormat dateFormat = new SimpleDateFormat(Constants.DATE_FORMAT);
Date date = new Date();
String currentDir = System.getProperty("user.dir") + "/" + Constants.OUTPUT_FOLDER_NAME + "/" + dateFormat.format(date);
System.setProperty("logDir", currentDir);
final LoggerContext ctx = (LoggerContext)LogManager.getContext(false);
ctx.reconfigure();
log4j2.xml 看起来像:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="trace">
<Appenders>
<File name="file" fileName="${sys:logDir}/log.log">
<PatternLayout>
<Pattern>%d{ISO8601} [%t] %C %M - %p: %m%n</Pattern>
</PatternLayout>
</File>
<File name="report" fileName="${sys:logDir}/report.txt">
<PatternLayout>
<Pattern>%m%n</Pattern>
</PatternLayout>
</File>
<Console name="console" target="SYSTEM_OUT">
<PatternLayout>
<Pattern>%d{ISO8601} - %p: %m%n</Pattern>
</PatternLayout>
</Console>
</Appenders>
<Loggers>
<Logger name="Report" level="info" additivity="false">
<AppenderRef ref="report" level="INFO"/>
</Logger>
<Root level="info">
<AppenderRef ref="file" level="INFO" />
<AppenderRef ref="console" level="INFO" />
</Root>
</Loggers>
</Configuration>
当我在 Eclipse 中 运行 时,文件生成正常,但是当我 运行 我的程序的主要 class 使用包含编译的主要 [=26] 的 JAR 文件时=] 在 class 路径上为:
java -cp CLASSPATH -Dlog4j.configuration=file:XMLFILE ClassName args
它只将 'error' 消息打印到控制台。目录结构和文件也不会创建。我做错了什么?
谢谢!
显然 Logger 找不到您的配置。它是否在 JAR 中导出(使用存档浏览器查看)?试着找出为什么它不在它必须在的地方并更新你的程序。
必须使用 -Dlog4j.configurationFile
指定位置,而不是 -Dlog4j.configuration
。
见http://logging.apache.org/log4j/2.x/faq.html#config_location