资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

Android中使用log4j输出log内容到sd卡-创新互联

在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.写测试代码:

package com.test.usb;

import java.io.File;

import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;

import de.mindpipe.android.logging.log4j.LogConfigurator;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.widget.TextView;


public class MainActivity extends Activity{
	private Logger gLogger;
	bsit_cardreader reader;
	TextView txt,txt1,txt2,txt3,txt4,txt5;
	private static final String TAG = MainActivity.class.getCanonicalName();

	 public void configLog()
	    {
	        final LogConfigurator logConfigurator = new LogConfigurator();
	         
	        logConfigurator.setFileName(Environment.getExternalStorageDirectory() + File.separator + "crifanli_log4j.txt");
	        // 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) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        configLog();
        gLogger.debug("test android log to file in sd card using log4j");
        txt=(TextView) findViewById(R.id.txt);
        txt1=(TextView) findViewById(R.id.txt1);
        txt2=(TextView) findViewById(R.id.txt2);
        txt3=(TextView) findViewById(R.id.txt3);
        txt4=(TextView) findViewById(R.id.txt4);
        txt5=(TextView) findViewById(R.id.txt5);
        reader = new bsit_cardreader();
        int openResult = reader.openreader(this);
        txt.setText("MainActivity--reader ="+ openResult);
        Log.i(TAG, "---------------4---MainActivity--reader =" + openResult);//7
        int bslot = 0 ; //非接触卡通道
        byte[] atr = reader.card_poweron(bslot);
        Log.i(TAG, "---------------5---MainActivity--atr =" + atr);
        txt1.setText("MainActivity--reader ="+ openResult);
        byte[] cmd = new byte[5];
        cmd[0] = 0x00;
        cmd[1] = -124;
        cmd[2] = 0x00; 
        cmd[3] = 0x00;
        cmd[4] = 0x08;
        Log.i(TAG, "---------------6---MainActivity--cmd =" + cmd[1]);
        txt2.setText("MainActivity--cmd ="+ cmd[1]);
        byte[] resp= reader.sendapdu(bslot, 5, cmd);  
        
    	for (int i = 0; i < resp.length; i++) {
			Log.i(TAG, "------------7-------resp[" + i + "]=" + resp[i]);
			 txt3.setText("------------7-------resp[" + i + "]=" + resp[i]);
		}
        reader.card_poweroff(bslot);
        
        
//        byte cmd = (byte) 0x84;
//        Log.i(TAG, "---------------6---MainActivity--cmd =" + cmd);
    }
    
    public boolean post(byte[] paramArrayOfByte,String http) { 
        ByteArrayEntity arrayEntity = new ByteArrayEntity(paramArrayOfByte); 
        arrayEntity.setContentType("application/octet-stream"); 
        HttpPost httpPost = new HttpPost(http); 
        httpPost.setEntity(arrayEntity); 
        DefaultHttpClient client = new DefaultHttpClient(); 
        try { 
            int result=client.execute(httpPost).getStatusLine().getStatusCode(); 
            Log.i(TAG, "post=" + result); 
            txt4.setText("post=" + result);
        } catch (Exception e) { 
            throw new RuntimeException(e); 
        } 
        return false; 
    }


}

即可实现:

(1)可以在/mnt/sdcard中生成对应的crifanli_log4j.txt文件

(2)log输出的内容中,是DEBUG,且对应的是自己的字符串标识符CrifanLiLog4jTest

因此,可以另外多下载一个叫android-logging-log4j的项目,地址在:
http://code.google.com/p/android-logging-log4j/downloads/list,注意,原本的log4j还是需要的。

 在AndroidManifest.xml中,增加如下设置:

  然后在程序中如下使用:

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
  的方式保存了。

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


网站标题:Android中使用log4j输出log内容到sd卡-创新互联
标题链接:http://cdkjz.cn/article/ijhhj.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220