Add log utils
This commit is contained in:
25
app/src/main/java/com/github/gotify/log/Format.java
Normal file
25
app/src/main/java/com/github/gotify/log/Format.java
Normal file
@@ -0,0 +1,25 @@
|
||||
package com.github.gotify.log;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import com.hypertrack.hyperlog.LogFormat;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
public class Format extends LogFormat {
|
||||
Format(Context context) {
|
||||
super(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getFormattedLogMessage(
|
||||
String logLevelName,
|
||||
String tag,
|
||||
String message,
|
||||
String timeStamp,
|
||||
String senderName,
|
||||
String osVersion,
|
||||
String deviceUUID) {
|
||||
return String.format(Locale.ENGLISH, "%s %s: %s", timeStamp, logLevelName, message);
|
||||
}
|
||||
}
|
||||
52
app/src/main/java/com/github/gotify/log/Log.java
Normal file
52
app/src/main/java/com/github/gotify/log/Log.java
Normal file
@@ -0,0 +1,52 @@
|
||||
package com.github.gotify.log;
|
||||
|
||||
import android.content.Context;
|
||||
import android.text.TextUtils;
|
||||
|
||||
import com.hypertrack.hyperlog.HyperLog;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public class Log {
|
||||
private static String TAG = "gotify";
|
||||
|
||||
public static void init(Context content) {
|
||||
HyperLog.initialize(content, new Format(content));
|
||||
HyperLog.setLogLevel(android.util.Log.INFO); // TODO configurable
|
||||
}
|
||||
|
||||
public static String get() {
|
||||
List<String> logs = HyperLog.getDeviceLogsAsStringList(false);
|
||||
Collections.reverse(logs);
|
||||
return TextUtils.join("\n", logs.subList(0, Math.min(200, logs.size())));
|
||||
}
|
||||
|
||||
public static void e(String message) {
|
||||
HyperLog.e(TAG, message);
|
||||
}
|
||||
|
||||
public static void e(String message, Throwable e) {
|
||||
HyperLog.e(TAG, message + '\n' + android.util.Log.getStackTraceString(e));
|
||||
}
|
||||
|
||||
public static void i(String message) {
|
||||
HyperLog.i(TAG, message);
|
||||
}
|
||||
|
||||
public static void i(String message, Throwable e) {
|
||||
HyperLog.i(TAG, message + '\n' + android.util.Log.getStackTraceString(e));
|
||||
}
|
||||
|
||||
public static void w(String message) {
|
||||
HyperLog.w(TAG, message);
|
||||
}
|
||||
|
||||
public static void w(String message, Throwable e) {
|
||||
HyperLog.w(TAG, message + '\n' + android.util.Log.getStackTraceString(e));
|
||||
}
|
||||
|
||||
public static void clear() {
|
||||
HyperLog.deleteLogs();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.github.gotify.log;
|
||||
|
||||
public class UncaughtExceptionHandler {
|
||||
public static void registerCurrentThread() {
|
||||
Thread.setDefaultUncaughtExceptionHandler((t, e) -> Log.e("uncaught exception", e));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user