cn.lkk.web.util.LogUtil Maven / Gradle / Ivy
The newest version!
package cn.lkk.web.util;
import java.util.HashMap;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author wangfujun
* @date 2018/12/18
*/
public class LogUtil {
private static Map, Logger> logMap = new HashMap, Logger>();
public static void log(Class> clazz, String msg) {
Logger log = findLogger(clazz);
writeLog(log, msg, null);
}
public static void log(Class> clazz, String msg, Throwable t) {
Logger log = findLogger(clazz);
writeLog(log, msg, t);
}
private static void writeLog(Logger log, String msg, Throwable t) {
if (log.isDebugEnabled()) {
if (null == t) {
log.debug(msg);
} else {
log.debug(msg, t);
}
} else if (log.isInfoEnabled()) {
if (null == t) {
log.info(msg);
} else {
log.info(msg, t);
}
} else if (log.isWarnEnabled()) {
if (null == t) {
log.warn(msg);
} else {
log.warn(msg, t);
}
} else if (log.isErrorEnabled()) {
if (null == t) {
log.error(msg);
} else {
log.error(msg, t);
}
} else if (log.isTraceEnabled()) {
if (null == t) {
log.trace(msg);
} else {
log.trace(msg, t);
}
}
}
private static Logger findLogger(Class> clazz) {
if (logMap.containsKey(clazz)) {
return logMap.get(clazz);
}
final Logger log = LoggerFactory.getLogger(clazz);
logMap.put(clazz, log);
return log;
}
}