When we are developing Android, we often have this experience. Since the customer's device models are different, various problems occur. However, for R&D personnel, it is not easy to locate the specific location of the problem because there is no log log file. So we urgently need a tool that can write program logs into SD card files, similar to the web log4j. At this time, microlog4android entered our field of view. Although it still has shortcomings, it can meet most of the needs.
The steps to use microlog4android are as follows:
1. Download
Download the microlog4android-1.0.0.jar and microlog.properties files at http://code.google.com/p/microlog4android/downloads/list.
2. Create a logger object
private static final Logger logger = LoggerFactory.getLogger(main.class);
3. Initialize the method in the oncreate method of the first activity of the program
PropertyConfigurator.getConfigurator(this).configure();
4. Put the microlog.properties file into the assets folder
Note: The assets folder is on par with 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("This is debug information");
6. Add permission to write SD card in AndroidManifest.xml
<uses-permission android:name=”android.permission.WRITE_EXTERNAL_STORAGE” />
Run the program and then go to the root directory of the SD card and you will find that there is a microlog.txt file, which contains our log.
Change the path and name of the log asking price
A friend asked how to change the path and name of the log file. He found information online but couldn't find the answer. He had to download the source code at https://github.com/johanlkarlsson/microlog4android. After checking the PropertyConfigurator class of the source code, he found that there is such a configuration parameter microlog.appender.FileAppender.File in the source code, so that the path and name of the log file should be changed.
The configuration file is as follows:
microlog.level=DEBUG microlog.appender=FileAppender;LogCatAppender microlog.appender.FileAppender.File=mylog.txt microlog.formatter=PatternFormatter microlog.formatter.PatternFormatter.pattern=%c [%P] %m %T
When I ran the program, I found that the log file was still called microlog.txt. The name change was invalid. After finding various reasons, I couldn't solve it. I had to decompile the microlog4android-1.0.0.jar package we downloaded before. I found that the PropertyConfigurator class is different from the source code PropertyConfigurator class just downloaded from GIT. The PropertyConfigurator class in the microlog4android-1.0.0.jar package does not have such a parameter microlog.appender.FileAppender.File. I had to repackage the source code downloaded from GIT, temporarily type it into microlog4android-1.1.jar, and then rerun the program, OK, and get it done. The log file name becomes mylog.txt we configured.
Thank you for reading, I hope it can help you. Thank you for your support for this site!