cn.hutool.log.dialect.slf4j.Slf4jLogFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hutool-all Show documentation
Show all versions of hutool-all Show documentation
Hutool是一个小而全的Java工具类库,通过静态方法封装,降低相关API的学习成本,提高工作效率,使Java拥有函数式语言般的优雅,让Java语言也可以“甜甜的”。
package cn.hutool.log.dialect.slf4j;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import org.slf4j.LoggerFactory;
import org.slf4j.helpers.NOPLoggerFactory;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
/**
* SLF4J log.
* 同样无缝支持 LogBack
*
* @author Looly
*
*/
public class Slf4jLogFactory extends LogFactory {
public Slf4jLogFactory() {
this(true);
}
/**
* 构造
*
* @param failIfNOP 如果未找到桥接包是否报错
*/
public Slf4jLogFactory(boolean failIfNOP) {
super("Slf4j");
checkLogExist(LoggerFactory.class);
if(false == failIfNOP){
return;
}
// SFL4J writes it error messages to System.err. Capture them so that the user does not see such a message on
// the console during automatic detection.
final StringBuilder buf = new StringBuilder();
final PrintStream err = System.err;
try {
System.setErr(new PrintStream(new OutputStream(){
@Override
public void write(int b) {
buf.append((char) b);
}
}, true, "US-ASCII"));
} catch (UnsupportedEncodingException e) {
throw new Error(e);
}
try {
if (LoggerFactory.getILoggerFactory() instanceof NOPLoggerFactory) {
throw new NoClassDefFoundError(buf.toString());
} else {
err.print(buf);
err.flush();
}
} finally {
System.setErr(err);
}
}
@Override
public Log createLog(String name) {
return new Slf4jLog(name);
}
@Override
public Log createLog(Class> clazz) {
return new Slf4jLog(clazz);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy