mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
support spi
This commit is contained in:
parent
c5e4d0ef34
commit
d6f8fb0077
@ -1,11 +1,14 @@
|
||||
package cn.hutool.log;
|
||||
|
||||
import java.net.URL;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.ServiceLoader;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import cn.hutool.core.io.resource.ResourceUtil;
|
||||
import cn.hutool.core.lang.caller.CallerUtil;
|
||||
import cn.hutool.core.util.ServiceLoaderUtil;
|
||||
import cn.hutool.log.dialect.commons.ApacheCommonsLogFactory;
|
||||
import cn.hutool.log.dialect.console.ConsoleLogFactory;
|
||||
import cn.hutool.log.dialect.jboss.JbossLogFactory;
|
||||
@ -224,35 +227,14 @@ public abstract class LogFactory {
|
||||
* @return 日志实现类
|
||||
*/
|
||||
private static LogFactory doCreate() {
|
||||
try {
|
||||
return new Slf4jLogFactory(true);
|
||||
} catch (NoClassDefFoundError e) {
|
||||
// ignore
|
||||
}
|
||||
try {
|
||||
return new Log4j2LogFactory();
|
||||
} catch (NoClassDefFoundError e) {
|
||||
// ignore
|
||||
}
|
||||
try {
|
||||
return new Log4jLogFactory();
|
||||
} catch (NoClassDefFoundError e) {
|
||||
// ignore
|
||||
}
|
||||
try {
|
||||
return new ApacheCommonsLogFactory();
|
||||
} catch (NoClassDefFoundError e) {
|
||||
// ignore
|
||||
}
|
||||
try {
|
||||
return new TinyLogFactory();
|
||||
} catch (NoClassDefFoundError e) {
|
||||
// ignore
|
||||
}
|
||||
try {
|
||||
return new JbossLogFactory();
|
||||
} catch (NoClassDefFoundError e) {
|
||||
// ignore
|
||||
final ServiceLoader<LogFactory> factories = ServiceLoaderUtil.load(LogFactory.class);
|
||||
final Iterator<LogFactory> factoryIterator = factories.iterator();
|
||||
while(factoryIterator.hasNext()){
|
||||
try{
|
||||
return factoryIterator.next();
|
||||
} catch (NoClassDefFoundError e){
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
|
||||
// 未找到任何可支持的日志库时判断依据:当JDK Logging的配置文件位于classpath中,使用JDK Logging,否则使用Console
|
||||
|
@ -20,7 +20,7 @@ import cn.hutool.log.LogFactory;
|
||||
public class Slf4jLogFactory extends LogFactory {
|
||||
|
||||
public Slf4jLogFactory() {
|
||||
this(false);
|
||||
this(true);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -0,0 +1,6 @@
|
||||
cn.hutool.log.dialect.slf4j.Slf4jLogFactory
|
||||
cn.hutool.log.dialect.log4j2.Log4j2LogFactory
|
||||
cn.hutool.log.dialect.log4j.Log4jLogFactory
|
||||
cn.hutool.log.dialect.commons.ApacheCommonsLogFactory
|
||||
cn.hutool.log.dialect.tinylog.TinyLogFactory
|
||||
cn.hutool.log.dialect.jboss.JbossLogFactory
|
@ -97,7 +97,7 @@ public class CustomLogTest {
|
||||
|
||||
@Test
|
||||
public void slf4jTest(){
|
||||
LogFactory factory = new Slf4jLogFactory();
|
||||
LogFactory factory = new Slf4jLogFactory(false);
|
||||
LogFactory.setCurrentLogFactory(factory);
|
||||
Log log = LogFactory.get();
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user