Android中使用log4j

来源:互联网 发布:淘宝网男夏装 编辑:程序博客网 时间:2024/05/19 13:44

From:http://jackyrong.iteye.com/blog/1694699

http://blog.csdn.net/djun100/article/details/18457257


在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-logging-log4j的项目,地址在: 
http://code.google.com/p/android-logging-log4j/downloads/list,注意,原本的log4j还是需要的。 

  在AndroidManifest.xml中,增加如下设置: 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
   然后在程序中如下使用: 
 

Java代码  收藏代码
  1. package com.android.myapp;  
  2.    
  3. .  
  4. import java.io.File;  
  5.    
  6.   
  7. import org.apache.log4j.Level;  
  8.    
  9.   
  10. import org.apache.log4j.Logger;  
  11. .  
  12. import android.app.Application;  
  13.    
  14.   
  15. import android.os.Environment;  
  16.    
  17.   
  18. import de.mindpipe.android.logging.log4j.LogConfigurator;  
  19.  .  
  20.    
  21. public class MyApplication extends Application {  
  22.    
  23.   
  24.         @Override  
  25.    
  26.   
  27.         public void onCreate() {  
  28.    
  29.   
  30.                 super.onCreate();  
  31.    
  32.   
  33.                 LogConfigurator logConfigurator = new LogConfigurator();  
  34.    
  35.   
  36.                 logConfigurator.setFileName(Environment.getExternalStorageDirectory()  
  37.    
  38.   
  39.                                 + File.separator + "MyApp" + File.separator + "logs"  
  40.    
  41.   
  42.                                 + File.separator + "log4j.txt");  
  43.    
  44.   
  45.                 logConfigurator.setRootLevel(Level.DEBUG);  
  46.    
  47.   
  48.                 logConfigurator.setLevel("org.apache", Level.ERROR);  
  49.    
  50.   
  51.                 logConfigurator.setFilePattern("%d %-5p [%c{2}]-[%L] %m%n");  
  52.    
  53.   
  54.                 logConfigurator.setMaxFileSize(1024 * 1024 * 5);  
  55.    
  56.   
  57.                 logConfigurator.setImmediateFlush(true);  
  58.    
  59.   
  60.                 logConfigurator.configure();  
  61.    
  62.   
  63.                 Logger log = Logger.getLogger(MyApplication.class);  
  64.    
  65.   
  66.                 log.info("My Application Created");  
  67.    
  68.   
  69.         }  
  70.    
  71.   
  72. }  

     现在日志则是以: 
Environment.getExternalStorageDirectory() + File.separator + "MyApp" + File.separator + "logs" + File.separator + "log4j.txt 
   的方式保存了。

0 0
原创粉丝点击