我正在使用 log4j 将错误记录到文件中,但是,当我运行程序时,我不断收到以下错误,尽管日志文件已创建并且错误已记录在文件中,但仅当我退出时程序
错误
log4j:ERROR Could not read configuration file [log4j.properties].
java.io.FileNotFoundException: log4j.properties (No such file or directory)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:131)
at java.io.FileInputStream.<init>(FileInputStream.java:87)
at org.apache.log4j.PropertyConfigurator.doConfigure(PropertyConfigurator.java:372)
at org.apache.log4j.PropertyConfigurator.configure(PropertyConfigurator.java:403)
at javafxapplication4.JavaFXApplication4.init(JavaFXApplication4.java:227)
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:788)
at com.sun.javafx.application.LauncherImpl.access$000(LauncherImpl.java:56)
at com.sun.javafx.application.LauncherImpl$1.run(LauncherImpl.java:158)
at java.lang.Thread.run(Thread.java:724)
log4j:ERROR Ignoring configuration file [log4j.properties].
代码:
PropertyConfigurator.configure("log4j.properties");
...
catch(SQLException e)
{
System.out.println("Error on Database connection");
System.out.print(e.getMessage());
logger.error("Your log message", e);
}
配置文件:
log4j.rootLogger=debug, stdout, R
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=/log/example.log
log4j.appender.R.MaxFileSize=100KB
# Keep one backup file
log4j.appender.R.MaxBackupIndex=1
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n
最佳答案
这是因为在类路径中找不到 log4j.properties
文件,请将其文件夹包含在类路径中或提供绝对路径。
关于java - 我使用 log4j 收到异常消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20717270/