Skip to content

Instantly share code, notes, and snippets.

@birdeveloper
Created January 26, 2022 22:15
Show Gist options
  • Save birdeveloper/ed086381df94511a3f814ad5f8d55d96 to your computer and use it in GitHub Desktop.
Save birdeveloper/ed086381df94511a3f814ad5f8d55d96 to your computer and use it in GitHub Desktop.
android log util file with java
import android.util.Log;
import com.google.gson.Gson;
import com.app.name.BuildConfig;
/**
* This class defines the Logger
*/
public final class Logger {
private static final String TAG = "PENTI-LOG";
private static final boolean LOG_ENABLE = BuildConfig.DEBUG;
private static final boolean DETAIL_ENABLE = BuildConfig.DEBUG;
private Logger() {
}
private static String buildMsg(String msg) {
StringBuilder buffer = new StringBuilder();
if (DETAIL_ENABLE) {
final StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[4];
buffer.append("[ ");
buffer.append(Thread.currentThread().getName());
buffer.append(": ");
buffer.append(stackTraceElement.getFileName());
buffer.append(": ");
buffer.append(stackTraceElement.getLineNumber());
buffer.append(": ");
buffer.append(stackTraceElement.getMethodName());
}
buffer.append("() ] _____ ");
buffer.append(msg);
return buffer.toString();
}
public static void v(String msg) {
if (LOG_ENABLE && Log.isLoggable(TAG, Log.VERBOSE)) {
Log.v(TAG, buildMsg(msg));
}
}
public static void v(Object data) {
if (LOG_ENABLE && Log.isLoggable(TAG, Log.ERROR)) {
Log.v(TAG, buildMsg(new Gson().toJson(data)));
}
}
public static void d(String msg) {
if (LOG_ENABLE && Log.isLoggable(TAG, Log.DEBUG)) {
Log.d(TAG, buildMsg(msg));
}
}
public static void d(Object data) {
if (LOG_ENABLE && Log.isLoggable(TAG, Log.ERROR)) {
Log.d(TAG, buildMsg(new Gson().toJson(data)));
}
}
public static void i(String msg) {
if (LOG_ENABLE && Log.isLoggable(TAG, Log.INFO)) {
Log.i(TAG, buildMsg(msg));
}
}
public static void i(Object data) {
if (LOG_ENABLE && Log.isLoggable(TAG, Log.ERROR)) {
Log.i(TAG, buildMsg(new Gson().toJson(data)));
}
}
public static void w(String msg) {
if (LOG_ENABLE && Log.isLoggable(TAG, Log.WARN)) {
Log.w(TAG, buildMsg(msg));
}
}
public static void w(String msg, Exception e) {
if (LOG_ENABLE && Log.isLoggable(TAG, Log.WARN)) {
Log.w(TAG, buildMsg(msg), e);
}
}
public static void w(Object data) {
if (LOG_ENABLE && Log.isLoggable(TAG, Log.ERROR)) {
Log.w(TAG, buildMsg(new Gson().toJson(data)));
}
}
public static void e(String msg) {
if (LOG_ENABLE && Log.isLoggable(TAG, Log.ERROR)) {
Log.e(TAG, buildMsg(msg));
}
}
public static void e(String msg, Exception e) {
if (LOG_ENABLE && Log.isLoggable(TAG, Log.ERROR)) {
Log.e(TAG, buildMsg(msg), e);
}
}
public static void e(Object data) {
if (LOG_ENABLE && Log.isLoggable(TAG, Log.ERROR)) {
Log.e(TAG, buildMsg(new Gson().toJson(data)));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment