Skip to content

Instantly share code, notes, and snippets.

@bryanmaina
Last active August 20, 2018 09:23
Show Gist options
  • Save bryanmaina/a3b44ad1229e17d0186881340aa8fe9b to your computer and use it in GitHub Desktop.
Save bryanmaina/a3b44ad1229e17d0186881340aa8fe9b to your computer and use it in GitHub Desktop.
package com.example.tools;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
public class ThreadStackPrinter {
private static Gson gson = new GsonBuilder()
.setPrettyPrinting()
.serializeNulls()
.enableComplexMapKeySerialization()
.create();
public static void printObjectAsJson(Object object) {
StackTraceElement callingMethodStack = Thread.currentThread().getStackTrace()[2]; // method from which we are printing
System.out.println(new StringBuilder()
.append("====================== ").append(callingMethodStack.toString()).append(" ==========================")
.append(System.lineSeparator())
.append(gson.toJson(object))
.append(System.lineSeparator())
.append("================================================"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment