0
我想學習log4j版本2.x. log4j屬性使用xml完成。下面給出的代碼在eclipse STS的maven項目中。它不會將任何日誌消息輸出到控制檯或文件中。Log4j沒有登錄到文件或控制檯
我不知道如何調試。谷歌搜索沒有給我任何日食的答案。請建議我如何自己調試並修復它。我不需要馬上得到完整的答案,除非我的大部分代碼都是錯誤的。
代碼:
package com.api.log4j;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class LoggingExample {
static Logger logger = LogManager.getLogger(LoggingExample.class);
private void loggerLevel(String message) {
if (logger.isDebugEnabled()) {
logger.debug("This is set to debug: " + message);
}
if (logger.isInfoEnabled()) {
logger.info("This is set to info: " + message);
}
logger.warn("This is set to warn: " + message);
logger.error("This is set to error: " + message);
logger.fatal("This is set to fatal: " + message);
}
public static void main(String[] args) {
System.out.println("Running main...");
LoggingExample loggingExample = new LoggingExample();
loggingExample.loggerLevel("calling the loggerLevel method...");
}
}
Log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<Console name="Console" target="System.out">
<PatternLayout>
[%-5level]<!-- Use 5 chars to show the level text. If level text < 5
chars, then append spaces to make it 5 chars. -->
%d{yyyy-MM-dd HH:mm:ss.SSS}<!-- The date. -->
[%t]<!-- Which thread is running. -->
%c{1}<!-- Class being logged. -->
- %msg%n
</PatternLayout>
</Console>
<File name="File" filename="/src/main/resources/log4j-file.txt">
<PatternLayout>
[%-5level]<!-- Use 5 chars to show the level text. If level text < 5
chars, then append spaces to make it 5 chars. -->
%d{yyyy-MM-dd HH:mm:ss.SSS}<!-- The date. -->
[%t]<!-- Which thread is running. -->
%c{1}<!-- Class being logged. -->
- %msg%n
</PatternLayout>
</File>
</Appenders>
<Loggers>
<Root>
<AppenderRef ref="Console" /> <!-- Appends only to console appender -->
</Root>
<Logger name="com.api.log4j.LoggingExample" level="debug"
additivity="false">
<AppenderRef ref="File" /> <!-- Appends only to file appender -->
</Logger>
</Loggers>
</Configuration>
你可以分享你的項目結構嗎,就像在哪裏是xml文件一樣。 – user1211