我開源的Android日誌收集器
轉自: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.
- 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 步驟一:
新增這些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. */
相關文章
- apple/GCGC:蘋果開源其視覺化 Java 垃圾收集器日誌的工具APPGC蘋果視覺化Java
- 日誌收集器Filebeat詳解
- 日誌分析平臺ELK之日誌收集器filebeat
- 最好用的開源日誌分析工具
- 日誌分析平臺ELK之日誌收集器logstash
- 5 個有用的開源日誌分析工具
- 我的管理日誌【一】
- Android開發:日誌功能備忘Android
- 日誌分析平臺ELK之日誌收集器logstash常用外掛配置
- GoAccess 開源日誌輕工具部署與分析Go
- 開源日誌框架TNT4J · GitHub框架Github
- 開源日誌記錄元件Log4Net的使用元件
- 開源元件ELK日誌系統配置與管理元件
- 5款Java開源日誌框架大比拼Java框架
- Android日誌Log使用Android
- Android除錯----日誌Android除錯
- 我最新的慢日誌去哪了?
- 我的MYSQL學習心得(15) : 日誌MySql
- 比較開源日誌:Logstash、FluentD 和 Fluent Bit
- 日誌開篇
- Android-Crash日誌抓取Android
- Android ANR日誌分析指南Android
- 木頭的開發日誌
- 開源!非凸Rust高效能日誌庫ftlogRust
- 開源高效能結構化日誌模組NanoLogNaN
- 開發日誌5
- 開發日誌8
- 開發日誌7
- 開發日誌10
- 開發日誌9
- Avalonia開發日誌
- Android的log日誌知識點剖析Android
- Logan:美團點評的開源移動端基礎日誌庫
- go開發屬於自己的日誌庫-日誌庫優化Go優化
- lumen cli日誌和普通日誌分開儲存
- android Activity崩潰日誌收集Android
- django開發-log日誌的配置Django
- 啟動tomcat時,日誌裡大量輸出建立資料來源dataSource的日誌Tomcat