2012-10-12 91 views
0

由於Google分析可能引發很多隱私問題,因此我實施了一個事件記錄器。自制Google Analytics(分析)

我的第一個想法是將用戶生成的事件記錄到日誌文件中,然後將它們發送回服務器,以便爲系統管理員和應用程序工程師執行數據分析。

就目前的想法是記錄器實例爲ApplicationService類,並使用這些元素onCreateonDestroy安全地處理日誌文件。

解決的辦法很簡單:

  1. 打開文件
  2. 追加到它產生
  3. 一旦一個MAX_NUM_LINES事件每次到達,發送日誌服務器(可能是我我將生成文本文件)

我不知道是否有任何東西已經在野外烘焙我不知道你可能知道(如ACRA)。 每一個貢獻將不勝感激。

回答

0

在這裏我的實現。 然而,任何更好的版本非常感謝。 TSG objet只是一個靜態類,我用它作爲時間管理器。

只要您重新發布/編輯修改,就可以使用該代碼並對其進行改進。

public class Logger { 
    private BufferedWriter logFile; 
    private String nameFile; 
    public int fileLines; 
    private File fTemp; 
    private timeStampGenerator TSG; 
    private int LOG_LINES_LIMIT = 100; 
    private Object mutex; 

    public enum EventType { 
     BUTTON_PRESSED, 
     PAGE_VIEWED, 
     LOADED_ACTIVITY, 
     GENERIC_EVENT 
    } 

    public Logger (String fileName) throws IOException { 
     nameFile = fileName; 

     createLogFile(); 

     fileLines = countLines(); 

     TSG = new timeStampGenerator(); 

     // This is our mutex to access to the file 
     mutex = new Object(); 
    } 


    public void createLogFile() throws IOException{ 
     fTemp = new File (nameFile); 

     if (!fTemp.exists()) { 
      fTemp.createNewFile(); 
     } 

     logFile = new BufferedWriter(new FileWriter(nameFile, true)); 

    } 

    public void LogEvent(EventType event, String comment, String value) { 

     String line = ""; 
     line += TSG.getTimestampMillis(); 
     line += ","; 
     line += event.name(); 
     line += ","; 

     if (comment != "") { 
      line += comment.replaceAll(",", ";"); 
     } else { 
      line += " "; 
     } 

     line += ","; 

     if (value != "") { 
      line += value.replaceAll(",", ";"); 
     } else { 
      line += " "; 
     } 

     line += "\n"; 

     synchronized (mutex) { 
      try { 
       logFile.append(line); 
      } catch (IOException e) { 
       // Do wathever you want here 
      } 
      fileLines++; 
     } 
    } 


    public int countLines() //throws IOException 
    { 
     InputStream is; 
     try { 
      is = new BufferedInputStream(new FileInputStream(nameFile)); 
     } catch (FileNotFoundException e1) { 
      //let's consider it an empty file 
      return 0; 
     } 


     int count = 0; 
     boolean empty = true; 


     try { 
      int readChars = 0; 

      byte[] c = new byte[1024]; 

      while ((readChars = is.read(c)) != -1) { 
       empty = false; 
       for (int i = 0; i < readChars; ++i) { 
        if (c[i] == '\n') 
         ++count; 
       } 
      } 
     } catch(IOException e) { 
      // Do wathever you want here 
     } 


     try { 
      is.close(); 
     } catch (IOException e) { 
      // Do wathever you want here 
     } 

     return (count == 0 && !empty) ? 1 : count; 
    } 


    public boolean isLimitReached() { 

     return (fileLines >= LOG_LINES_LIMIT); 

    } 


    public void close() { 
     flush(); 
     try { 
      logFile.close(); 
     } catch (IOException e) { 
      // Do wathever you want here 
     } 
    } 



    /** 
    * clear the content of the file 
    */ 
    public void clearFile() { 
     synchronized (mutex) { 
      if (fTemp.delete()) { 
       try { 
        createLogFile(); 
       } catch (IOException e1) { 
        // Do wathever you want here 
       } 
      } 

     } 
    } 


    /** 
    * Get the full content of the file 
    * @return the content 
    */ 
    public String getContent() { 

     StringBuffer fileData = new StringBuffer(); 

     synchronized (mutex) { 
      try { 
       BufferedReader reader = new BufferedReader(new FileReader(nameFile)); 
       char[] buf = new char[1024]; 
       int numRead = 0; 
       while ((numRead = reader.read(buf)) != -1) { 
        String readData = String.valueOf(buf, 0, numRead); 
        fileData.append(readData); 
       } 
       reader.close(); 
      } catch (IOException e) { 
       // Do wathever you want here 
      } 
     } 

     return fileData.toString(); 
    } 

    public void flush() { 
     try { 
      logFile.flush(); 
     } catch (IOException e) { 
      // Do wathever you want here 
    } 
    } 
} 
相關問題