It used to be that all the logs were output to a file, and today a colleague asked to output the logs under a package to
Designated place, so on the Internet to check some information, summed up, so as not to use later.
What is log4j?
Log4j is an open source logging component whose products are fairly mature and widely used. In the project to be easy to use, easy to replace the System.out and other printed statements.
The goal of log4j is that it allows the developer to control which log instructions are output at any level of granularity. By using an external configuration file, you can configure it at run time.
Log4j specifically finds it in http://logging.apache.org/log4j/(with the documentation instructions used). In addition, LOG4J has been converted to C, C + +, C #, Perl, Python, Ruby, and Eiffel languages.
Second, we usually create a
The log4j.properties file is placed under SRC and is generated after compilation. /web-inf/class/
Of course, it can be placed in any other directory, as long as the directory is included in the classpath, locate the file, and read the configuration to complete the file. This configuration file tells log4j in what format, what information, and where to export.
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.
The following three components are described separately:
1, Loggers
The loggers component is divided into five levels in this system: DEBUG, INFO, WARN, error, and fatal. These five levels are sequential, and DEBUG log4j has a rule:
Assuming that the loggers level is P, if a level q is higher than p in the loggers, it can be started or 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.
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)
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
This provides considerable convenience for the output of the log.
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 log output styles, such as HTML style, free-to-specify style, style with log level 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 level and information strings for log information)
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 formats the log information in a print format similar to the printf function in C, with the following printing parameters:
%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 newline character, 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)
4. Configuration of the log4j
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: public static Logger GetLogger (String name)
Actual use: static Logger Logger = 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, which is expressed in syntax:
Or put the log4j.properties file directly under src basicconfigurator.configure (): Automatically and quickly using the default log4j environment.
Propertyconfigurator.configure (String configfilename): Reads a configuration file written using the Java properties file.
Domconfigurator.configure (String filename): Reads the configuration file in XML form.
Actual use: Propertyconfigurator.configure ("serverwithlog4j.properties");
log4j Using Tutorials