简体   繁体   English

Java独立应用程序日志记录异常(log4j)

[英]Java standalone application logging exceptions (log4j)

I have a standalone java application. 我有一个独立的Java应用程序。 Application is using couple of libraries and it's managed by maven and 'shade' plugin to create one-big-jar with all the dependencies. 应用程序使用几个库,它由maven和'shade'插件管理,以创建具有所有依赖项的一个大jar。 I have a problem with logging uncaught exceptions into a file (application is ran on linux). 将未捕获的异常记录到文件中时遇到问题(应用程序在linux上运行)。 I configured log4j.properties and added rolling file appender. 我配置了log4j.properties并添加了滚动文件appender。 It's working but whenever exception is thrown it's printed on console rather in the configured log file. 它正在工作,但每当抛出异常时,它都会打印在控制台而不是配置的日志文件中。 Basically I'm ending with log file with all the INFO+ entries but no exceptions (uncaught). 基本上我以包含所有INFO +条目的日志文件结束,但没有例外(未捕获)。

Here is the log4j.properties: 这是log4j.properties:

# Root logger option
log4j.rootLogger=INFO, file

# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=logs/my-app.log
log4j.appender.file.MaxFileSize=100MB
log4j.appender.file.MaxBackupIndex=10
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

I running application using: 我运行应用程序使用:

java -jar -Dlog4j.configuration=file:log4j.properties myApp.jar

Currently I bypassed the problem using console appender and running application using: 目前我使用控制台appender和使用以下命令运行应用程序来绕过问题:

java -jar -Dlog4j.configuration=file:log4j.properties myApp.jar >> logs/my-app.log 2>&1

...but I'd really would like to use rolling file appender. ...但我真的想使用滚动文件appender。

Found a solution for such a problem. 找到了解决这个问题的方法。 It's quite easy. 这很容易。 You need to add DefaultUncaughtExceptionHandler in you 'main' class. 您需要在'main'类中添加DefaultUncaughtExceptionHandler。 Here is the snippet: 这是片段:

    Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {

        @Override
        public void uncaughtException(Thread t, Throwable e) {
            logger.error("Uncaught exception", e);
        }
    });

对于异常,您不应使用e.printStackTrace()而应使用logger.fatal(e.getStrackTrace())。

If the exceptions are uncaught then they won't get logged by log4j at all. 如果异常未被捕获,则log4j根本不会记录它们。 So I think a redirection is very useful, regardless. 所以我认为重定向非常有用,无论如何。

Note that if (say) you trigger a thread stack trace via a kill -3, that comes out on stderr and consequently I would always catch stderr / out and write it to a log file distinct from the log4j file. 请注意,如果(比方说)你通过kill -3触发线程堆栈跟踪, 就是stderr ,因此我总是会捕获stderr / out并将其写入与log4j文件不同的日志文件中。

Since you can't guarantee two processes writing to the same log file will interleave correctly, I wouldn't log both the redirection and log4j to the same log file (as you're doing above). 由于您无法保证写入同一日志文件的两个进程将正确交错,因此我不会将重定向和log4j都记录到同一个日志文件中(就像您上面所做的那样)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM