This article assumes that you have learned the basic concept of log4j.
The following is given a specific example:
See the previous article in the installation process
Step1 configuration file log4j.xml
XML Version = "1.0" encoding = "UTF-8"?>
In this application, this file is located under the SRC folder, compiled into the class folder.
STEP2) Package log4j
This step is not necessary, depending on the specific application, in my application, many classes use the log4j's Logger object, in order to avoid defining a Static Logger Logger = Logger.getLogger (Clazz) attributes in each class And in order to avoid repetitive configurations in each class, you can do the following:
Package gasturbine.model.util;
Import org.apache.log4j.logger; import org.apache.log4j.xml.domconfigurator;
/ ** * @Author pw * * * / public class syslog {static logger logger = logger.getrootlogger (); static {domconfigurator.configure ("classes / log4j.xml");}
Public Static Void Debug (Object O) {Logger.debug (O);
} Public static void debug (double d) {logger.debug (" d);
}
}
This way, you can call the syslog.debug () method directly in each class that needs the log.
Of course, I am very simple, even logger.info (), logger.Error () is not, because I only need Debug for the time being, you can add it according to the specific situation
Step3) Write logs
With STEP2, this step is too simple, because Step2 has made most work, now just call the syslog.debug () method in the class that needs to write the log.
Client code snippet: