2012-09-27 51 views
8

我知道這個話題已經被討論了很多,但並非如此。 我需要將日誌存儲在.txt文件中,但我不能使用log4j或任何其他類,但android.util.log 我有這個解決方案,但它不是最好的。 對於與以下內容相同的信息:Log.i(TAG,「INFO Message」); 我必須寫...如何使用android.util.log將日誌存儲在txt文件中

ERROR = logLevel < 3; 
WARNING = logLevel < 2; 
INFO = logLevel < 1; 
if (INFO){ 

    appendLog("LEVEL: I TIME: "+java.util.GregorianCalendar.DAY_OF_MONTH + 
         "-"+ java.util.GregorianCalendar.MONTH +" "+GregorianCalendar.HOUR_OF_DAY +":"+GregorianCalendar.MINUTE + 
         ":"+GregorianCalendar.SECOND +"."+GregorianCalendar.MILLISECOND + " PID: "+ 
         android.os.Process.myPid()+ " TID: "+android.os.Process.myTid()+ " Application: com.example.myapplication"+ 
         " TAG:" +TAG+ " TEXT: An INFO Message"); 
} 

然後......

public void appendLog(String text) {   
    File logFile = new File("sdcard/log.txt"); 
    if (!logFile.exists()) { 
     try { 
      logFile.createNewFile(); 
     }catch (IOException e){ 
      e.printStackTrace(); 
     } 
    } 
    try { 
     BufferedWriter buf = new BufferedWriter(new FileWriter(logFile, true)); 
     buf.append(text); 
     buf.newLine(); 
     buf.close(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

沒有人有比這更好的解決方案?感謝你們對我的幫助。

+0

看看http://stackoverflow.com/a/3359857/1321873 – Rajesh

+0

感謝拉傑什這是非常接近我想要的東西。 – Alberto

回答

2

創建一個包裝類,它將包裝Android的Log類。此包裝類將通過將文本另外記錄到文件中來擴展Log類的功能。

例子:

public class MyLog{ 
    public static void i(String TAG, String message){ 

     // Printing the message to LogCat console 
     Log.i(TAG, message); 

     // Write the log message to the file 
     appendLog(message); 
    } 

    public static void d(String TAG, String message){ 
     Log.d(TAG, message); 
     appendLog(message); 
    } 

    // rest of log methods... 
} 

然後你使用對子級像這樣:

MyLog.i("LEVEL 1", "Your log message here..."); 
+0

但它並沒有給我時間,TID,PID,應用程序名稱...只給我的信息。 我要求一個解決方案,我不必自己寫所有這些參數。 當然,如果我將它包裝在課堂中,我只需要編寫一次,但仍然看起來不是最好的解決方案... 任何其他解決方案? – Alberto

9

這裏跟我附上簡單的Logger類的定義,你可以用它的。 要將日誌信息存儲到SDCARD中的Log.txt文件中,請使用

package com.clientname.projectname; 

import java.io.BufferedWriter; 
import java.io.File; 
import java.io.FileWriter; 
import java.io.IOException; 
import java.util.logging.FileHandler; 

import android.os.Environment; 
import android.util.Log; 

/** 
* @author Rakesh.Jha 
* Date - 07/10/2013 
* Definition - Logger file use to keep Log info to external SD with the simple method 
*/ 

public class Logger { 

    public static FileHandler logger = null; 
    private static String filename = "ProjectName_Log"; 

    static boolean isExternalStorageAvailable = false; 
    static boolean isExternalStorageWriteable = false; 
    static String state = Environment.getExternalStorageState(); 

    public static void addRecordToLog(String message) { 

     if (Environment.MEDIA_MOUNTED.equals(state)) { 
      // We can read and write the media 
      isExternalStorageAvailable = isExternalStorageWriteable = true; 
     } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) { 
      // We can only read the media 
      isExternalStorageAvailable = true; 
      isExternalStorageWriteable = false; 
     } else { 
      // Something else is wrong. It may be one of many other states, but all we need 
      // to know is we can neither read nor write 
      isExternalStorageAvailable = isExternalStorageWriteable = false; 
     } 

     File dir = new File("/sdcard/Files/Project_Name");  
     if (Environment.MEDIA_MOUNTED.equals(state)) { 
      if(!dir.exists()) { 
       Log.d("Dir created ", "Dir created "); 
       dir.mkdirs(); 
      } 

      File logFile = new File("/sdcard/Files/Project_Name/"+filename+".txt"); 

      if (!logFile.exists()) { 
       try { 
        Log.d("File created ", "File created "); 
        logFile.createNewFile(); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
      } 
      try { 
       //BufferedWriter for performance, true to set append to file flag 
       BufferedWriter buf = new BufferedWriter(new FileWriter(logFile, true)); 

       buf.write(message + "\r\n"); 
       //buf.append(message); 
       buf.newLine(); 
       buf.flush(); 
       buf.close(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 
    } 
} 

現在,一旦你創建了這個文件,在任何你想存儲日誌信息到下面的代碼log.txt文件使用。 -

package com.clientname.projectname; 

import android.app.Activity; 
import android.media.MediaPlayer; 
import android.os.Bundle; 
import android.util.Log; 

/** 

* @author Rakesh.Jha 
* Date - 03/10/2013 
* Definition - //ToDO 

*/ 

public class MainActivity extends Activity { 

    private static final String TAG = null; 
    Logger logger; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     Log.d("Testing  :","log"); // no need to do this line, use below line 
     logger.addRecordToLog("Testing  : log "); 

     logger.addRecordToLog("TAG MediaPlayer audio session ID: "); 

     MediaPlayer mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.test);//test is audio file, u have to keep in raw folder 

     logger.addRecordToLog("MediaPlayer audio session ID: " + mediaPlayer.getAudioSessionId()); 
     logger.addRecordToLog("Media Player started " + "Started !"); 

     mediaPlayer.start(); // no need to call prepare(); create() does that for you 
    } 

    private void prepareMediaServer() { } 
} 
相關問題