实际项目开发中,发现许多开发人员(特别是新人)不喜欢记录日志,主要原因可能是目前的日志器都需要从日志工厂声明好日志对象然后才能使用,以apache logging为例:
Log log = LogFactory.getLog("XXXXX");//声明日志器
这一点让开发人员感觉麻烦,甚至于整个项目中经常出现sysout打印,严重影响性能和可控性。那么,为什么日志器不能仅仅是一系列的静态方法并且使用超简单的类型声明呢(如Logs)?答案是可以的(什么?性能问题?看看我的实现的,不会有太大的性能问题
)。Logs类做到了这一点,代码如下:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.Marker;
import org.slf4j.spi.LocationAwareLogger;
/**
* 通用日志器,依赖于slf4j的API,最大限度优化性能
*
*
*/
public class Logs {
private static final Object[] EMPTY_ARRAY = new Object[] {};
private static final String FQCN = Logs.class.getName();
/**
* 获取适配日志器,供内部调用
*
* @return
*/
@SuppressWarnings("restriction")
private static LocationAwareLogger getLocationAwareLogger(final int depth) {
String className = sun.reflect.Reflection.getCallerClass(depth)
.getName();
return (LocationAwareLogger) LoggerFactory.getLogger(className);
}
/**
* 静态的获取日志器
*
* @return
*/
public static Logger getLogger() {
return getLocationAwareLogger(3);
}
public static String getName() {
return getLocationAwareLogger(3).getName();
}
public static boolean isTraceEnabled() {
return getLocationAwareLogger(3).isTraceEnabled();
}
public static void trace(String msg) {
getLocationAwareLogger(3).log(null, FQCN,
LocationAwareLogger.TRACE_INT, msg, EMPTY_ARRAY, null);
}
public static void trace(String format, Object arg) {
getLocationAwareLogger(3).log(null, FQCN,
LocationAwareLogger.TRACE_INT, format, new Object[] { arg },
null);
}
public static void trace(String format, Object arg1, Object arg2) {
getLocationAwareLogger(3).log(null, FQCN,
LocationAwareLogger.TRACE_INT, format,
new Object[] { arg1, arg2 }, null);
}
public static void trace(String format, Object... arguments) {
getLocationAwareLogger(3).log(null, FQCN,
LocationAwareLogger.TRACE_INT, format, arguments, null);
}
public static void trace(String msg, Throwable t) {
getLocationAwareLogger(3).log(null, FQCN,
LocationAwareLogger.TRACE_INT, msg, EMPTY_ARRAY, t);
}
public static boolean isTraceEnabled(Marker marker) {
return getLocationAwareLogger(3).isTraceEnabled(marker);
}
public static void trace(Marker marker, String msg) {
getLocationAwareLogger(3).log(marker, FQCN,
LocationAwareLogger.TRACE_INT, msg, EMPTY_ARRAY, null);
}
public static void trace(Marker marker, String format, Object arg) {
getLocationAwareLogger(3).log(marker, FQCN,
LocationAwareLogger.TRACE_INT, format, new Object[] { arg },
null);
}
public static void trace(Marker marker, String format, Object arg1,
Object arg2) {
getLocationAwareLogger(3).log(marker, FQCN,
LocationAwareLogger.TRACE_INT, format,
new Object[] { arg1, arg2 }, null);
}
public static void trace(Marker marker, String format, Object... argArray) {
getLocationAwareLogger(3).log(marker, FQCN,
LocationAwareLogger.TRACE_INT, format, argArray, null);
}
public static void trace(Marker marker, String msg, Throwable t) {
getLocationAwareLogger(3).log(marker, FQCN,
LocationAwareLogger.TRACE_INT, msg, EMPTY_ARRAY, t);
}
public static boolean isInfoEnabled() {
return getLocationAwareLogger(3).isInfoEnabled();
}
public static void info(String msg) {
getLocationAwareLogger(3).log(null, FQCN, LocationAwareLogger.INFO_INT,
msg, EMPTY_ARRAY, null);
}
public static void info(String format, Object arg) {
getLocationAwareLogger(3).log(null, FQCN, LocationAwareLogger.INFO_INT,
format, new Object[] { arg }, null);
}
public static void info(String format, Object arg1, Object arg2) {
getLocationAwareLogger(3).log(null, FQCN, LocationAwareLogger.INFO_INT,
format, new Object[] { arg1, arg2 }, null);
}
public static void info(String format, Object... arguments) {
getLocationAwareLogger(3).log(null, FQCN, LocationAwareLogger.INFO_INT,
format, arguments, null);
}
public static void info(String msg, Throwable t) {
getLocationAwareLogger(3).log(null, FQCN, LocationAwareLogger.INFO_INT,
msg, EMPTY_ARRAY, t);
}
public static boolean isInfoEnabled(Marker marker) {
return getLocationAwareLogger(3).isInfoEnabled(marker);
}
public static void info(Marker marker, String msg) {
getLocationAwareLogger(3).log(marker, FQCN,
LocationAwareLogger.INFO_INT, msg, EMPTY_ARRAY, null);
}
public static void info(Marker marker, String format, Object arg) {
getLocationAwareLogger(3).log(marker, FQCN,
LocationAwareLogger.INFO_INT, format, new Object[] { arg },
null);
}
public static void info(Marker marker, String format, Object arg1,
Object arg2) {
getLocationAwareLogger(3).log(marker, FQCN,
LocationAwareLogger.INFO_INT, format,
new Object[] { arg1, arg2 }, null);
}
public static void info(Marker marker, String format, Object... argArray) {
getLocationAwareLogger(3).log(marker, FQCN,
LocationAwareLogger.INFO_INT, format, argArray, null);
}
public static void info(Marker marker, String msg, Throwable t) {
getLocationAwareLogger(3).log(marker, FQCN,
LocationAwareLogger.INFO_INT, msg, EMPTY_ARRAY, t);
}
public static boolean isDebugEnabled() {
return getLocationAwareLogger(3).isDebugEnabled();
}
public static void debug(String msg) {
getLocationAwareLogger(3).log(null, FQCN,
LocationAwareLogger.DEBUG_INT, msg, EMPTY_ARRAY, null);
}
public static void debug(String format, Object arg) {
getLocationAwareLogger(3).log(null, FQCN,
LocationAwareLogger.DEBUG_INT, format, new Object[] { arg },
null);
}
public static void debug(String format, Object arg1, Object arg2) {
getLocationAwareLogger(3).log(null, FQCN,
LocationAwareLogger.DEBUG_INT, format,
new Object[] { arg1, arg2 }, null);
}
public static void debug(String format, Object... arguments) {
getLocationAwareLogger(3).log(null, FQCN,
LocationAwareLogger.DEBUG_INT, format, arguments, null);
}
public static void debug(String msg, Throwable t) {
getLocationAwareLogger(3).log(null, FQCN,
LocationAwareLogger.DEBUG_INT, msg, EMPTY_ARRAY, t);
}
public static boolean isDebugEnabled(Marker marker) {
return getLocationAwareLogger(3).isDebugEnabled(marker);
}
public static void debug(Marker marker, String msg) {
getLocationAwareLogger(3).log(marker, FQCN,
LocationAwareLogger.DEBUG_INT, msg, EMPTY_ARRAY, null);
}
public static void debug(Marker marker, String format, Object arg) {
getLocationAwareLogger(3).log(marker, FQCN,
LocationAwareLogger.DEBUG_INT, format, new Object[] { arg },
null);
}
public static void debug(Marker marker, String format, Object arg1,
Object arg2) {
getLocationAwareLogger(3).log(marker, FQCN,
LocationAwareLogger.DEBUG_INT, format,
new Object[] { arg1, arg2 }, null);
}
public static void debug(Marker marker, String format, Object... argArray) {
getLocationAwareLogger(3).log(marker, FQCN,
LocationAwareLogger.DEBUG_INT, format, argArray, null);
}
public static void debug(Marker marker, String msg, Throwable t) {
getLocationAwareLogger(3).log(marker, FQCN,
LocationAwareLogger.DEBUG_INT, msg, EMPTY_ARRAY, t);
}
public static boolean isWarnEnabled() {
return getLocationAwareLogger(3).isWarnEnabled();
}
public static void warn(String msg) {
getLocationAwareLogger(3).log(null, FQCN, LocationAwareLogger.WARN_INT,
msg, EMPTY_ARRAY, null);
}
public static void warn(String format, Object arg) {
getLocationAwareLogger(3).log(null, FQCN, LocationAwareLogger.WARN_INT,
format, new Object[] { arg }, null);
}
public static void warn(String format, Object arg1, Object arg2) {
getLocationAwareLogger(3).log(null, FQCN, LocationAwareLogger.WARN_INT,
format, new Object[] { arg1, arg2 }, null);
}
public static void warn(String format, Object... arguments) {
getLocationAwareLogger(3).log(null, FQCN, LocationAwareLogger.WARN_INT,
format, arguments, null);
}
public static void warn(String msg, Throwable t) {
getLocationAwareLogger(3).log(null, FQCN, LocationAwareLogger.WARN_INT,
msg, EMPTY_ARRAY, t);
}
public static boolean isWarnEnabled(Marker marker) {
return getLocationAwareLogger(3).isWarnEnabled(marker);
}
public static void warn(Marker marker, String msg) {
getLocationAwareLogger(3).log(marker, FQCN,
LocationAwareLogger.WARN_INT, msg, EMPTY_ARRAY, null);
}
public static void warn(Marker marker, String format, Object arg) {
getLocationAwareLogger(3).log(marker, FQCN,
LocationAwareLogger.WARN_INT, format, new Object[] { arg },
null);
}
public static void warn(Marker marker, String format, Object arg1,
Object arg2) {
getLocationAwareLogger(3).log(marker, FQCN,
LocationAwareLogger.WARN_INT, format,
new Object[] { arg1, arg2 }, null);
}
public static void warn(Marker marker, String format, Object... argArray) {
getLocationAwareLogger(3).log(marker, FQCN,
LocationAwareLogger.WARN_INT, format, argArray, null);
}
public static void warn(Marker marker, String msg, Throwable t) {
getLocationAwareLogger(3).log(marker, FQCN,
LocationAwareLogger.WARN_INT, msg, EMPTY_ARRAY, t);
}
public static boolean isErrorEnabled() {
return getLocationAwareLogger(3).isErrorEnabled();
}
public static void error(String msg) {
getLocationAwareLogger(3).log(null, FQCN,
LocationAwareLogger.ERROR_INT, msg, EMPTY_ARRAY, null);
}
public static void error(String format, Object arg) {
getLocationAwareLogger(3).log(null, FQCN,
LocationAwareLogger.ERROR_INT, format, new Object[] { arg },
null);
}
public static void error(String format, Object arg1, Object arg2) {
getLocationAwareLogger(3).log(null, FQCN,
LocationAwareLogger.ERROR_INT, format,
new Object[] { arg1, arg2 }, null);
}
public static void error(String format, Object... arguments) {
getLocationAwareLogger(3).log(null, FQCN,
LocationAwareLogger.ERROR_INT, format, arguments, null);
}
public static void error(String msg, Throwable t) {
getLocationAwareLogger(3).log(null, FQCN,
LocationAwareLogger.ERROR_INT, msg, EMPTY_ARRAY, t);
}
public static boolean isErrorEnabled(Marker marker) {
return getLocationAwareLogger(3).isErrorEnabled(marker);
}
public static void error(Marker marker, String msg) {
getLocationAwareLogger(3).log(marker, FQCN,
LocationAwareLogger.ERROR_INT, msg, EMPTY_ARRAY, null);
}
public static void error(Marker marker, String format, Object arg) {
getLocationAwareLogger(3).log(marker, FQCN,
LocationAwareLogger.ERROR_INT, format, new Object[] { arg },
null);
}
public static void error(Marker marker, String format, Object arg1,
Object arg2) {
getLocationAwareLogger(3).log(marker, FQCN,
LocationAwareLogger.ERROR_INT, format,
new Object[] { arg1, arg2 }, null);
}
public static void error(Marker marker, String format, Object... argArray) {
getLocationAwareLogger(3).log(marker, FQCN,
LocationAwareLogger.ERROR_INT, format, argArray, null);
}
public static void error(Marker marker, String msg, Throwable t) {
getLocationAwareLogger(3).log(marker, FQCN,
LocationAwareLogger.ERROR_INT, msg, EMPTY_ARRAY, t);
}
}
之所以说这个类性能突出,秘诀在于sun.reflect.Reflection.getCallerClass这个方法。当然了,这个还是有些性能损耗的,如果实在觉得某些场景Logs影响了性能,那么可以在换回传统做法嘛。相信这种场景出现的几率是极小的。