下面是 Log4j 记录器的代码。我不得不说,很多人在另一个问题上给了我帮助。但我认为既然这是一个新问题,我会打开一个。
我的问题是为什么会出现这些错误?我的所有配置都正确吗?如何添加附加程序。我认为我在某个地方遗漏了一些东西,但我不确定它是什么。
代码:
package TestMenu;
import org.apache.log4j.Logger;
import javax.swing.*;
public class TestLogs {
private static Logger logger = Logger.getLogger(TestLogs.class.getName());
public TestLogs() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception ex) {
}
}
private void LogTester() {
logger.info("It works");
}
public static void main(String[] args) {
TestLogs tls = new TestLogs();
tls.LogTester();
}
}
错误:
log4j:WARN No appenders could be found for logger (TestMenu.TestLogs).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
属性文件:
# Root logger
log4j.rootLogger=INFO, file, stdout
# Write to file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File="C:\Users\itpr13266\Desktop\test.log"
log4j.appender.file.MaxFileSize=50MB
log4j.appender.file.MaxBackupIndex=1
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
# Write to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
最佳答案
Log4j 似乎找不到您的配置文件。您是否尝试过使用以下参数运行应用程序:
-Dlog4j.configuration=file:config/log4j.properties
以便 Log4j 使用系统属性 log4j.configuration
找到您的配置文件("config/log4j.properties"
):
或者,您也可以将此配置文件放在类路径中,以便 Log4j 找到它,例如放在源目录中:
否则,这是 Log4j 文档的解释或此错误消息:
This occurs when the default configuration files log4j.properties and log4j.xml can not be found and the application performs no explicit configuration. log4j uses Thread.getContextClassLoader().getResource() to locate the default configuration files and does not directly check the file system. Knowing the appropriate location to place log4j.properties or log4j.xml requires understanding the search strategy of the class loader in use. log4j does not provide a default configuration since output to the console or to the file system may be prohibited in some environments. Also see FAQ: Why can't log4j find my properties in a J2EE or WAR application?
关于java - 为什么我会收到我收到的错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23616254/