1.下载android的log4j的库(的封装)
去: http://code.google.com/p/android-logging-log4j/
下载对应的 android-logging-log4j-1.0.3.jar ,加到项目中。
2.再去下载所依赖的apache的log4j库
去: http://logging.apache.org/log4j/1.2/download.html
下载1.2系列版本的: log4j-1.2.17.zip
解压得到 log4j-1.2.17.jar 加到项目中。
示例
package com.android.myapp; import java.io.File; import org.apache.log4j.Level; import org.apache.log4j.Logger; import android.app.Application; import android.os.Environment; import de.mindpipe.android.logging.log4j.LogConfigurator; public class MyApplication extends Application { @Override public void onCreate() { super.onCreate(); LogConfigurator logConfigurator = new LogConfigurator(); logConfigurator.setFileName(Environment.getExternalStorageDirectory() + File.separator + "MyApp" + File.separator + "logs" + File.separator + "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 * 5); logConfigurator.setImmediateFlush(true); logConfigurator.configure(); Logger log = Logger.getLogger(MyApplication.class); log.info("My Application Created"); } }
现在日志则是以:Environment.getExternalStorageDirectory() + File.separator + "MyApp" + File.separator + "logs" + File.separator + "log4j.txt
的方式保存了。其他和log4j的用法差不多了。