How to use the "Warning" framework in the Java class library

How to use the "Warning" framework in the Java class library Introduction: In Java development, log information is often required to record and process warnings.In order to better process and record these warning information, some open source frameworks have been provided in the Java library.This article will introduce several common methods of the 'Warning' framework, including the Java.util. Logging comes with Log4J, LogBack and Java. 1. Log4j: LOG4J is one of the most commonly used log frames in Java development.The LOG4J record warning information is very simple.First, you need to add LOG4J dependency items to the project.You can add the following content to the pom.xml file in the Maven project to quote the relevant dependencies: <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> After adding the above code to pom.xml, you can use the following example code to record the logo information of the warning level. import org.apache.log4j.Logger; public class Log4jExample { private static final Logger LOGGER = Logger.getLogger(Log4jExample.class); public static void main(String[] args) { LOGGER.warn("This is a warning message."); } } In the above examples, use the `logger.getLogger (log4jexample.class)` to get a logger instance, and then use the `logler.warn (" this is a warning message. ") Method to record warning log information. 2. Logback: LOGBACK is the follow -up version of LOG4J, which provides higher -level functions and performance optimization.Use logback warning information to be similar to log4j.First of all, you need to add LogBack dependency items to the project.You can add the following content to the pom.xml file in the Maven project: the relevant dependencies are referenced: <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.2.3</version> </dependency> After adding the above code to pom.xml, you can use the following example code to record the logo information of the warning level. import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class LogbackExample { private static final Logger LOGGER = LoggerFactory.getLogger(LogbackExample.class); public static void main(String[] args) { LOGGER.warn("This is a warning message."); } } In the above examples, use the `loggerFactory.getLogger (logBackexample.class)` to get a logger instance, and then use the `loger.warn (" this is a warning message. ")` Method to record the warning log information. 3. java.util.logging: In addition to using a third -party framework, the Java.util. Logging framework is also built in the Java class library.By quoting the `java.util.logging` package, the log framework that comes with Java can also easily record warning information.The following is an example code that uses the warning information to record the warning information using `java.util.logging`. import java.util.logging.Level; import java.util.logging.Logger; public class JavaUtilLoggingExample { private static final Logger LOGGER = Logger.getLogger(JavaUtilLoggingExample.class.getName()); public static void main(String[] args) { LOGGER.log(Level.WARNING, "This is a warning message."); } } In the above examples, obtain a Logger instance through the `logger.getLogger (javautilloggingExample.class.getName ())`), and then use the `logler.log (level.warning," this is a warning message. ")` Method record warning loginformation. Summarize: In the development of Java, it is very important to record and process the logo information of the warning level.By using the frameworks such as log4j, logback and Java.util.logging, you can easily record and manage these warning information.This article introduces the above methods of use of several common frameworks, hoping to help Java developers in terms of recording and processing warning information.