用android-logging-log4j去实现log输出内容到sd卡中的文件的功能

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值