Demand:
In project development, the operation log needs to be logged. At first, I wrote a simple way to write the text to the text, and then as the project expanded, more action was needed to start thinking about performance issues.
Realize:
Consider using the SLF4J + log4j framework to implement. SLF4J is a facade of log records and supports a variety of log frameworks. Log4j is a very good log-recording framework.
Implementation: Download the class library:
Download the class library to the main site first
SLF4J website: http://www.slf4j.org/download.html
LOG4J website: http://logging.apache.org/log4j/1.2/download.html
To use it again in Android, you need a configuration tool: Http://code.google.com/p/android-logging-log4j/downloads/list may not open in the country, the following example contains
The class library you need to use:
You will need to find the following jar package in the class library that you downloaded, specifically ignore the version number section and you may have an updated version when you read this article.
Android-logging-log4j-1.0.3.jar need to use this class library when configuring Log4j in Android.
Log4j-1.2.17.jar log4j's main feature Pack, this file has 460k large, for the public users, need to consider the size of the app package carefully consider the log mode of this article.
Slf4j-api-1.7.12.jar SJF4J's main feature Pack.
Slf4j-log4j12-1.7.12.jar bundle, bind sff4j and log4j together so you can use SLF4J call log to mobilize log4j to log
Implementation code
Add the above several class library references to your project, typically copied to the Libs folder.
Customize the application class, inherit from application, configure the log at oncreate startup, with the following code:
PackageVir56k.logdemo;Importandroid.app.Application;Importandroid.os.Environment;ImportOrg.apache.log4j.Level;ImportOrg.apache.log4j.Logger;ImportJava.io.File;ImportDe.mindpipe.android.logging.log4j.LogConfigurator;/*** Created by Zhangyunfei on 15/9/9.*/ Public classMyAppextendsApplication {@Override Public voidonCreate () {Super. OnCreate (); Configlog ("Log"); Logger Log= Logger.getlogger (MYAPP).class); Log.info ("My Application Created"); } Private voidConfiglog (String logfilenameprefix) {logconfigurator logconfigurator=NewLogconfigurator (); Logconfigurator.setfilename (Environment.getexternalstoragedirectory ()+ File.separator + "Logdemo" + file.separator + "Logs" + File.separator + (Logfilenameprefix = =NULL? "": Logfilenameprefix) + "Log4j.txt"); Logconfigurator.setrootlevel (Level.debug); Logconfigurator.setlevel ("Org.apache", Level.error); Logconfigurator.setfilepattern ("%d%-5p [%c{2}]-[%l]%m%n"); Logconfigurator.setmaxfilesize (1024 * 1024 * 2); Logconfigurator.setimmediateflush (true); Logconfigurator.configure (); }}
Write the Log code:
Instantiation of
private static final Logger Logger = Loggerfactory.getlogger (Mainactivity.class);
Perform
Logger.debug ("Some log message. Details: {} "," Debug output ");
I put my demo on GitHub with the following address: Https://github.com/vir56k/demo
Reference:
Http://www.cnblogs.com/niutouzdq/archive/2013/12/24/3489062.html
http://cache.baiducontent.com/c?m= 9d78d513d99d1cfa5eb5837e7c5789320e55f06564c0d0642c88c508ce3f0d550026bdb47d645646c4c40f7a1cff1701bfe73605340721a09bbfd9178 2a6d76f64d379732c1c9b114c8e4cb28a0076877bc147e2a51ee6b9f33093acd1d0dc5754c1540f7d8af18f5d1d&p= 8b2a9715d9c342ab43b7d16815538c&newp=9c63c64ad4df11a058ee90664f4fcd231610db2151d4d01163&user=baidu& Fm=sc&query=log4j+slf4j+android&qid=a779aae80001b5b4&p1=8
Http://stackoverflow.com/questions/2915150/log4j-support-in-android
Http://stackoverflow.com/questions/2915150/log4j-support-in-android
Configuring the SLF4J + log4j logging framework in Android