Android JDK logs can promote innovation of mobile devices and provide users with the most advantageous mobile services. At the same time, developers will also get a new open level. A wide range of development environments include device simulators, debugging tools, memory and performance analysis charts, and integrated development environment plug-ins with Eclipse.
This class shows that the abstract log class of Android JDK Provides abstract interfaces: publish, flush, and close. These interfaces provide basic functions for log output. At the same time, the Handler class saves Formatter, and the Filter and Level objects are used to control log output. Therefore, the following steps are required to write a Custom Handler class:
1. inherit the Handler abstract class
2. Implement the publish, flush, and close methods. The publish method is used to publish a log record. The flush method clears the memory buffer. When the application is closed, the close method releases the resources requested by the Handler class, such as files and sockets)
3. Set the default Formatter, Filter, and Level objects. When necessary, you can read the configuration file during class initialization to set these parameters.
- public class MyFormatter extends Formatter {
- private final String lineSeparator = System.getProperty("line.separator");
- @Override
- public String format(LogRecord record) {
- StringBuffer sb = new StringBuffer();
- String message = formatMessage(record);
- sb.append(record.getLevel().getLocalizedName());
- sb.append(message);
- sb.append(lineSeparator);
- if (record.getThrown() != null) {
- try {
- StringWriter sw = new StringWriter();
- PrintWriter pw = new PrintWriter(sw);
- record.getThrown().printStackTrace(pw);
- pw.close();
- sb.append(sw.toString());
- } catch (Exception ex) {
- }
- }
- return sb.toString();
- }
- }
Here, the reportError method outputs the error information in the log class to the outside world. This is the ErrorManager class implemented by the ErrorManager class to record Handler errors in the log framework. Generally, this error is printed to the console.
Each specific Log message is encapsulated into a LogRecord object by the Android JDK log framework. The definition of this type is shown in the list. As can be seen from the list, the LogRecord class contains all the information of a log message level, message text, time, parameter, thread, and so on, which are handled by Handler, Formatter, and Filter objects.
This class is also serializable and can be serialized to networks and files. This class can also be bound to a ResourceBundle object to implement localized processing of message strings. Describes the implementation of a typical Custom Handler class. In the later part of this article, we will introduce how to implement an Android JDK log processing class.