Java Log exception

Source: Internet
Author: User
Tags exception handling html form log4j

On the exception, in the troubleshooting, the log for us is particularly important, a few days before the release of a tragic phenomenon, the release of the beta test, found that there is a nullpointexception, but the log only the class full name, but no stack information and code line number, Unable to locate which line to run out of, so can only change code, very egg pain. So the log related things to sweep Ray.

--------------------------------------------------------------------------------------------------------------- ------

Log System

log4j -The relatively successful log system that appeared earlier is log4j. The log system model (LOGGER/APPENDER/LEVEL) pioneered by log4j is effective and has been in the works ever since.

April (java.util.logging.* --jdk1.4 is the first JDK with a log system, short (June). It did not have the obvious advantage of defeating Log4j, but it created standard confusion-applications with different logging systems could not coexist harmoniously.

Logback --is a newer log system. It is a set of systems that log4j's authors have learned from years of experience. It is more convenient to use, more powerful, and higher performance. Logback cannot be used alone and must be used in conjunction with the log frame slf4j.

--------------------------------------------------------------------------------------------------------------- --------

References a description of Java log in an article.

In order to overcome the confusion caused by the coexistence of multiple logging systems, the "Log Framework" appears. The log framework itself does not provide logging functionality, it provides only the interface for log calls. Basically understandable, log4j as log information configuration, that is, where the log hit, how to play, and so on, and then open source Jar package provides access to the log file interface, used to and specific log system decoupling, such as commmon-logging and SLF4J, Provides a logfactory to obtain the specific log object, after obtaining the log object, it can be logged. Here are two common log frames

(1) Commons-logging,apache provides the log façade interface, mainly to avoid the program and specific log coupling. First you need to introduce Commons-logging.jar (either through Maven or any other method), and then you can use it.

<!--[if!supportlists]-->1. <!--[Endif]-->log logger = Logfactory.getlog (Testcommonslogging.class);

<!--[if!supportlists]-->2. <!--[Endif]-->logger.debug ("Test Debug");

(2) SLF4J and commons-logging functions are similar, the first to introduce Slf4j-api-1.6.4jar, and then, for example, to support log4j, the introduction of Slf4j-log4j12-1.6.4jar

Logger Logger = Loggerfactory.getlogger (Slf4jtest.class);

Logger.debug ("Test Debug"); <!--endfragment-->

--------------------------------------------------------------------------------------------------------------- --------

Several concepts of log4j:

(1) Layout: one-line log format, regular form

Org.apache.log4j.HTMLLayout (layout in HTML form),
Org.apache.log4j.PatternLayout (You can specify layout patterns flexibly),

%p: Output log information priority, i.e. Debug,info,warn,error,fatal,
%d: the date or time of the output log point of time, the default format is ISO8601, or the format can be specified thereafter, for example:%d{yyy MMM dd Hh:mm:ss,sss},

%r: The number of milliseconds to output from the application boot to the output of this log information
%c: The class that the output log information belongs to, usually the full name of the class in which it is located
%t: Output The name of the thread that generated the log event
%l: The location where the output log event occurs,

%x: The NDC (nested diagnostics environment) associated with the current line threads relative, especially in multiple client multi-threaded applications such as Java Servlets.
Percent%: output a "%" character
%F: The name of the file where the output log message was generated
%l: line number in output code
%M: The message specified in the output code, the resulting log specific information
%n: Output a carriage return line feed,

Org.apache.log4j.SimpleLayout (The level and information string that contains the log information),
Org.apache.log4j.TTCCLayout (contains information about the time, thread, category, and so on that the log was generated)

If you need thread information, you can add%t to get this information

(2) Appender: Position information of the output log

Org.apache.log4j.ConsoleAppender (console),
Org.apache.log4j.FileAppender (file),
Org.apache.log4j.DailyRollingFileAppender (a log file is generated every day),
Org.apache.log4j.RollingFileAppender (a new file is generated when the file size reaches the specified size),
Org.apache.log4j.WriterAppender (send log information to any specified place in streaming format)

(3) Level: Log output level, a total of five, debug/info/warn/error/fatal

--------------------------------------------------------------------------------------------------------------- --------

There is no exception stack information in the log:

JDK5 after the JVM has an optimization, that is, if an exception is thrown for a period of time, for performance reasons, the subsequent will not throw stacktrace information, of course, this feature can also be removed from the JVM's startup parameters:-xx:-O Mitstacktraceinfastthrow.

--------------------------------------------------------------------------------------------------------------- --------

About Throwable

(1) Getcause ()

Get the reason for this throw;

(2) GetMessage ()

Gets the descriptive information thrown, such as throw new Exception ("I was thrown out of the exception"),

This time the method returns the textual information inside the parentheses.

(3) Getstacktrace ()

Gets the stack information thrown, printstacktrace () is able to print stack information.

Logger.error ("I am Zhongyong", e); Output text information and exception stack information

Logger.error (e); Simply returns the name of the exception information, the stack information is not

--------------------------------------------------------------------------------------------------------------- --------

Abnormal transformation has been mentioned above, in fact, after the exception is caught, the exception to the new type of exception to throw, this is generally for the exception of the information more intuitive. Like what:

public void Run () throws myexception{

...

try{

...

}catch (IOException e) {

...

throw new MyException ();

}finally{

...

}

}

At the time of throw new MyException (), we recommend MyException (e), otherwise there will be abnormal eating.


--------------------------------------------------------------------------------------------------------------- --------

For Run-time exceptions, we do not use Try...catch to capture processing, but in the process development debugging phase, as far as possible to avoid this exception, once found that the exception, the correct approach will improve the program design code and implementation, modify the error in the program, so as to avoid this anomaly. Capturing and handling Run-time exceptions is a good solution, because this exception can be avoided by improving code implementations.

For the check exception, do not, honestly to follow the exception handling method to deal with, or use Try...catch to catch and solve, or with throws thrown.

For error (run-time error), do not need to do any processing in the program, after the problem, should be in the program outside the place to find problems, and then solve.

1, avoid too large try block, do not put the exception code into the try block, try to keep a try block corresponding to one or more exceptions.

2, the type of refinement of the exception, do not matter what type of exceptions are written excetpion.

3, catch block as far as possible to maintain a block to catch a class of exceptions, do not ignore the catch of the exception, caught after either processing, or translation, or to throw out the new type of exception.

4. Do not throw away the exception that you can handle.

5, do not use Try...catch to participate in the program process, the fundamental purpose of the exception control is to deal with the abnormal situation of the program.

--------------------------------------------------------------------------------------------------------------- --------

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.