我開源的Android日誌收集器

yangxi_001發表於2014-10-31

轉自:http://blog.csdn.net/forlong401/article/details/21896299

Github地址:https://github.com/licong/log

csdn code地址:https://code.csdn.net/forlong401/android_log_collector

Log Collector

Collect the normal or crash log in Android, then save them into files or upload into server.

  1. How using the libs?

1.1 step1:

Add the below permission into your manifest xml.

<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />

1.2 step2:

Add output\log.jar into your libs folder in your project.

1.3 step3:

Register or unregister the crash handler in your application

/**

  • Log application.
  • @author Li Cong
  • @date 2014-3-23 */

public class LogApp extends Application {

@Override
public void onCreate() {
    super.onCreate();
    LogManager.getManager(getApplicationContext()).registerCrashHandler();
}

@Override
public void onTerminate() {
    super.onTerminate();
    LogManager.getManager(getApplicationContext()).unregisterCrashHandler();
}

}

1.4 step4(Collect carsh log done.):

Register the activity in the onCreate() of Activity.Unregister the activity in the onDestroy() of Activity. You should register and unregister for all activities in your manifest xml.

public class MainActivity extends Activity { private static final String TAG = "MainActivity";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    LogManager.getManager(getApplicationContext()).registerActivity(this);
    LogManager.getManager(getApplicationContext()).log(TAG, "onCreate",
            LogUtils.LOG_TYPE_2_FILE_AND_LOGCAT);
    String crashNullException = null;
    crashNullException.charAt(1);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
protected void onDestroy() {
    LogManager.getManager(getApplicationContext()).unregisterActivity(this);
}

}

1.5 step5(optional):

If you need collect the normal log into files or server, you just should call the below method.

LogManager.getManager(getApplicationContext()).log(TAG, "onCreate", LogUtils.LOG_TYPE_2_FILE_AND_LOGCAT);

Enable or disable the log:

public static boolean DEBUG = true;
public static boolean CRASH_SAVE_2_FILE = true;
public static boolean CRASH_UPLOAD_2_NETWORK = false;    

Find the log files:

SD card dir + package name + log/crash + files. Such as:
sd dir/com_forlong401_log/log/log_timestamp.txt       -->normal log
sd dir/com_forlong401_log/crash/crash_timestamp.txt   -->crash log

Advance skills-1:

Encrypt your data:

public final class Utils {

public static String encrypt(String str) {

    // TODO: encrypt data.

    return str;

}

}

Advance skills-2:

Implement your upload method using your server log API:

/**

  • Handle log task.
  • @author Li Cong
  • @date 2014-3-23 */

public class LogTask implements Runnable {

private void log2Network(String tag, String msg) {

    // TODO: Server API for upload message.

    // TODO: Encode and encrypt the message.

}    

}

Copyright, license and contact:

/*

  • Copyright (C) 2014 Li Cong, forlong401@163.com http://www.360qihoo.com
  • Licensed under the Apache License, Version 2.0 (the "License");
  • you may not use this file except in compliance with the License.
  • You may obtain a copy of the License at *
  • http://www.apache.org/licenses/LICENSE-2.0 *
  • Unless required by applicable law or agreed to in writing, software
  • distributed under the License is distributed on an "AS IS" BASIS,
  • WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  • See the License for the specific language governing permissions and
  • limitations under the License. */

日誌收集器

記錄普通或者crash的日誌到檔案或網路伺服器。

  1. 如何使用這個庫?

1.1 步驟一:

新增這些permission到你的manifest檔案中。

<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />

1.2 步驟二:

新增output目錄下的log.jar到你工程的libs目錄下,重新整理。

1.3 步驟三:

在Application中Register 或 unregister crash handler。

/**

  • Log application.
  • @author Li Cong
  • @date 2014-3-23 */

public class LogApp extends Application {

@Override
public void onCreate() {
    super.onCreate();
    LogManager.getManager(getApplicationContext()).registerCrashHandler();
}

@Override
public void onTerminate() {
    super.onTerminate();
    LogManager.getManager(getApplicationContext()).unregisterCrashHandler();
}

}

1.4 步驟四(如果只收集crash,這步驟搞完就ok了):

在每個Activity的onCreate()中Register,onDestroy()中unregister.所有的Activity都要新增。

public class MainActivity extends Activity { private static final String TAG = "MainActivity";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    LogManager.getManager(getApplicationContext()).registerActivity(this);
    LogManager.getManager(getApplicationContext()).log(TAG, "onCreate",
            LogUtils.LOG_TYPE_2_FILE_AND_LOGCAT);
    String crashNullException = null;
    crashNullException.charAt(1);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
protected void onDestroy() {
    LogManager.getManager(getApplicationContext()).unregisterActivity(this);
}

}

1.5 步驟五(打log):

如果你要收集普通的日誌到檔案或者伺服器,那麼呼叫下面的方法即可。

LogManager.getManager(getApplicationContext()).log(TAG, "onCreate", LogUtils.LOG_TYPE_2_FILE_AND_LOGCAT);

開啟或關閉log:

public static boolean DEBUG = true; // 關閉或開啟普通日誌

public static boolean CRASH_SAVE_2_FILE = true;// 關閉或開啟crash日誌寫入檔案。

public static boolean CRASH_UPLOAD_2_NETWORK = false;// 關閉或開啟crash日誌上傳伺服器。

去哪裡找到你的日誌檔案:

SD card 目錄下的包名中點替換為下劃線的資料夾下的 + log/crash + 日誌檔案. 例如:

sd dir/com_forlong401_log/log/log_timestamp.txt       -->普通日誌

sd dir/com_forlong401_log/crash/crash_timestamp.txt   -->crash日誌

高階技能-1:

加密你的資料:

public final class Utils {

public static String encrypt(String str) {

    // TODO: encrypt data.

    return str;

}

}

高階技能-2:

實現你上傳日誌的程式碼:

/**

  • Handle log task.
  • @author Li Cong
  • @date 2014-3-23 */

public class LogTask implements Runnable {

private void log2Network(String tag, String msg) {

    // TODO: Server API for upload message.

    // TODO: Encode and encrypt the message.

}    

}

版權, 授權協議和聯絡方式:

/*

  • Copyright (C) 2014 Li Cong, forlong401@163.com http://www.360qihoo.com
  • Licensed under the Apache License, Version 2.0 (the "License");
  • you may not use this file except in compliance with the License.
  • You may obtain a copy of the License at *
  • http://www.apache.org/licenses/LICENSE-2.0 *
  • Unless required by applicable law or agreed to in writing, software
  • distributed under the License is distributed on an "AS IS" BASIS,
  • WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  • See the License for the specific language governing permissions and
  • limitations under the License. */

相關文章