Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Java utility class to get Logs.
// INTERFACE:
public interface ILogger {
public static void error(String msg, Object obj) {
System.out.println(msg + " " + obj.toString());
}
public static void warning(String msg, Object obj) {
System.out.println(msg + " " + obj.toString());
}
public static void info(String msg, Object obj) {
System.out.println(msg + " " + obj.toString());
}
public static void error(String msg) {
System.out.println(msg);
}
public static void warning(String msg) {
System.out.println(msg);
}
public static void info(String msg) {
System.out.println(msg);
}
}
// CLASS:
public class Logger implements ILogger {
private static final String ERROR = "ERROR: ";
private static final String WARNING = "WARNING: ";
private static final String INFO = "INFO: ";
private static final String ANSI_RED = "\u001B[31m";
private static final String ANSI_BLUE = "\u001B[34m";
private static final String ANSI_YELLOW = "\u001B[33m";
private static final String ANSI_RESET = "\u001B[0m";
public static void error(String msg, Object obj) {
String message = ANSI_RED + ERROR + ANSI_RESET + msg;
log(message, obj);
}
public static void warning(String msg, Object obj) {
String message = ANSI_YELLOW + WARNING + ANSI_RESET + msg;
log(message, obj);
}
public static void info(String msg, Object obj) {
String message = ANSI_BLUE + INFO + ANSI_RESET + msg;
log(message, obj);
}
public static void error(String msg) {
error(msg, null);
}
public static void warning(String msg) {
warning(msg, null);
}
public static void info(String msg) {
info(msg, null);
}
private static void log(String msg, Object obj) {
if (obj != null) {
System.out.println(msg + " :: " + obj.toString());
return;
}
System.out.println(msg);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment