Use log4j without configuration files
Log4j is an open-source Java program of Apache. It has powerful functions and can be applied to different fields. It supports various types of logs, such as console, file, email, and DB. However, the configuration of log4j must also be complex, and a log4j is required. the properties file needs to be modified every time a log is configured, And the configuration file configuration is complex. How can we use log4j to create a simple and general log system?
Below is a general-purpose log4j-based Class I wrote. Many functions have been reduced, but the log requirements are met to the greatest extent. The program is as follows, which can be modified by the reader:
/*
* To change this template, choose tools | templates
* And open the template in the editor.
*/
Package logproject;
Import org. Apache. log4j. Logger;
Import org. Apache. log4j. rollingfileappender;
Import org. Apache. log4j. basicconfigurator;
Import org. Apache. log4j. patternlayout;
Import org. Apache. log4j. level;
Import java. Io. file;
Import java. util. hashmap;
/**
*
* @ Author pgaof
*/
Public class Logging {
Private logger = NULL;
Private string posflag = "";
Private Static hashmap instances = new hashmap ();
Private logging (string LOGNAME, string logpath ){
Logger = logger. getlogger (LOGNAME );
Rollingfileappender appender = new rollingfileappender ();
If (logpath = NULL | logpath. Trim (). Equals ("") // if no logpath is specified, set it to the default logpath.
Logpath = getdefaloglogpath ();
File file = new file (logpath );
If (! File. exists () {// check whether the logpath exists. If not, create
File. mkdir ();
}
Appender. setfile (logpath + "/" + LOGNAME + ". log ");
Appender. setappend (true );
Appender. setmaxfilesize ("10240kb"); // set the maximum size of the log file to 10 MB.
Appender. setmaxbackupindex (2); // set the log file generation. If the value exceeds 10 MB, It is log.1, log.2, etc.
Appender. setlayout (New patternlayout ("[% d] % R %-5 p -- % m % N"); // set the log generation format
Appender. activateoptions (); // activation options
Logger. addappender (appender );
}
/**
* If no log name is specified, the default value is "default". The generated log is default. log.
* @ Return
*/
Public static logging getlog (){
Return getlog (null );
}
Public static logging getlog (string LOGNAME ){
Return getlog (LOGNAME, null );
}
/**
* Obtain the logging instance.
* @ Param LOGNAME the name of the log file. If the same log name is used, it is written to the same log file.
* @ Param logpath: log storage path
* @ Return
*/
Public synchronized static logging getlog (string LOGNAME, string logpath ){
If (LOGNAME = NULL | LOGNAME. Trim (). Equals ("") LOGNAME = "default ";
If (! Instances. containskey (LOGNAME )){
Logging logging = new logging (LOGNAME, logpath );
Instances. Put (LOGNAME, logging );
}
Return (logging) instances. Get (LOGNAME );
}
Public void Info (Object MSG ){
Getcaller ();
MSG = MSG + "--" + posflag;
Logger.info (MSG );
}
Public void Info (Object MSG, throwable t ){
Getcaller ();
MSG = MSG + "--" + posflag;
Logger.info (MSG, t );
}
Public void debug (Object MSG ){
Getcaller ();
MSG = MSG + "--" + posflag;/* MSG is added with the call information of the calling class. This is because the configuration of log4j contains this information, but because it is a general class, the call situation is not the same. If you do not manually add it, the displayed class information is the class of this class, and information items cannot be correctly displayed */
Logger. debug (MSG );
}
Public void debug (Object MSG, throwable t ){
Getcaller ();
MSG = MSG + "--" + posflag;
Logger. debug (MSG, t );
}
Public void error (Object MSG ){
Getcaller ();
MSG = MSG + "--" + posflag;
Logger. Error (MSG );
}
Public void error (Object MSG, throwable t ){
Getcaller ();
MSG = MSG + "--" + posflag;
Logger. Error (MSG, t );
}
Public void fatal (Object MSG ){
Getcaller ();
MSG = MSG + "--" + posflag;
Logger. Fatal (MSG );
}
Public void fatal (Object MSG, throwable t ){
Getcaller ();
MSG = MSG + "--" + posflag;
Logger. Fatal (MSG, t );
}
/**
* Obtain the root class called by this class. That is, the class called the class at the beginning.
* @ Return
*/
Private void getcaller (){
Stacktraceelement stack [] = (New throwable (). getstacktrace ();
For (INT I = 0; I <stack. Length-1; I ++ ){
// System. Out. println (stack [I]. getclassname () + "," + stack [I]. getfilename ()
// + "," + Stack [I]. getmethodname () + "," + stack [I]. getlinenumber () + "|" + this. getclass (). getname ());
If (stack [I]. getclassname (). Equals (this. getclass (). getname ())&&! Stack [I + 1]. getclassname (). Equals (this. getclass (). getname ())){
Posflag = "[" + stack [I + 1]. getclassname () + ". "+ stack [I + 1]. getmethodname () + "(" + stack [I + 1]. getfilename () + ":" + stack [I + 1]. getlinenumber () + ")]";
Break;
}
}
}
Private string getdefaloglogpath (){
String logpath = "C:/logfiles ";
If (file. pathseparatorchar = ':'){
Logpath = "/logfiles ";
}
Return logpath;
}
}
Using this class is simple, and the method is as follows:
Logging logging = logging. getlog (); // C:/logfiles in the default position window, And/logfiles in UNIX to generate a default. log file.
Logging logging = logging. getlog ("test"); // generate the test. Log File by default.
Logging logging = logging. getlog ("test", "d:/"); // generate the test. Log File under D :/.
Logging.info ("Hello World ");
Note: "test" is a log file name. If it is set to the same, it will be generated in the same log file.
Source: http://hi.baidu.com/cliff/item/16bae5a8ab3e7af715329b13