Document directory
- Additional instructions (implement cyclic scrolling logs, that is, keep logs appended to the end of the previous one)
Cause Analysis
When do we need to write our logs to a file and store them for viewing? I will summarize several situations:
1. debug the service started by alarm manager. If you want to know whether every service execution is normal, everything is what you think;
2. Some important information needs to be saved for analysis or uploaded to the server for analysis;
3. During Development, understand the working status and execution efficiency of network interfaces (the execution time of each interface;
Process Analysis
1. Download
Download the http://code.google.com/p/microlog4android/downloads/list and microlog. properties files from the microlog4android-1.0.0.jar (note:
The downloaded microlog.propertiesis microlog.properties.txt. Modify the suffix)
2. Create a logger object
Private Static final logger = loggerfactory. getlogger (main. Class );
3. initialize the method in the oncreate method of the activit or service of the program.
Propertyconfigurator. getconfigurator (this). Configure ();
4. Put the microlog. properties file in the assets folder.
Note: The assets folder is the same as the res folder.
Then change the microlog. properties file to the following:
Microlog. Level = debug
Microlog. appender = logcatappender; fileappender
Microlog. formatter = patternformatter
Microlog. formatter. patternformatter. pattern = % C [% P] % m % t
5. Write log records
Logger. debug ("My debug ");
6. Add SD card write permission in androidmanifest. xml
<Uses-Permission Android: Name = "android. Permission. write_external_storage"/>
After the program is started, you can find a microlog.txt file, which contains the logs we recorded.
Additional instructions (implement cyclic scrolling logs, that is, keep logs appended to the end of the previous one)
1. The above logs are called in multiple places. Sometimes we may find that only one record is saved, and each time the subsequent log overwrites the previous log, we need to add additional code;
2. At the end of Step 3: Final fileappender fa = logger. getappender (1); fa. setappend (true); OK, this is done.