Reprint, original website: http://blog.csdn.net/wud_jiyanhui/article/details/6213443
The concept and function of system log
As an application service, the log system plays an important role in tracking debugging, program state record, and recovery of crash data .
Common Java Log System
Log4j
One of the earliest Java log frameworks, launched by the Apache Foundation, provides a flexible and powerful logging mechanism.
JDK1.4 Loggingframework
Following log4j, the JDK Standards Board absorbed LOG4J's basic ideas into the JDK, releasing the first log framework interface in JDK1.4.
Commonsloggingframwork
Provides a unified calling interface and configuration method, allowing Java projects to switch freely on the use of log4j and jdk1.4lloggingframework
Application of log4j
LOG4J is an Apache open source project, through the use of log4j, we can control the destination of the log information delivery, output format , by setting the level of log information can also be detailed control of the log generation process. log4j has three main components:loggers (logger), appenders (output source) and layouts (layout), which can be simply understood as the log category, where the log is to be exported and how the logs are exported. Combined with these three components, you can easily record the type and level of information, and control the style and location of the log output at run time
LOG4J Three core concepts
The public class Logger is responsible for most of the operations that handle logging.
The public interface Appender is responsible for controlling the output of the log records.
The public abstract class Layout is responsible for formatting the output of the Appender
1.Logger Logger is the core component of log processing
The loggers component is divided into five levels in this system: DEBUG, INFO, WARN, error, and fatal.
These five levels are sequential, debug<info<warn<error<fatal, respectively, to specify the importance of this log information, it is important to understand that, here log4j has a rule: Suppose loggers level is P, If a level q is higher than p in the loggers, it can be started, otherwise masked out. Assuming that the level you define is info, then the log for error and warn can be displayed and the debug information below him is not displayed.
2 special levels, all OFF
[Java] view Plaincopy
- Java Programs for example:
- Establish an instance of logger, named Com.foo?
- Logger Logger=logger.getlogger ("Com.foo"); "Com.foo" is an example
- Can be named, or any
- Sets the level of logger. The level of logger is usually not set in the program. Generally in the configuration text
- Set in the widget.
- Logger.setlevel (Level.info);
- Loggerbarlogger=logger.getlogger ("Com.foo.Bar");
- The following request is available because Warn>=info
- Logger.warn ("lowfuellevel.");
- The following request is not available because Debug<info
- Logger.debug ("startingsearchfornearestgasstation.");
- The instance named "Com.foo.bar" Barlogger inherits the instance? The level of Com.foo?
- Don't. Therefore, the following request is available because the Info>=info
- Barlogger.info ("locatednearestgasstation.");
- The following request is not available because Debug<info
- Barlogger.debug ("Exitinggasstationsearch");
The "whether available" here means that you can output logger information.
When naming logger instances, there are no restrictions and you can take any name you are interested in. In general, it is recommended to name the logger instance in the same location as the class, which is a more efficient logger naming method at present. This allows each class to establish its own log information for easy administration.
Like what:
static Logger Logger=logger.getlogger (ClientWithLog4j.class.getName ());
2.Appender Control log output location
The log4j log system allows you to output logs to different places, such as consoles, files, new files based on number of days or file sizes, streams to other places, and so on. Its syntax is expressed as:
Org.apache.log4j.ConsoleAppender (console)
Org.apache.log4j.FileAppender (file)
Org.apache.log4j.DailyRollingFileAppender (Generate a log file every day)
Org.apache.log4j.RollingFileAppender (creates a new file when the file size reaches the specified size)
Org.apache.log4j.WriterAppender (send log information in stream format to any specified location)
Smtpappender sending mail
Socketappender remote Day to server send log event Loggingevent object
The configuration is used in the following ways:
Log4j.appender.appendername=fully.qualified.name.of.appender.class
Log4j.appender.appendername.option1=value1
... log4j.appender.appendername.option=valuen
The Appender interface can also be implemented by itself, which provides considerable convenience for the output of the log.
3.Layout: format output log information
Appender must use a Layout associated with it in order to know how to format the output log information
LOG4J has three types of layout
HtmllayouT
Format log output as HTML table
Patternlayout
Formats the log output according to the specified conversion mode
Simplelayout
Format the log output in a very simple way
Configuring log4j with configuration Files
Log4j.xml configuration file
Log4j.properties configuration file
Typically, we provide a file named Log4j.properties, which, when first called to log4j, log4j in the Classpath (.. /web-inf/class/can also be placed in any other directory, as long as the directory is included in the classpath, to locate the file, and to read the configuration that the file completed. This configuration file tells log4j in what format, what information, and where to export.
2, Appenders
3, Layouts
Sometimes the user wants to format their own log output according to their preferences. Log4j can add layouts to the appenders to complete this function. Layouts provides four types of log output, such as HTML style, free-to-specify style, styles that contain log levels and information, and styles that contain information such as log time, threads, categories, and so on.
Its syntax is expressed as:
Org.apache.log4j.HTMLLayout (Layout in HTML table Form),
Org.apache.log4j.PatternLayout (flexibility to specify layout mode),
Org.apache.log4j.SimpleLayout (contains the level of log information and the information string),
Org.apache.log4j.TTCCLayout (contains information about the time, thread, category, etc.) of the log
The configuration is used in the following ways:
Log4j.appender.appendername.layout=fully.qualified.name.of.layout.class
Log4j.appender.appendername.layout.option1=value1
Log4j.appender.appendername.layout.option=valuen
LOG4J BASIC Programming method
The above is a schematic explanation of the use of log4j, in specific Java programming using log4j can refer to the following example:
1, Establish logger example:
Syntax representation: Publicstaticloggergetlogger (Stringname)
Actual use: Staticloggerlogger=logger.getlogger (ServerWithLog4j.class.getName ());
2. Read the configuration file:
After obtaining an instance of logger, the next step is to configure the LOG4J usage environment:
Syntax means:
Basicconfigurator.configure (): Automatically and quickly using the default log4j environment.
Propertyconfigurator.configure (Stringconfigfilename): reads a configuration file written using the Java properties file.
Domconfigurator.configure (Stringfilename): reads the configuration file in XML form.
Actual use:
Propertyconfigurator.configure ("Serverwithlog4j.properties");
3. Insert Log information
Once you have completed the above-mentioned steps, you can insert the log at a different level from anywhere you want to log.
Syntax means:
Logger.debug (objectmessage);//debug information
Logger.info (objectmessage);//General Information
Logger.warn (objectmessage);//warning message
Logger.error (objectmessage);//Error message
Logger.fatal (objectmessage);//Fatal error message
Actual use:
Logger.info ("serversocketbeforeaccept:" +server);
4.log4j configuration file
In the actual programming, in order to make the log4j really run in the system in advance also to define the configuration file. The defining step is to use the logger, Appender, and layout separately. LOG4J supports two configuration file formats, one in XML format and one javaproperties (key=value) "Java attribute file (key = value)".
-----------------------------------------------------------------------------------------
Printing parameters: log4j Format the log information in a print format similar to the printf function in C, as follows:
%M the message specified in the output code
%p output priority, i.e. Debug,info,warn,error,fatal
%r output the number of milliseconds that the log information is consumed from the application boot to output
%c output belongs to the class, which is usually the full name of the class
%t output The name of the thread that generated the log event
%n output a carriage return line break, Windows platform is "/r/n", UNIX platform is "/n"
%d the date or time of the output log time, the default format is ISO8601, can also be specified after the format, such as:%d{yyy MMM dd HH:mm:ss, SSS}, output similar: October 18, 2002 22:10:28, 921
%l where the output log event occurs, including the class name, the thread that occurred, and the number of rows in the code. Example: Testlog4.main (testlog4.java:10)
Log4j--java System log (reprint)