这是本人见过写博文最负责的一个人:

http://www.crifan.com/android_try_use_android_logging_log4j_to_output_log_to_sd_card_file/

为什么说他负责呢??

因为作者写出了从确定需求到找到解决方案的整个过程,不仅会让读者少走弯路,还把解决方案全盘托出,简直是太赞啦!

当然也有些废话,把它的精华吸收过来,废话不说,上干货:

在android中,实现输出log内容到sd卡中的文件里面,做法是:

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

更多相关文章

  1. android文件下载!download!
  2. mac 配置android studio 环境
  3. Flutter遇到的坑
  4. Android(安卓)之 LogCat Log
  5. Xamarin Mono Android(安卓)Ios 安装、破解(4.12)
  6. android -x86 虚拟机成功安装
  7. 我来教大家怎样在windows下载android源码
  8. 调整android输出分辨率,适配更大分辨率的屏
  9. How to download sourcecode of android with cygwin

随机推荐

  1. Android(安卓)Q 使用通知栏消息
  2. android 拖动图片移动效果
  3. Android使用Retrofit上传单个文件以及多
  4. Android(安卓)EditText获取焦点事件
  5. android如何在子线程中更新UI
  6. 记录代码合并时产生的bug
  7. Android中一些错误
  8. 常用的android弹出对话框
  9. android之ListView与Adapter(结合JavaBea
  10. android日期控件显示