How to implement a set of custom log records and "Utilities Logging" framework in the Java library
How to implement a custom log recorder in the Java library and the "Utilities Logging" framework
Overview:
During the development of the Java class library, log records are very important. It can help developers track and debug code and provide key runtime information.Java provides many log record frameworks, one of which is "Utilities Logging" framework.This article will introduce how to realize a custom log recorder in the Java library and integrate it with the "Utilities Logging" framework.
Step 1: Import dependencies
First, we need to import the "Utilities Logging" framework in the project construction file (such as Maven's Pom.xml).
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.32</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.32</version>
</dependency>
<!-Other dependencies->
</dependencies>
Step 2: Create a custom log recorder
We can create a custom log recorder by implementing the "java.util.logging.handler" interface.The following is an example:
import java.util.logging.Handler;
import java.util.logging.LogRecord;
public class CustomLogger extends Handler {
@Override
public void publish(LogRecord record) {
// Write the logic of log records here
}
@Override
public void flush() {
// Writing the logic of the log refresh here
}
@Override
public void close() throws SecurityException {
// Write the logic of the logging of the log here
}
}
In a custom log recorder, you can write logic of logging, refreshing and closing as needed.
Step 3: Integrated "Utilities Logging" framework
In order to integrate the custom log recorder with the "Utilities Logging" framework, we need to create a configuration file that is used to specify the implementation class and other related configurations of the log recorder.
Create a file called "Logging.properties" under the project's resource folder and add the following:
handlers = com.example.CustomLogger
com.example.CustomLogger.level = ALL
In the above configuration, "handlers" specifies the implementation class of the log recorder.In addition, "com.example.customlogger.Level" specifies the level of log records. The setting here as "all" means that all logs will be recorded.
Step 4: Use a custom log recorder
In the code of the Java class library, we can use the "java.util.logging.logger" class to obtain the log recorder and record log information.The following is an example:
import java.util.logging.Logger;
public class MyClass {
private static final Logger logger = Logger.getLogger(MyClass.class.getName());
public void doSomething() {
// Here is the code logic
Logger.info ("Executive Dosomething Method");
}
}
In the above example, we obtained a log recorder related to the "MyClass" class through the "logger.getLogger ()" method.We can then record a message using the "Logger.info ()" method.
Step 5: Run program and view log
In the application of the Java library, we can specify the logging file used by setting the system attribute "java.util.logging.config.file".When the application starts, we can specify the path of the configuration file through the following ways:
System.setProperty("java.util.logging.config.file", "/path/to/logging.properties");
Add the above code to the starting position of the application and run the program.The "Utilities Logging" framework will use a specified configuration file and record the log records according to the settings in the configuration file.
Summarize:
The integration of the custom log recorder in the Java class library and the "Utilities Logging" framework is very important. It can help developers better track and debug code during the development process.This article introduces how to create a custom log recorder and integrate it with the "Utilities Logging" framework.By following the above steps, you can easily implement the custom logging function in the Java class library.
Reference link:
- [The Java Tutorials: Logging](https://docs.oracle.com/javase/tutorial/essential/logging/index.html)
- [SLF4J - Simple Logging Facade for Java](http://www.slf4j.org/)