Skip to content

Instantly share code, notes, and snippets.

@warmwaffles
Created September 9, 2013 02:51
Show Gist options
  • Save warmwaffles/6490969 to your computer and use it in GitHub Desktop.
Save warmwaffles/6490969 to your computer and use it in GitHub Desktop.
import java.io.PrintStream;
/**
* @formatter:off
* @author Matthew A. Johnston [warmwaffles@gmail.com]
*/
public class Logger {
public static final int NONE = 0;
public static final int ERROR = 1;
public static final int WARN = 2;
public static final int INFO = 3;
public static final int DEBUG = 4;
private static PrintStream out = System.out;
private static int currentLevel = INFO;
/**
* Sets the logger level.
*
* @param level
*/
public static void setLevel(int level) {
if (level >= NONE && level <= DEBUG) {
currentLevel = level;
}
}
/**
* Does not follow the logger levels. It simply outputs to the console
* regardless.
*
* @param message
* @param args
*/
public static void log(String message, Object... args) {
printf("LOG", message, args);
}
/**
* Log an info message. If no formatting is desired, don't pass extra
* arguments.
*
* @param message
* @param args
*/
public static void info(String message, Object... args) {
if (currentLevel >= INFO) {
printf("INFO", message, args);
}
}
/**
* Log an error message. If no formatting is desired, don't pass extra
* arguments.
*
* @param message
* @param args
*/
public static void error(String message, Object... args) {
if (currentLevel >= ERROR) {
printf("ERROR", message, args);
}
}
/**
* Log a debug message. If no formatting is desired, don't pass extra
* arguments.
*
* @param message
* @param args
*/
public static void debug(String message, Object... args) {
if (currentLevel >= DEBUG) {
printf("DEBUG", message, args);
}
}
/**
* @param message
* @param args
*/
public static void warn(String message, Object... args) {
if (currentLevel >= WARN) {
printf("WARN", message, args);
}
}
/**
* @param tag
* @param format
* @param args
*/
private static void printf(String tag, String format, Object... args) {
if (args.length == 0) {
out.printf("%s: %s\n", tag, format);
} else {
out.printf("%s: %s\n", tag, String.format(format, args));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment