如何在java中把错误日志或异常写入文件[英] How to write error log or exception into file in java

本文是小编为大家收集整理的关于如何在java中把错误日志或异常写入文件的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

是否有任何方法可以将错误日志或异常写入Java中的文件.我经历了log4j.我谷歌搜索了它,但Diidt找到了一个很好的解决方案.我写了一个简单的代码

catch (Exception e) {
    PrintWriter pw = new PrintWriter(new FileOutputStream("Log"));     
    e.printStackTrace(pw);
} 

还有其他方法可以记录错误或异常吗?任何身体都可以为我提供wwith示例log4j?

的示例

推荐答案

首先阅读 log4j手册,这很容易配置滚动日志文件.您不必进行任何明确的文件操作.

#SET LEVEL of ROOT-LOGGER, you will like to have Debug in local, but in prod you may just want WARN and ABOVE. This setting is done here!
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. (basically, format of log)
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n

# THIS IS WHERE YOU WILL HAVE ALL THE LOG WRITTEN
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=/var/log/applogs/example.log

# Maximum size of log file, usually we keep 10MB
log4j.appender.R.MaxFileSize=100KB
# Keep one backup file, usually we keep 10
log4j.appender.R.MaxBackupIndex=1

log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n

第二,每当您发现异常时,都会喜欢

public class MyClass{

  private static Logger logger = Logger.getLogger(MyClass.class);

  public ReturnType myMethod(Param p, Param2 p2) {
    ....
    ....
    try {
      ..    
    } catch(MyException e) {
       logger.log("Exceptions happen!", e); //this will put all the details in log file configured earlier
    }
    ....
  }

  ....
}

值得阅读手册.最好阅读完成log4j手册

其他推荐答案

您可以将异常作为参数添加到log4j语句中,例如

catch(Exception e)
{
    logger.error("Unexpected error", e);
}

,如果您的文件Appender运行正常,则将输出异常的完整堆栈跟踪.

其他推荐答案

使用log4j您可以很容易地记录异常:

try {
    ...
} catch(Exception e) {
    log.error("An exception! Oops!", e);
}

本文地址:https://www.itbaoku.cn/post/1574788.html

问题描述

Is there any way to write error log or exception into a file in java. i have gone through Log4j. I googled about it but diidnt find a good solution. I have written a simple code

catch (Exception e) {
    PrintWriter pw = new PrintWriter(new FileOutputStream("Log"));     
    e.printStackTrace(pw);
} 

Is there any other way to log the errors or exception? can any body provide me wwith sample example of Log4j?

推荐答案

First read log4j Manual, it's easy to configure a rolling log file. You do not have to do any explicit file operations.

#SET LEVEL of ROOT-LOGGER, you will like to have Debug in local, but in prod you may just want WARN and ABOVE. This setting is done here!
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. (basically, format of log)
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n

# THIS IS WHERE YOU WILL HAVE ALL THE LOG WRITTEN
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=/var/log/applogs/example.log

# Maximum size of log file, usually we keep 10MB
log4j.appender.R.MaxFileSize=100KB
# Keep one backup file, usually we keep 10
log4j.appender.R.MaxBackupIndex=1

log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n

Second, whenever you catch an exception, do like this

public class MyClass{

  private static Logger logger = Logger.getLogger(MyClass.class);

  public ReturnType myMethod(Param p, Param2 p2) {
    ....
    ....
    try {
      ..    
    } catch(MyException e) {
       logger.log("Exceptions happen!", e); //this will put all the details in log file configured earlier
    }
    ....
  }

  ....
}

It worth reading the manual. Even better read Complete log4j Manual

其他推荐答案

You can add the exception as a parameter to your log4j statement, e.g.

catch(Exception e)
{
    logger.error("Unexpected error", e);
}

Provided you've got a file appender running OK, this will output the complete stack trace of the exception.

其他推荐答案

Using log4j you can log exceptiosn quite easily:

try {
    ...
} catch(Exception e) {
    log.error("An exception! Oops!", e);
}
相关标签/搜索