Skip to content

Instantly share code, notes, and snippets.

@Bloody-Badboy
Created December 21, 2018 16:15
Show Gist options
  • Save Bloody-Badboy/b134b5134fd4d14ded8aee6f10b66f45 to your computer and use it in GitHub Desktop.
Save Bloody-Badboy/b134b5134fd4d14ded8aee6f10b66f45 to your computer and use it in GitHub Desktop.
Logging utility for java
import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Created by bloodybadboycreation on 17/9/17.
*/
public class Log {
private static final int TAG_CALL_STACK_INDEX = 4;
private static final Pattern ANONYMOUS_CLASS = Pattern.compile("(\\$\\d+)+$");
private static final Log INSTANCE = new Log();
private Log() {
}
public static void log(String message, Object... args) {
INSTANCE.prepareLog(null, false, message, args);
}
public static void log(Throwable t, String message, Object... args) {
INSTANCE.prepareLog(t, false, message, args);
}
public static void err(String message, Object... args) {
INSTANCE.prepareLog(null, true, message, args);
}
public static void err(Throwable t, String message, Object... args) {
INSTANCE.prepareLog(t, true, message, args);
}
private static String getStackTraceString(Throwable t) {
StringWriter sw = new StringWriter(256);
PrintWriter pw = new PrintWriter(sw, false);
t.printStackTrace(pw);
pw.flush();
return sw.toString();
}
private void prepareLog(Throwable t, boolean error, String message, Object... args) {
String tag = getTag();
if (message != null && message.length() == 0) {
message = null;
}
if (message == null) {
if (t == null) {
return; // Swallow message if it's null and there's no throwable.
}
message = getStackTraceString(t);
} else {
if (args != null && args.length > 0) {
message = String.format(message, args);
}
if (t != null) {
message += "\n" + getStackTraceString(t);
}
}
printLog(tag, message, error);
}
private void printLog(String tag, String message, boolean error) {
String log;
try {
log = new SimpleDateFormat("MM-dd HH:mm:ss.SSS").format(new Date(System.currentTimeMillis())) + " - " + tag + "." + Thread.currentThread().getStackTrace()[4].getMethodName() + " : " + message;
} catch (Exception e) {
log = new SimpleDateFormat("MM-dd HH:mm:ss.SSS").format(new Date(System.currentTimeMillis())) + " - " + tag + " : " + message;
}
if (error) {
System.err.println(log);
} else {
System.out.println(log);
}
}
private String getTag() {
StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
if (stackTrace.length <= TAG_CALL_STACK_INDEX) {
throw new IllegalStateException("Synthetic stacktrace didn't have enough elements");
}
String tag = stackTrace[TAG_CALL_STACK_INDEX].getClassName();
Matcher m = ANONYMOUS_CLASS.matcher(tag);
if (m.find()) {
tag = m.replaceAll("");
}
tag = tag.substring(tag.lastIndexOf('.') + 1);
return tag;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment