1.Logger class
The Rootlogger is logger.getrootlogger by the static method of the Logger class. All other loggers are instantiated and obtained by means of static method Logger.getlogger. This method logger.getlogger the desired logger name as an argument. Some other basic methods of the Logger class are listed below:
Package org.apache.log4j;
public class Logger {
// Creation and retrieval methods:
public static Logger getRootLogger();
public static Logger getLogger(String name);
// printing methods:
public void debug(Object message);
public void info(Object message);
public void warn(Object message);
public void error(Object message);
public void fatal(Object message);
// generic printing method:
public void log(Level l, Object message);
}
2. GetLogger method
The GetLogger method is called with the same parameter name, and the returned reference always points to the exact same logger object. For example, here:
Logger x = Logger.getLogger("wombat");
Logger y = Logger.getLogger("wombat");
X and y point to the exact same logger object.
3.log4j Use Process
1 Initialize log4j according to configuration file
Log4j can be initialized with 3 configurator:
Basicconfigurator,domconfigurator,propertyconfigurator.
Here's the propertyconfigurator. The use of Propertyconfigurator applies to all systems. such as the following statement.
Propertyconfigurator.configure ("Log4j.properties");
The log4j environment is initialized with Log4j.properties for the configuration file. For General Java project to initialize log4j,log4j without using the above statement, the configuration file is automatically found and initialized under Classpath. If the log4j cannot initialize the configuration file automatically, it needs to be initialized with the above method.
Note: Initializing the configuration file is best done only once when the system is started, if executed multiple times, one is a waste of resources, the other is for the old version of the log4j, the use of dailyrollingfileappender, there may be problems.
2) Import Org.apache.log4j.Logger and related packages.
3 Get the log instance where you need to use log4j.
private static Logger log = Logger.getlogger ("Myclass.class");
4) using the debug,info,fatal of the Logger object ... Method.
Log.debug ("It is the Debug info");