What are the commonly used LOG frameworks in Java?
There are many log frameworks used in Java. Among them, there are more common and popular ones:
1. LOG4J: log4j is a log frame provided by the Apache open source organization, providing a highly configured logging function for the Java program.It supports different levels of log records, which can output log output to different goals such as console, files, databases, and other goals.
The following is an example of Java code that uses log4j to record logs:
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class MyClass {
private static final Logger logger = LogManager.getLogger(MyClass.class);
public static void main(String[] args) {
logger.debug("This is a debug log message.");
logger.info("This is an info log message.");
logger.error("This is an error log message.");
}
}
2. LOGBACK: LogBack is the successor of log4j, which is also developed by CEKI Gülcü.Compared with LOG4J, LOGBACK has improved performance and efficiency, and supports more features and functions.
The following is an example of Java code that uses a logback record log:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class MyClass {
private static final Logger logger = LoggerFactory.getLogger(MyClass.class);
public static void main(String[] args) {
logger.debug("This is a debug log message.");
logger.info("This is an info log message.");
logger.error("This is an error log message.");
}
}
3. JDK Logging: JDK Logging is a log frame provided by the Java standard library.It is a lightweight log frame, which is suitable for simple log record needs, but it is limited compared to other log frameworks.
Here are a Java code example using JDK 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.fine("This is a debug log message.");
logger.info("This is an info log message.");
logger.severe("This is an error log message.");
}
}
Summary: In Java, log4j, logback and JDK Logging are more popular log framework.According to actual needs and personal preferences, you can choose a log frame suitable for you to record and manage the log.