Created
November 16, 2013 01:28
-
-
Save hamiltont/7494673 to your computer and use it in GitHub Desktop.
A one-file logger. Can easily be replaced with a more advanced system, but useful for starting
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* <p> | |
* This is a one-file logger that can be used to get you started quickly. You | |
* can send it any array of objects that it will print to stdout/stderr. | |
* Reflection is used to print the class/line/method. QuickLogger is pretty darn | |
* slow, so don't use it in production. | |
* </p> | |
* <p> | |
* All methods are static, and you can (if your IDE supports it, which most do) | |
* use static imports to make your life even easier | |
* </p> | |
* <code> | |
* import static QuickLogger.*; | |
* </code> | |
* | |
* <p> | |
* Using QuickLogger is pretty easy | |
* </p> | |
* <code> | |
* err("I am logging", 100, '%', "of the", new Object()); | |
* </code> | |
* | |
* <p> | |
* You can configure a few items, such as <code>QuickLogger.printClass_</code>, | |
* <code>QuickLogger.printMethod_</code>, etc | |
* </p> | |
* | |
* | |
* @author hamiltont | |
* | |
*/ | |
public class QuickLogger { | |
public interface LogListener { | |
public void onLog(Level l, String message); | |
} | |
private static LogListener listener_; | |
public void setListener(LogListener l) { | |
listener_ = l; | |
} | |
private static boolean printClass_ = true, printMethod_ = true, | |
printStdOut_ = true, printStdErr_ = false, printLine_ = true; | |
private static String separator_ = " "; | |
public void setSeparator(String separator) { | |
separator_ = separator; | |
} | |
public void setPrint(boolean printStdOut, boolean printStdErr, | |
boolean printLine, boolean printClass, boolean printMethod) { | |
printClass_ = printClass; | |
printMethod_ = printMethod; | |
printLine_ = printLine; | |
printStdOut_ = printStdOut; | |
printStdErr_ = printStdErr; | |
} | |
public enum Level { | |
ERROR, WARN, DEBUG, INFO, VERBOSE | |
} | |
static StringBuilder builderCache = new StringBuilder(512); | |
private static void log(Level l, Object... log) { | |
if (log.length == 0) | |
return; | |
builderCache.delete(0, builderCache.length()); | |
StackTraceElement[] st = new Exception().getStackTrace(); | |
StackTraceElement caller = st[2]; | |
String className = caller.getClassName(); | |
String methodName = caller.getMethodName(); | |
int lineNumber = caller.getLineNumber(); | |
if (printLine_) | |
builderCache.append(lineNumber).append(":"); | |
if (printClass_) | |
builderCache.append(className); | |
if (printMethod_) | |
builderCache.append('#').append(methodName); | |
for (Object toLog : log) | |
builderCache.append(separator_).append(toLog.toString()); | |
if (printStdOut_) | |
System.out.println(builderCache.toString()); | |
if (printStdErr_) | |
System.err.println(builderCache.toString()); | |
if (listener_ != null) | |
listener_.onLog(l, builderCache.toString()); | |
} | |
public static void err(Object... log) { | |
log(Level.ERROR, log); | |
} | |
public static void debug(Object... log) { | |
log(Level.DEBUG, log); | |
} | |
public static void info(Object... log) { | |
log(Level.INFO, log); | |
} | |
public static void verbose(Object... log) { | |
log(Level.VERBOSE, log); | |
} | |
public static void warn(Object... log) { | |
log(Level.WARN, log); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment