JFinal log 目前的设计是为了让自身对第三方无依赖,在无 log4j.jar 的情况下会自动切换到 JDK logger,
slf4j 好像只是一个门面实现,对 JFinal 来说有点厚重了,JFinal 将来考虑集成最优秀的日志系统,开放配置接口,这样就可以无限支持新的 logger 了。
不能打出类的全名可能是 JDK logger 的问题, JFinal 仅仅是转调了一下 JDK logger,目前建议使用 log4j。
JFinal log 默认使用log4j,在log4j不支持情况下,自动切换的JDK logger,
在Log源码能看到;
/**
* Log源码下5个日志级别;
* The five logging levels used by Log are (in order):
* 1. DEBUG (the least serious)
* 2. INFO
* 3. WARN
* 4. ERROR
* 5. FATAL (the most serious)
*/
public abstract class Log {
private static ILogFactory defaultLogFactory = null;
static {
init();
}
// 默认使用log4j,在log4j不支持情况下,自动切换的JDK logger
static void init() {
if (defaultLogFactory == null) {
try {
Class.forName("org.apache.log4j.Logger");
Class> log4jLogFactoryClass = Class.forName("com.jfinal.log.Log4jLogFactory");
defaultLogFactory = (ILogFactory)log4jLogFactoryClass.newInstance(); // return new Log4jLogFactory();
} catch (Exception e) {
defaultLogFactory = new JdkLogFactory();
}
}
}
static void setDefaultLogFactory(ILogFactory defaultLogFactory) {
if (defaultLogFactory == null) {
throw new IllegalArgumentException("defaultLogFactory can not be null.");
}
Log.defaultLogFactory = defaultLogFactory;
}
public static Log getLog(Class> clazz) {
return defaultLogFactory.getLog(clazz);
}
public static Log getLog(String name) {
return defaultLogFactory.getLog(name);
}
.....................省略抽象方法..........................
}
在JFinal2.2中引入slf4j;
以后总结下这种框架设计
Jfinal2.2将 Logger 改名成 Log;
引入相应jar,或者配置pod依赖;
2、实现slf4j插件,具体可以仿照框架中JdkLog、Log4j的实现;
3、在configConstant中配置setLogFactory;
public class Slf4jLogFactory implements ILogFactory{
public Log getLog(Class> clazz) {
return new Slf4jLog(clazz);
}
public Log getLog(String name) {
return new Slf4jLog(name);
}
}
public class Slf4jLog extends Log {
private org.slf4j.Logger log;
Slf4jLog(Class> clazz) {
log = LoggerFactory.getLogger(clazz);
}
Slf4jLog(String name) {
log = LoggerFactory.getLogger(name);
}
// info、debug、warn、error、fatal;注意参数顺序
@Override
public void info(String message) {
log.info(message);
}
@Override
public void info(String message, Throwable t) {
log.info(message, t);
}
@Override
public void debug(String message) {
log.debug(message);
}
@Override
public void debug(String message, Throwable t) {
log.debug(message, t);
}
@Override
public void warn(String message) {
log.warn(message);
}
@Override
public void warn(String message, Throwable t) {
log.warn(message, t);
}
@Override
public void error(String message) {
log.error(message);
}
@Override
public void error(String message, Throwable t) {
log.error(message, t);
}
@Override
public void fatal(String message) {
log.error(message);
}
@Override
public void fatal(String message, Throwable t) {
log.error(message, t);
}
@Override
public boolean isInfoEnabled() {
return log.isInfoEnabled();
}
@Override
public boolean isDebugEnabled() {
return log.isDebugEnabled();
}
@Override
public boolean isWarnEnabled() {
return log.isWarnEnabled();
}
@Override
public boolean isErrorEnabled() {
return log.isErrorEnabled();
}
@Override
public boolean isFatalEnabled() {
return log.isErrorEnabled();
}
}