support spi

This commit is contained in:
Looly 2020-03-02 16:56:41 +08:00
parent c5e4d0ef34
commit d6f8fb0077
4 changed files with 19 additions and 31 deletions

View File

@ -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

View File

@ -20,7 +20,7 @@ import cn.hutool.log.LogFactory;
public class Slf4jLogFactory extends LogFactory {
public Slf4jLogFactory() {
this(false);
this(true);
}
/**

View File

@ -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

View File

@ -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();