Use microlog4android to write logs from Android to SD card files

Source: Internet
Author: User

Recently, when working on the andoird project, we often encounter different machine models on the client side, resulting inProgramThere is a bug problem, and we cannot locate the exact location of this bug (because there is no log file ).

So I found the relevant information about writing program logs to the SD card, just like the Web log4j, to generate a log file to the SD card.

After searching for half a day, I did not find a satisfactory open-source software, so I found microlog4android. Although I was not satisfied with it, it could meet most of the requirements.

The specific usage is as follows:

1. Download

Download the http://code.google.com/p/microlog4android/downloads/list and microlog. properties files to the microlog4android-1.0.0.jar

2. Create a logger object

Private Static final 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 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 ("this is debug information ");

6. Add SD card write permission in androidmanifest. xml

<Uses-Permission Android: Name = "android. Permission. write_external_storage"/>

 

After running the program, you can find a microlog.txt file, which contains our logs.

Follow-up:

A friend asked how to change the path and name of the log file, on the Internet to find a channel of information, did not find the answer, had to download to the https://github.com/johanlkarlsson/microlog4androidSource code, View the sourceCodeIn the source code, we can find that there is such a configuration parameter microlog. appender. fileappender. file, so that we can change the path and name of the log file.

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

Run the program, found the log file is also called microlog.txt, change the name is invalid, find a variety of reasons can not solve, had to decompile the microlog4android-1.0.0.jar package we downloaded before, found propertyconfigurator class and just downloaded from git propertyconfigurator class is not the same, the propertyconfigurator class in the microlog4android-1.0.0.jar package does not have such a parameterMicrolog. appender. fileappender. file, so you have to download the source code from git,Pack a new pack and compress it into a microlog4android-1.1.jar.Then re-run the program. OK, and the log file name is changed to mylog.txt.

Attaches the entire project codeHttp://files.cnblogs.com/fbsk/LogTest.zip

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.