用android-logging-log4j去实现log输出内容到sd卡中的文件的功能
16lz
2021-01-23
http://www.crifan.com/android_try_use_android_logging_log4j_to_output_log_to_sd_card_file/
在android中,实现输出log内容到sd卡中的文件里面,做法是:
还是相对来说,log4j,算是好用。
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加到项目中。
3.写测试代码:
?1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | import de.mindpipe.android.logging.log4j.LogConfigurator; import java.io.File; import android.os.Environment; import org.apache.log4j.Level; import org.apache.log4j.Logger; public class BaseActivity extends Activity { private Logger gLogger; public void configLog() { final LogConfigurator logConfigurator = new LogConfigurator(); logConfigurator.setFileName(Environment.getExternalStorageDirectory() + File.separator + "crifanli_log4j.log" ); // Set the root log level logConfigurator.setRootLevel(Level.DEBUG); // Set log level of a specific logger logConfigurator.setLevel( "org.apache" , Level.ERROR); logConfigurator.configure(); //gLogger = Logger.getLogger(this.getClass()); gLogger = Logger.getLogger( "CrifanLiLog4jTest" ); } @Override protected void onCreate(Bundle savedInstanceState) { configLog(); gLogger.debug( "test android log to file in sd card using log4j" ); |
即可实现:
(1)可以在/mnt/sdcard中生成对应的crifanli_log4j.log文件
(2)log输出的内容中,是DEBUG,且对应的是自己的字符串标识符CrifanLiLog4jTest
更多相关文章
- Android 快速打包项目成jar包
- android中读XML文件
- android带进度的文件上传
- 实现Android ListView 自动加载更多内容
- AIR Native Extension的使用(Android)二 : Flex mobile项目中使
- android 查看解压后的.xml文件代码(axmlprinter2)
- 文件编码的测试(android)
- 【Android】使用dex2jar 与JD-Gui 反编译APK文件,查看源代码
- (转)Android软件测试的日志文件抓取简介