How to write error log or exception into file in j

2019-02-01 11:39发布

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?

5条回答
兄弟一词,经得起流年.
2楼-- · 2019-02-01 11:43

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

查看更多
姐就是有狂的资本
3楼-- · 2019-02-01 11:43
try {
       System.setErr(new PrintStream(new FileOutputStream(System.getProperty("user.home")+"/error.log")));
} catch (FileNotFoundException ex) {
        ex.printStackTrace();
}

Now all error output is written into this file

查看更多
我欲成王,谁敢阻挡
4楼-- · 2019-02-01 11:43

Using log4j you can log exceptiosn quite easily:

try {
    ...
} catch(Exception e) {
    log.error("An exception! Oops!", e);
}
查看更多
走好不送
5楼-- · 2019-02-01 11:46

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.

查看更多
老娘就宠你
6楼-- · 2019-02-01 12:07

Look this tutorial about "File Appender"

See official Log4j short introduction and the "Configuration" section.

You can also make search about "RollingFileAppender" or "File appender".

You configure your logger to send its message to an appender. This appender can forward message towards the console (stdin), towards a file (FileAppender, RollingFileAppender...)...

Use this to perform error log:

try{
    throw new Exception("bla bla bla...");
} catch( Exception e ){
     // log without stack trace
     mLogger.error("Your log message");

     // log with stack trace
     mLogger.error("Your log message", e);
}
查看更多
登录 后发表回答