Microlog4android the Android log log to the SD card file to implement the method _java

Source: Internet
Author: User
Tags log log

When we do Android development, we often have this experience, because the customer's device models are different, resulting in a variety of problems, but for developers, because there is no log log files, it is not easy to locate the exact location of the problem. So we need a tool that can write the program log to the SD card file, like the Web log4j. At this time microlog4android into our vision, although it has not enough, but also has been able to meet most of the needs.

The steps to use Microlog4android are as follows:

1, download

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

2, establish the use of Logger objects

private static final Logger Logger = Loggerfactory.getlogger (Main.class);

3. Initializing 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 peer to 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 the log record

Logger.debug ("This is debug information");

6, in Androidmanifest.xml Add write SD card permissions

<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>

Run the program, and then to the SD card root directory, you can find a microlog.txt file, which is our log.

Change the path and name of the journal Price

A friend asked how to change the path and name of the log file, found a pass on the internet, did not find the answer, had to https://github.com/johanlkarlsson/microlog4android download source code, After viewing the source code of the Propertyconfigurator class, found in the source code has such a configuration parameter microlog.appender.FileAppender.File, so you should be able to change the log file path and name.

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, Find the log file or call Microlog.txt, change the name is invalid, find a variety of reasons can not be resolved, had to decompile our previous download Microlog4android-1.0.0.jar package, found Propertyconfigurator this class and just downloaded from git source code PR Opertyconfigurator classes are different, Microlog4android-1.0.0.jar bag Propertyconfigurator class does not have such a parameter microlog.appender.FileAppender.File, had to put the source code downloaded from Git, back to play a bag, temporarily Microlog4android-1.1.jar, and then rerun the program, OK, fix, log file name into our configured mylog.txt.

Thank you for reading, I hope to help you, thank you for your support for this site!

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.