Understand the log records and debugging skills in Apache Commons Logging

In Java development, the log of the application is often required to perform debugging and investigation errors.Apache Commons Logging is an open source Java log record framework that provides developers with a general abstract layer that allows them to switch between different log record tools.This article will introduce the log records and debugging techniques in Apache Commons Logging, and provide some Java code examples. 1. Introduce dependencies To use Apache Commons Logging, you need to introduce the corresponding dependence in the project.In the Maven project, the following dependencies can be added to the pom.xml file: <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>xxx</version> </dependency> Please replace the `xxx` to the required version number. 2. Configure log recorder Before starting to use Apache Commonts Logging, we need to configure a log recorder for it to determine the log record tool to be used.It can be implemented by adding a configuration file called `COMMONS-LOGGINGING.PROPERTIES` under the` ClassPath` of the project.In this configuration file, you can specify the required log record tools, such as log4j, java util logging, etc.The following is an example configuration file: # Use log4j as a log record tool org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger 3. Record log Once the configuration is completed, you can start to record the log.Apache Commons Logging provides several commonly used recording logs, as shown below: import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; public class MyClass { // Get the log recorder private static final Log log = LogFactory.getLog(MyClass.class); public static void main(String[] args) { String name = "John"; log.debug ("Debug Message"); // Record debug logs log.info ("Hello," + name); // Record the information log Log.warn ("Warning Message"); // Record warning log log.error ("ERROR Message"); // Record the wrong log } } In the above example, a log recorder was obtained using the method of `logfactory.getlog ()` `myclass.class`.Then, you can use the `LOG` object to call different methods to record logs, such as` debug () `,` info (), `warn () and` Error () `. 4. Log level Apache Commons Logging supports different logs, including `Debug`,` Info`, `Warn` and` Error` and so on.Developers can choose the appropriate log level as needed.By configured files and log recorders, you can control the recorded log level. # Configuration log level is INFO log4j.rootLogger=INFO, console In the above example, set the log level to `Info`, which means that it only records the logs of the` Info` level and above.Developers can set the corresponding settings according to the actual situation. 5. log output format Apache Commons Logging supports custom log output formats.Settings can be performed in the configuration file of the log recorder.The following is an example configuration: log4j.appender.console.layout.ConversionPattern=%d [%t] %-5p %c - %m%n In the above example, the `LOG4J.APPENDER.CONSOLE.LAYOUT.CONVERSIONPATERN` attribute is used to set the output format of the log.Customized formats can be customized according to demand. Summarize: This article introduces the log records and debugging techniques in Apache Commons Logging.By configured the log recorder, the logs of different levels, and setting the log output format, the developers can better debug and check the errors.If you want to understand more functions of Apache Commons Logging, please consult the official documentation.