Use log4j to output log content to sd card in Android

Source: Internet
Author: User

In Android, the implementation of the output log content in the SD card file, the practice is:

Or relatively, log4j, is useful.

1. Download the Android log4j library (in the package)

Go: http://code.google.com/p/android-logging-log4j/

download the corresponding android-logging-log4j-1.0.3.jar , added to the project.

2. Download the dependent Apache log4j Library

Go: http://logging.apache.org/log4j/1.2/download.html

Download the 1.2 series version of: Log4j-1.2.17.zip

Unzip to get Log4j-1.2.17.jar added to the project.

3. Writing the test code:

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); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;LOG.I (TAG,   "---------------4---mainactivity--reader ="  + openresult);//7         int bslot = 0 ; //non-contact card channel          byte[] atr = reader.card_poweron (Bslot);        &NBSP;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;      &NBSP;&NBSP;&NBSP;LOG.I (tag,  "---------------6---mainactivity--cmd ="  + cmd[1]);         txt2.settext ("mainactivity--cmd =" + cmd[1]);   &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;BYTE[]&NBSP;RESP=&NBSP;READER.SENDAPDU (Bslot, 5, cmd);               for  (int i =  0; i < resp.length; i++)  {log.i (tag,  "------------7-------resp["  +  i +  "]="  +&nbSp;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 =&nbsP;new httppost (HTTP);          httppost.setentity (arrayEntity );         defaulthttpclient client = new  Defaulthttpclient ();         try {              int result=client.execute (HttpPost). GetStatusLine () . Getstatuscode (); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;LOG.I (TAG,   "post="  + result);              txt4.settext ("post="  + result);         } catch   (exception e)  {              Throw new runtimeexception (e);         }          return false;     }} 

Can be achieved:

(1) The corresponding Crifanli_log4j.txt file can be generated in the/mnt/sdcard

(2) The contents of the log output are debug and correspond to their own string identifiers Crifanlilog4jtest



Therefore, you can download an additional item called ANDROID-LOGGING-LOG4J, the address in:
Http://code.google.com/p/android-logging-log4j/downloads/list, note that the original log4j is still needed.

in Androidmanifest.xml, add the following settings:
<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>
   is then used in the program as follows:  

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");                }        }

  now the log is to:  
environment.getexternalstoragedirectory () + File.separator + "MyApp" + file.separator + "Logs" + File.separator + "log4j. txt 
   Way to save it.

This article from "No Water Fish" blog, reproduced please contact the author!

Use log4j to output log content to sd card in Android

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.