在android中,實(shí)現(xiàn)輸出log內(nèi)容到sd卡中的文件里面,做法是:
創(chuàng)新互聯(lián)公司是一家集網(wǎng)站建設(shè),彭水苗族土家族企業(yè)網(wǎng)站建設(shè),彭水苗族土家族品牌網(wǎng)站建設(shè),網(wǎng)站定制,彭水苗族土家族網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營(yíng)銷,網(wǎng)絡(luò)優(yōu)化,彭水苗族土家族網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競(jìng)爭(zhēng)力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長(zhǎng)自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。
還是相對(duì)來(lái)說(shuō),log4j,算是好用。
1.下載android的log4j的庫(kù)(的封裝)
去:http://code.google.com/p/android-logging-log4j/
下載對(duì)應(yīng)的android-logging-log4j-1.0.3.jar,加到項(xiàng)目中。
2.再去下載所依賴的apache的log4j庫(kù)
去:http://logging.apache.org/log4j/1.2/download.html
下載1.2系列版本的:log4j-1.2.17.zip
解壓得到log4j-1.2.17.jar加到項(xiàng)目中。
3.寫測(cè)試代碼:
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; } }
即可實(shí)現(xiàn):
(1)可以在/mnt/sdcard中生成對(duì)應(yīng)的crifanli_log4j.txt文件
(2)log輸出的內(nèi)容中,是DEBUG,且對(duì)應(yīng)的是自己的字符串標(biāo)識(shí)符CrifanLiLog4jTest
因此,可以另外多下載一個(gè)叫android-logging-log4j的項(xiàng)目,地址在:
http://code.google.com/p/android-logging-log4j/downloads/list,注意,原本的log4j還是需要的。
在AndroidManifest.xml中,增加如下設(shè)置:
然后在程序中如下使用:
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"); } }
現(xiàn)在日志則是以:
Environment.getExternalStorageDirectory() + File.separator + "MyApp" + File.separator + "logs" + File.separator + "log4j.txt
的方式保存了。