Log4j external configuration file.
Log4j details:
1. Define the configuration file
Log4j supports two configuration file formats: XML and Java. log4j. properties (Key = value ).
I will give two written files (log4j. xml and log4j. properties)
Log4j. xml
++ ++
<? XML version = "1.0" encoding = "gb2312"?>
<! Doctype log4j: Configuration System "log4j. DTD">
<Log4j: configuration xmlns: log4j = "http://jakarta.apache.org/log4j/">
<Appender name = "org. zblog. All" class = "org. Apache. log4j. rollingfileappender">
<! -- Set the channel ID: org. zblog. All
Output Method: org. Apache. log4j. rollingfileappender
(A new file is generated when the file size reaches the specified size)
-->
<Param name = "file" value = "E:/log4jnote/log4j/all. Output. log"/>
<! -- Set the file parameter: log output file name -->
<Param name = "APPEND" value = "false"/>
<! -- Set whether to add a new log to the base of the original log when the service is restarted -->
<Param name = "maxbackupindex" value = "10"/>
<Layout class = "org. Apache. log4j. patternlayout">
<Param name = "conversionpattern" value = "% P (% C: % L)-% m % N"/> <! -- Set the output file project and format -->
</Layout>
</Appender>
<Appender name = "org. zblog. zcw" class = "org. Apache. log4j. rollingfileappender">
<Param name = "file" value = "E:/log4jnote/log4j/zhuwei. Output. log"/>
<Param name = "APPEND" value = "true"/>
<Param name = "maxfilesize" value = "10240"/> <! -- Set the file size -->
<Param name = "maxbackupindex" value = "10"/>
<Layout class = "org. Apache. log4j. patternlayout">
<! -- Org. Apache. log4j. patternlayout (you can flexibly specify the layout mode) -->
<Param name = "conversionpattern" value = "% P (% C: % L)-% m % N"/>
</Layout>
</Appender>
<Logger name = "zcw. log"> <! -- Set the domain name limit, that is, the zcw. log domain and the following logs are output to the corresponding channel below -->
<Level value = "debug"/> <! -- Set level -->
<Appender-ref = "org. zblog. zcw"/> <! -- Corresponds to the preceding channel id -->
</Logger>
<Root> <! -- Set the channel for receiving all output -->
<Appender-ref = "org. zblog. All"/> <! -- Corresponds to the preceding channel id -->
</Root>
</Log4j: configuration>
++ ++
Log4j. Properties
######################################## #####################
### Set the levels of info, debug, warn, error, and output location A1, A2 for the org. zblog domain ##
Log4j.category.org. zblog = error, A1
Log4j.category.org. zblog = info, A2
Log4j. appender. A1 = org. Apache. log4j. leleappender
### Set output location A1 to leleappender (console )##
Log4j. appender. a1.layout = org. Apache. log4j. patternlayout
### Set the output layout format patterlayout of A1. (you can flexibly specify the layout mode )##
Log4j. appender. a1.layout. conversionpattern = % d {yyyy-mm-dd hh: mm: SS, SSS} [% C]-[% P] % m % N
### Configure the log output format ##
Log4j. appender. A2 = org. Apache. log4j. rollingfileappender
### Set the output location A2 to the file (a new file is generated when the file size reaches the specified size )##
Log4j. appender. a2.file = E:/log4j/democreen.html
### File location ##
Log4j. appender. a2.maxfilesize = 500kb
### File size ##
Log4j. appender. a2.maxbackupindex = 1
Log4j. appender. a2.layout = org. Apache. log4j. htmllayout
# Specify HTML output
######################################## ######################
Give two java files to test log4j in different ways
First, provide the project directory:
Log4jdemo. Java
Package com. inspur. log4j. test;
Import org. Apache. log4j. Logger;
Import org. Apache. log4j. propertyconfigurator;
Import org. Apache. log4j. xml. domconfigurator;
Public class log4jdemo {
Public static void main (string [] ARGs ){
Domconfigurator. Configure ("E:/log4j/log4j. xml"); // load the. xml file
// Propertyconfigurator. Configure ("E:/log4j/log4j. properties"); // load the. properties File
Logger log = logger. getlogger ("org. zblog. test ");
Log.info ("test ");
Log. debug ("test ");
}
}
Testlog4j. Java
Package com. inspur. log4j. test;
Import org. Apache. log4j. Logger;
Import org. Apache. log4j. propertyconfigurator;
Import org. Apache. log4j. priority;
Public class testlog4j {
Static logger = logger. getlogger (testlog4j. Class. getname ());
Public testlog4j (){
}
Public static void main (string [] ARGs ){
// Initialization through the basicaggregator class
// Basicconfigurator. Configure ();
// (1) initialize through the configuration file
Propertyconfigurator. Configure ("F: // log4j. properties ");
Logger. debug ("Start of the main () in testlog4j"); // code (2)
Logger.info ("just testing a log message with priority set to Info ");
Logger. Warn ("just testing a log message with priority set to warn ");
Logger. Error ("just testing a log message with priority set to error ");
Logger. Fatal ("just testing a log message with priority set to fatal ");
Logger. Log (priority. Warn, "testing a log message use a alternate form ");
Logger. debug (testlog4j. Class. getname (); // code (2)
}
}
The two can be compared. I will save some configuration files for Development logs later.
Log4j has come to an end.
Start learning about JPA injection!