improve logging
This commit is contained in:
parent
f2022c091f
commit
586b97e17b
1 changed files with 50 additions and 15 deletions
|
@ -1,53 +1,88 @@
|
|||
package proxy.util;
|
||||
|
||||
import java.io.PrintWriter;
|
||||
import java.io.StringWriter;
|
||||
import java.text.DateFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.Locale;
|
||||
|
||||
import proxy.Proxy;
|
||||
|
||||
public class Log {
|
||||
private static final boolean DEBUG = System.getProperty("log.debug") != null;
|
||||
private static enum LogLevel {
|
||||
ERROR("ERROR", 31),
|
||||
WARN("WARN", 33),
|
||||
INFO("INFO", 32),
|
||||
DEBUG("DEBUG", 35);
|
||||
|
||||
private final String name;
|
||||
private final int color;
|
||||
|
||||
private LogLevel(String name, int color) {
|
||||
this.name = name;
|
||||
this.color = color;
|
||||
}
|
||||
}
|
||||
|
||||
private static void log(String prefix, String msg) {
|
||||
System.err.printf("[%s|%s] %s\n", prefix, Thread.currentThread().getName(), msg);
|
||||
private static final boolean DEBUG = System.getProperty("log.debug") != null;
|
||||
private static final boolean COLORS = !System.getProperty("os.name").toLowerCase(Locale.US).contains("win") && System.getProperty("log.nocolor") == null;
|
||||
private static final DateFormat FORMAT = new SimpleDateFormat("HH:mm:ss");
|
||||
|
||||
private static void log(LogLevel level, String msg) {
|
||||
System.err.printf(COLORS ? "\u001b[34m[%s]\u001b[m \u001b[" + level.color + "m[%s/%s]\u001b[m \u001b[36m(" + Proxy.NAME + ")\u001b[m \u001b[0m%s\n" : "[%s] [%s/%s] (" + Proxy.NAME + ") %s\n",
|
||||
FORMAT.format(new Date()), Thread.currentThread().getName(), level.name, msg);
|
||||
}
|
||||
|
||||
public static void error(String msg) {
|
||||
log("error", msg);
|
||||
log(LogLevel.ERROR, msg);
|
||||
}
|
||||
|
||||
public static void error(String fmt, Object ... args) {
|
||||
log("error", String.format(fmt, args));
|
||||
log(LogLevel.ERROR, String.format(fmt, args));
|
||||
}
|
||||
|
||||
public static void error(Throwable t, String msg) {
|
||||
log("error", msg);
|
||||
t.printStackTrace(System.err);
|
||||
log(LogLevel.ERROR, msg);
|
||||
StringWriter sw = new StringWriter();
|
||||
PrintWriter st = new PrintWriter(sw);
|
||||
t.printStackTrace(st);
|
||||
sw.flush();
|
||||
log(LogLevel.ERROR, sw.toString().trim().replace("\t", " "));
|
||||
}
|
||||
|
||||
public static void error(Throwable t, String fmt, Object ... args) {
|
||||
log("error", String.format(fmt, args));
|
||||
t.printStackTrace(System.err);
|
||||
log(LogLevel.ERROR, String.format(fmt, args));
|
||||
StringWriter sw = new StringWriter();
|
||||
PrintWriter st = new PrintWriter(sw);
|
||||
t.printStackTrace(st);
|
||||
sw.flush();
|
||||
log(LogLevel.ERROR, sw.toString().trim().replace("\t", " "));
|
||||
}
|
||||
|
||||
public static void warn(String msg) {
|
||||
log("warn", msg);
|
||||
log(LogLevel.WARN, msg);
|
||||
}
|
||||
|
||||
public static void warn(String fmt, Object ... args) {
|
||||
log("warn", String.format(fmt, args));
|
||||
log(LogLevel.WARN, String.format(fmt, args));
|
||||
}
|
||||
|
||||
public static void info(String msg) {
|
||||
log("info", msg);
|
||||
log(LogLevel.INFO, msg);
|
||||
}
|
||||
|
||||
public static void info(String fmt, Object ... args) {
|
||||
log("info", String.format(fmt, args));
|
||||
log(LogLevel.INFO, String.format(fmt, args));
|
||||
}
|
||||
|
||||
public static void debug(String msg) {
|
||||
if(DEBUG)
|
||||
log("debug", msg);
|
||||
log(LogLevel.DEBUG, msg);
|
||||
}
|
||||
|
||||
public static void debug(String fmt, Object ... args) {
|
||||
if(DEBUG)
|
||||
log("debug", String.format(fmt, args));
|
||||
log(LogLevel.DEBUG, String.format(fmt, args));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue