"LOG" framework introduction and use in Java class libraries
"LOG" framework introduction and use in the common "LOG" framework in the Java class library
In Java development, log records are an important task.Through reasonable log records, the operation of the program and the tracking of the program can be achieved, which is convenient for developers to debug and error repair.In order to simplify the operation of log records, the Java class library provides a common "LOG" framework to facilitate developers to perform logging operations.
In the Java class library, the common "LOG" framework includes the following: log4j, java.util.logging and Commons-Logging.
1. log4j:
LOG4J is a powerful logging tool, which provides rich configuration options and flexible logging functions.Using log4j, the log outputs the log to different destinations such as console, files, databases, etc. through configuration files.The main class of log4j is Logger, which is used to obtain and configure the log recorder, and then record the log message by calling different levels of log methods (such as Debug, Info, ERROR).Here are a sample code that uses log4j to record logs:
import org.apache.log4j.Logger;
public class MyClass {
private static final Logger logger = Logger.getLogger(MyClass.class);
public static void main(String[] args) {
logger.debug("Debug message");
logger.info("Info message");
logger.error("Error message");
}
}
2. java.util.logging:
Java.util. Logging is the default log record framework provided by Java SE.It uses the logger class to provide a logging function.Similar to log4j, it can also configure the format, level, and destination of the log output.The following is an example code that uses java.util.logging to record logs:
import java.util.logging.Logger;
public class MyClass {
private static final Logger logger = Logger.getLogger(MyClass.class.getName());
public static void main(String[] args) {
logger.severe("Severe message");
logger.warning("Warning message");
logger.info("Info message");
logger.config("Config message");
logger.fine("Fine message");
logger.finer("Finer message");
logger.finest("Finest message");
}
}
3. commons-logging:
Commons-Logging is a universal log record interface that can integrate with multiple log record frameworks, such as log4j, java.util.logging, SLF4J, etc.By using Commons-Logging, you can seamlessly switch the logging framework in different environments.The following is an example code that uses Commons-Logging to record logs:
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class MyClass {
private static final Log logger = LogFactory.getLog(MyClass.class);
public static void main(String[] args) {
logger.debug("Debug message");
logger.info("Info message");
logger.error("Error message");
}
}
By using the above framework, logging operations can be easily performed.According to the actual situation and needs, choosing a log frame suitable for you, and reasonably configuration and use, will greatly improve development efficiency and debug convenience.