1. log4j. properties file:
Log4j. rootlogger = debug, Mina, file
### Console ###
Log4j. appender. stdout = org. Apache. log4j. leleappender
Log4j. appender. stdout. Target = system. Out
Log4j. appender. stdout. layout = org. Apache. log4j. patternlayout
Log4j. appender. stdout. layout. conversionpattern = % d {absolute} % 5 P % c {1}: % L-% m % N
Log4j. appender. Mina = org. Apache. log4j. leleappender
Log4j. appender. Mina. layout = org. Apache. log4j. patternlayout
Log4j. appender. mina. layout. conversionpattern = % d {yyyy-mm-dd hh \: mm \: SS, SSS} %-5 p % c {1} % x-% m % N
Log4j. appender. File = org. Apache. log4j. rollingfileappender
Log4j. appender. file. File = D:/logfile/minademos. Log
Log4j. appender. file. maxfilesize = 5120kb
Log4j. appender. file. maxbackupindex = 10
Log4j. appender. file. layout = org. Apache. log4j. patternlayout
Log4j. appender. file. layout. conversionpattern = [luozhonghua-Error] [% d] % p | % M | [% T] % C. % m (% L) % N
2 log4jconfig:
Import org. Apache. log4j. propertyconfigurator;
Public class log4jconfig {
Private Static Boolean isreload = true;
/**
* Load the log4j configuration file
*/
Public static void load (){
String Path = log4jconfig. Class. getclass (). getresource ("/"). getpath ()
+ "Config/log4j. properties ";
System. Out. println ("log4j configfile Path =" + path );
Propertyconfigurator. configureandwatch (path, 1000); // checks whether the file is modified at a specified interval and automatically reads the configuration again.
}
Public static void main (string [] ARGs ){
Load ();
}
Private Static void reload (){
If (isreload ){
Load ();
}
Isreload = false;
}
Public void setreload (Boolean flag ){
Isreload = flag;
}
}
3. Logger File
Import org. Apache. commons. Logging. log;
Import org. Apache. commons. Logging. logfactory;
Public class logger {
Private log = NULL;
Static {
Log4jconfig. Load (); // load the log4j configuration file
}
Private logger (){
Log = logfactory. getlog (this. getclass ());
}
Private logger (Class C ){
Log = logfactory. getlog (C );
}
Private logger (string classname ){
Log = logfactory. getlog (classname );
}
Public static logger getlogger (){
Return new logger ();
}
Public static logger getlogger (Class C ){
Return new logger (C );
}
Public static logger getlogger (string classname ){
Return new logger (classname );
}
Public void trace (string info ){
If (log. istraceenabled ())
Log. Trace (Info );
}
Public void debug (string info ){
If (log. isdebugenabled ())
Log. debug (Info );
}
Public void Info (string info ){
If (log. isinfoenabled ())
Log.info (Info );
}
Public void warn (string info ){
If (log. iswarnenabled ())
Log. Warn (Info );
}
Public void error (string info ){
If (log. iserrorenabled ())
Log. Error (Info );
}
Public void error (Object info, throwable t ){
If (log. iserrorenabled ())
Log. Error (Info + "," + t );
}
Public void fatal (string info ){
If (log. isfatalenabled ())
Log. Fatal (Info );
}
Public Boolean istraceenabled (){
Return log. istraceenabled ();
}
Public Boolean isdebugenabled (){
Return log. isdebugenabled ();
}
Public Boolean isinfoenabled (){
Return log. isinfoenabled ();
}
Public Boolean iswarnenabled (){
Return log. iswarnenabled ();
}
Public Boolean iserrorenabled (){
Return log. iserrorenabled ();
}
Public Boolean isfatalenabled (){
Return log. isfatalenabled ();
}
}
4: Test
Public class test {
Static logger log = logger. getlogger (test. Class );
Public static void main (string [] ARGs ){
For (INT I = 0; I <2; I ++ ){
Log.info ("---------- Info ");
Log. debug ("---------- debug ");
Log. Error ("---------- error ");
System. Out. println ("***********************");
Try {
Thread. Sleep (1000 );
} Catch (interruptedexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
}
}
}
The test is successful, but it seems to be available, but the log4j source code looks like a powerful Application Scenario
Custom encapsulation logger demo small test