1 Introduction:
For a fan that just step into Java, or relatively lazy programmers, record logs may
It is a job that does not have a good job. I remember that the author just started learning Java, I was revising a senior.
The code left, there is a lot of record logs in this code, but for the first entry
I, think that which code is extra-awkward, so I deleted the code of all record logs, afterwards
I only know system.out.println () in the career, but it is no wonder that it is not
JDK1.4's logging package, no log4j, written log.java program feel than system.out.println ()
I can't do much, and it is very troublesome when transplantation. Until now I want to be a comparative OO web item
Type, I returned to the starting point, I realized the benefits of the log.
2. Why do you want a record log?
2.1 advantages
Has a huge help during debugging and maintenance;
Help for managing staff managed by a running application.
2.2 Disadvantages
No, if the log record is properly implemented.
There may be a little 啰, but it is definitely worth it.
2.3 Performance impact
It is generally negligible.
If you don't have a log of logs properly, you may have a decline in performance, such as setting a big log record.
File, display a large log message, etc..
3. Use of Common-Logging Introduction Use the interface to programming interfaces and configuration interfaces, how to use log facilities in software systems to log registration, and configure the operation of the interface planning log facility. 3.1 The reason for Common-logging for Common is that it is a universal log package that is packaged can be log4j, logkit, and specific log systems such as LOG in JSDK 1.4. In the end, the binding is mainly dependent on the binding search strategy of the configuration interface and Common-logging.
The log format, log target diversity features mainly rely on the capabilities and configuration of specific log systems.
3.2 Log Level
At the time of the record log, you must record the log information we need according to the level, such as information that requires a record debug, or system prompt information, or program operation, and record exception information and disciples.
Record the wrong stack information, etc.
The following is the level of various logs:
The Java 1.4 log record defines the following log level constant in the java.util.logging.Logging.Level class
Ø Server: indicates a serious failure. There will always be a companion of Throwable.
Ø Config: Designed to the message generated during application configuration.
Ø Info: Medium priority. Indicates what a component is doing (for example, monitoring a task)
Ø Run progress), but not used to help debug the component.
Ø FINE: Tracking information. The level and lower priority level should be used to help debug some classes, but should not be used to explain the way the application is operated.
Ø Finer: Detailed tracking information.
Ø Finest: Very detailed tracking information.
Log4j defines the following log level constants in the org.apache.log4j.level class
Ø Fatal: indicates a serious failure. Leading the discontinuation of the application.
Ø Error: Represents an error event. The Application can continue to run.
Ø Warn: indicates a potential danger.
Ø Info: Indicates that a component is doing important operation, and the information is relatively rough.
Ø Debug: Represents a detailed operation of a component to debug Application. Ø All: Represents a record of all levels of log information.
Common-logging log level is divided into 6 kinds, from low to heights for TRACE, Debug, Info, Warn, Error, Fatal.
Ø Fatal: indicates a serious failure. Leading the discontinuation of the application.
Ø Error: Represents an error event. The Application can continue to run.
Ø Warn: indicates a potential danger.
Ø Info: Indicates that a component is doing important operation, and the information is relatively rough.
Ø Debug: Represents a detailed operation of a component to debug Application.
Ø Trace: Very detailed tracking information, only used to record the log.
Part of the log level is embodied in the programming interface, and a part is reflected in the configuration interface. Each level has a corresponding convenience method, such as Fatal () and info (), allowing one to be assigned to a message through a method, and recording an exception. Each message must allocate a level in these log levels to ensure that the log record is unable to
It is easy to control when running. 3.3 Application Programming Interface
Common-logging application programming interface is mainly in org.apache.commons.logging.log interface
Definition, this interface mainly defines two types of operations:
A class is a level judgment that reduces the parameters calculation of unnecessary log operations to improve performance, function name
And parameters are as follows:
Log.isfatalenabled ();
Log.iserrorenabled ();
Log.isswarnenabled ();
Log.isinfoenabled ();
Log.IndebugeNabled ();
Log.IstraceNabled ();
The following code can explain well:
IF (log.Indebugeload ()) {
... some high cost operation ...
Log.debug (theresult);
}
If the level of log facilities are defined above Debug, these high cost operations can avoid running.
Another category is log registration, follow the level registration log information, the function name, and parameters as follows: log.fatal (Object Message);
Log.fatal (Object Message, Throwable T);
Log.Error (Object Message);
Log.Error (Object Message, Throwable T);
Log.warn (Object Message);
Log.warn (Object Message, Throwable T);
Log.Info (Object Message);
Log.info (Object Message, Throwable T);
Log.debug (Object Message);
Log.debug (Object Message, Throwable T);
Log.Trace (Object Message);
Log.Trace (Object Message, Throwable T);
Log registration operation is also two small classes: a parameter log information registration operation and two parameters log information
Interest registration operation. The former applies to three types of users, the latter used to print the error stack information of the log registration office,
So applies more to developers and maintenance. Log Technology Drop (2)