【问题标题】:I get exception message using log4j我使用 log4j 收到异常消息
【发布时间】:2013-12-21 08:53:34
【问题描述】:

我正在使用 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

【问题讨论】:

  • 请显示项目层次结构。谢谢
  • 在我的编辑中查看层次结构

标签: java exception log4j


【解决方案1】:

这是因为log4j.properties 文件在类路径中找不到,要么在类路径中包含其文件夹,要么提供绝对路径。

【讨论】:

    【解决方案2】:

    您甚至不需要设置 log4j.properteis 文件。如果它在根文件夹中,Log4j 会自行找到它。

    【讨论】:

    • 我应该删除 PropertyConfigurator.configure("log4j.properties");,但是它没有记录
    • 尝试使用记录器而不是使用 PropertyConfigurator.configure("log4j.properties");
    • 我正在使用 osx 和 linus,但目前正在 osx 上测试
    • 我对你的结构有点困惑,但我认为你应该将你的属性文件移动到父文件夹。
    • 我没有注释 // PropertyConfigurator.configure("log4j.properties");现在正在工作。但是为什么我退出程序后写入文件
    猜你喜欢
    • 2020-09-02
    • 2019-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-05
    • 2019-12-13
    • 1970-01-01
    相关资源
    最近更新 更多