How to use JDK logs in Android
This article mainly explains "how to use JDK log in Android". The explanation in this article is simple and clear, easy to learn and understand. Please follow the editor's train of thought to study and learn "how to use JDK log in Android".
1. Inherit the Handler abstract class
2. Implement publish,flush and close methods. Where the publish method is used to publish a log record. The flush method is to clear the memory buffer. The close method releases the resources requested by the Handler class (such as files, socket, etc.) when the application is closed.
3. Set the default Formatter,Filter and Level objects. If necessary, you can read the configuration file to set these parameters when the class is initialized.
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, and the ErrorManager class implemented by the ErrorManager class is responsible for recording the errors of Handler in the logging framework. Typically, the error is printed to the console.
Each specific log message is encapsulated into a LogRecord object by the Android JDK logging framework, which is partially defined as shown in the listing. As you can see from the listing, the LogRecord class contains information about the level of a log message, message text, time, parameters, threads, and so on, which are handled by objects such as Handler,Formatter and Filter. At the same time, the class is serializable and can be serialized to the network and files. This class can also be bound to a ResourceBundle object to localize the message string. Describes the implementation of a typical custom Handler class.
Thank you for reading, the above is the content of "how to use JDK log in Android". After the study of this article, I believe you have a deeper understanding of how to use JDK log in Android, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!