前言:
專案開始沒有做好日誌統計工作,每次有問題後端都得找前端對接,嚴重影響工作效率。最近特地在專案中加上日誌儲存策略,在此分享,供需要的人學習。
更詳細的日誌資訊
既然決定自定義一個log,那我們就可以讓它顯示更多的資訊,如執行緒資訊:threadId,threadName等:
private String getFunctionName() {
StackTraceElement[] sts = Thread.currentThread().getStackTrace();
if (sts == null) {
return null;
}
for (StackTraceElement st : sts) {
if (st.isNativeMethod()) {
continue;
}
if (st.getClassName().equals(Thread.class.getName())) {
continue;
}
if (st.getClassName().equals(this.getClass().getName())) {
continue;
}
Thread t = Thread.currentThread();
return "[T(id:" + t.getId() +
", name:" + t.getName() +
", priority:" + t.getPriority() +
", groupName:" + t.getThreadGroup().getName() +
"): " + st.getFileName() + ":"
+ st.getLineNumber() + " " + st.getMethodName() + " ]";
}
return "";
}
複製程式碼
StackTrace(堆疊軌跡)存放的就是方法呼叫棧的資訊,我們從中獲取方法執行的執行緒相關的資訊,以及執行的方法名稱等。這些資訊能幫助我們更好的查詢問題之所在。
private void logPrint(int logLevel, Object msg) {
if (isDebug) {
String name = getFunctionName();
customTag = TextUtils.isEmpty(customTag) ? defaultTag : customTag;
Log.println(logLevel, customTag, name + " - " + msg);
}
}
複製程式碼
使用Log.println方法列印相關資訊即可。
日誌儲存策略
後端的人在測試的時候會遇到BUG,有時候不知道到底是前端出了問題還是後端的問題,為了更好更快速的定位,後端應該知道前端的日誌儲存在哪裡。這就需要我們制定一個日誌儲存策略。(即使要上傳日誌,也應該先儲存成檔案再上傳檔案,不然每一條日誌呼叫一次介面,介面的壓力會很大,很不合理)
由於儲存日誌的過程是個耗時過程,我們需要開啟執行緒去儲存。但是日誌產生的頻率可能很高,又不能採用一般的執行緒去處理,太多的執行緒也會損耗效能。所以我們應該考慮佇列的形式儲存日誌,然後一條一條的去儲存。
public void initSaveStrategy(Context context) {
if (saveLogStrategy != null || !isDebug) {
return;
}
final int MAX_BYTES = 1024 * 1024;
String diskPath = Environment.getExternalStorageDirectory().getAbsolutePath();
File cacheFile = context.getCacheDir();
if (cacheFile != null) {
diskPath = cacheFile.getAbsolutePath();
}
String folder = diskPath + File.separatorChar + "log";
HandlerThread ht = new HandlerThread("SohuLiveLogger." + folder);
ht.start();
Handler handler = new SaveLogStrategy.WriteHandler(ht.getLooper(), folder, MAX_BYTES);
saveLogStrategy = new SaveLogStrategy(handler);
}
複製程式碼
public static class WriteHandler extends Handler {
private final String folder;
private final int maxFileSize;
WriteHandler(@NonNull Looper looper, @NonNull String folder, int maxFileSize) {
super(checkNotNull(looper));
this.folder = checkNotNull(folder);
this.maxFileSize = maxFileSize;
}
@Override
public void handleMessage(@NonNull Message msg) {
String content = (String) msg.obj;
FileWriter fileWriter = null;
File logFile = getLogFile(folder, "logs");
try {
fileWriter = new FileWriter(logFile, true);
writeLog(fileWriter, content);
fileWriter.flush();
fileWriter.close();
} catch (IOException e) {
if (fileWriter != null) {
try {
fileWriter.flush();
fileWriter.close();
} catch (IOException e1) {
}
}
}
}
複製程式碼
我們使用HandlerThread來處理這個任務。HandlerThread是一個可以使用handler的Thread。當我們把訊息儲存到訊息佇列中去之後會線上程中去處理,又能保證不會產生很多執行緒。其實這裡也可以使用instentservice實現,這個服務適合量大而不太耗時的任務。
最後在一個方法中統一列印和儲存即可:
private void logPrint(int logLevel, Object msg) {
if (isDebug) {
String name = getFunctionName();
if (saveLogStrategy != null) {
saveLogStrategy.log(Log.ERROR, customTag, name + " - " + msg);
}
Log.println(logLevel, customTag, name + " - " + msg);
}
}
複製程式碼
自定義的log策略還是比較簡單,主要就是這個思想:列印日誌資訊詳細,儲存要採用佇列的形式。一下是全部程式碼:
public class Logger {
public final static String tag = "";
private static SaveLogStrategy saveLogStrategy;
private final static boolean logFlag = true;
private static Logger logger;
private int logLevel = Log.VERBOSE;
private static boolean isDebug = BuildConfig.DEBUG;
private String customTag = null;
private Logger(String customTag) {
this.customTag = customTag;
}
public void initSaveStrategy(Context context) {
if (saveLogStrategy != null || !isDebug) {
return;
}
final int MAX_BYTES = 1024 * 1024;
String diskPath = Environment.getExternalStorageDirectory().getAbsolutePath();
File cacheFile = context.getCacheDir();
if (cacheFile != null) {
diskPath = cacheFile.getAbsolutePath();
}
String folder = diskPath + File.separatorChar + "log";
HandlerThread ht = new HandlerThread("Logger." + folder);
ht.start();
Handler handler = new SaveLogStrategy.WriteHandler(ht.getLooper(), folder, MAX_BYTES);
saveLogStrategy = new SaveLogStrategy(handler);
}
public static Logger getLogger(String tag) {
if (logger == null) {
logger = new Logger(tag);
}
return logger;
}
public static Logger getLogger() {
if (logger == null) {
logger = new Logger(tag);
}
return logger;
}
/**
* Verbose(2) 級別日誌
*
* @param str String
*/
public void v(Object str) {
logLevel = Log.VERBOSE;
logPrint(logLevel, str);
}
/**
* Debug(3) 級別日誌
*
* @param str String
*/
public void d(Object str) {
logLevel = Log.DEBUG;
logPrint(logLevel, str);
}
/**
* Info(4) 級別日誌
*
* @param str String
*/
public void i(Object str) {
logLevel = Log.INFO;
logPrint(logLevel, str);
}
/**
* Warn(5) 級別日誌
*
* @param str String
*/
public void w(Object str) {
logLevel = Log.WARN;
logPrint(logLevel, str);
}
/**
* Error(6) 級別日誌
*
* @param str String
*/
public void e(Object str) {
logLevel = Log.ERROR;
logPrint(logLevel, str);
}
private void logPrint(int logLevel, Object msg) {
if (isDebug) {
String name = getFunctionName();
if (saveLogStrategy != null) {
saveLogStrategy.log(Log.ERROR, customTag, name + " - " + msg);
}
Log.println(logLevel, customTag, name + " - " + msg);
}
}
/**
* 獲取當前方法名
*
* @return 方法名
*/
private String getFunctionName() {
StackTraceElement[] sts = Thread.currentThread().getStackTrace();
if (sts == null) {
return null;
}
for (StackTraceElement st : sts) {
if (st.isNativeMethod()) {
continue;
}
if (st.getClassName().equals(Thread.class.getName())) {
continue;
}
if (st.getClassName().equals(this.getClass().getName())) {
continue;
}
Thread t = Thread.currentThread();
return "[Thread(id:" + t.getId() +
", name:" + t.getName() +
", priority:" + t.getPriority() +
", groupName:" + t.getThreadGroup().getName() +
"): " + st.getFileName() + ":"
+ st.getLineNumber() + " " + st.getMethodName() + " ]";
}
return "";
}
}
複製程式碼
public class SaveLogStrategy {
@NonNull
private final Handler handler;
public SaveLogStrategy(@NonNull Handler handler) {
this.handler = checkNotNull(handler);
}
public void log(int level, @Nullable String tag, @NonNull String message) {
checkNotNull(message);
handler.sendMessage(handler.obtainMessage(level, message));
}
static class WriteHandler extends Handler {
private final String folder;
private final int maxFileSize;
WriteHandler(@NonNull Looper looper, @NonNull String folder, int maxFileSize) {
super(checkNotNull(looper));
this.folder = checkNotNull(folder);
this.maxFileSize = maxFileSize;
}
@SuppressWarnings("checkstyle:emptyblock")
@Override
public void handleMessage(@NonNull Message msg) {
String content = (String) msg.obj;
FileWriter fileWriter = null;
File logFile = getLogFile(folder, "logs");
try {
fileWriter = new FileWriter(logFile, true);
writeLog(fileWriter, content);
fileWriter.flush();
fileWriter.close();
} catch (IOException e) {
if (fileWriter != null) {
try {
fileWriter.flush();
fileWriter.close();
} catch (IOException e1) {
}
}
}
}
private void writeLog(@NonNull FileWriter fileWriter, @NonNull String content) throws IOException {
checkNotNull(fileWriter);
checkNotNull(content);
fileWriter.append("\n").append(content);
}
private File getLogFile(@NonNull String folderName, @NonNull String fileName) {
checkNotNull(folderName);
checkNotNull(fileName);
File folder = new File(folderName);
if (!folder.exists()) {
if (!folder.mkdirs()) {
Log.println(Log.ERROR, "saveLog", "檔案未建立成功,可能是讀寫許可權沒給");
}
}
int newFileCount = 0;
File newFile;
File existingFile = null;
newFile = new File(folder, String.format("%s_%s.txt", fileName, newFileCount));
while (newFile.exists()) {
existingFile = newFile;
newFileCount++;
newFile = new File(folder, String.format("%s_%s.txt", fileName, newFileCount));
}
if (existingFile != null) {
if (existingFile.length() >= maxFileSize) {
return newFile;
}
return existingFile;
}
return newFile;
}
}
}
複製程式碼
以上就是全部內容,希望對大家有所幫助