This commit is contained in:
Looly 2023-04-24 13:45:28 +08:00
parent 49a05b6908
commit 0d47cf1a89
23 changed files with 421 additions and 403 deletions

View File

@ -0,0 +1,61 @@
/*
* Copyright (c) 2023 looly(loolly@aliyun.com)
* Hutool is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package org.dromara.hutool.log;
import org.dromara.hutool.log.engine.LogEngine;
/**
* 抽象日期引擎<br>
* 提供保存日志框架名称和checkLogExist方法
*
* @author looly
* @since 6.0.0
*/
public abstract class AbsLogEngine implements LogEngine {
/**
* 日志框架名用于打印当前所用日志框架
*/
protected String name;
/**
* 构造
*
* @param name 日志框架名
*/
public AbsLogEngine(final String name) {
this.name = name;
}
/**
* 获取日志框架名用于打印当前所用日志框架
*
* @return 日志框架名
* @since 4.1.21
*/
public String getName() {
return this.name;
}
/**
* 检查日志实现是否存在<br>
* 此方法仅用于检查所提供的日志相关类是否存在当传入的日志类类不存在时抛出ClassNotFoundException<br>
* 此方法的作用是在detectLogFactory方法自动检测所用日志时如果实现类不存在调用此方法会自动抛出异常从而切换到下一种日志的检测
*
* @param logClassName 日志实现相关类
*/
@SuppressWarnings("unused")
protected void checkLogExist(final Class<?> logClassName) {
// 不做任何操作
}
}

View File

@ -1,88 +0,0 @@
/*
* Copyright (c) 2023 looly(loolly@aliyun.com)
* Hutool is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package org.dromara.hutool.log;
import org.dromara.hutool.log.engine.commons.ApacheCommonsLogFactory;
import org.dromara.hutool.log.engine.console.ConsoleLogFactory;
import org.dromara.hutool.log.engine.jdk.JdkLogFactory;
import org.dromara.hutool.log.engine.log4j.Log4jLogFactory;
import org.dromara.hutool.log.engine.log4j2.Log4j2LogFactory;
import org.dromara.hutool.log.engine.slf4j.Slf4jLogFactory;
/**
* 全局日志工厂类<br>
* 用于减少日志工厂创建减少日志库探测
*
* @author looly
* @since 4.0.3
*/
public class GlobalLogFactory {
private static volatile LogFactory currentLogFactory;
private static final Object lock = new Object();
/**
* 获取单例日志工厂类如果不存在创建之
*
* @return 当前使用的日志工厂
*/
public static LogFactory get() {
if (null == currentLogFactory) {
synchronized (lock) {
if (null == currentLogFactory) {
currentLogFactory = LogFactory.of();
}
}
}
return currentLogFactory;
}
/**
* 自定义日志实现
*
* @see Slf4jLogFactory
* @see Log4jLogFactory
* @see Log4j2LogFactory
* @see ApacheCommonsLogFactory
* @see JdkLogFactory
* @see ConsoleLogFactory
*
* @param logFactoryClass 日志工厂类
* @return 自定义的日志工厂类
*/
public static LogFactory set(final Class<? extends LogFactory> logFactoryClass) {
try {
return set(logFactoryClass.newInstance());
} catch (final Exception e) {
throw new IllegalArgumentException("Can not instance LogFactory class!", e);
}
}
/**
* 自定义日志实现
*
* @see Slf4jLogFactory
* @see Log4jLogFactory
* @see Log4j2LogFactory
* @see ApacheCommonsLogFactory
* @see JdkLogFactory
* @see ConsoleLogFactory
*
* @param logFactory 日志工厂类对象
* @return 自定义的日志工厂类
*/
public static LogFactory set(final LogFactory logFactory) {
logFactory.getLog(GlobalLogFactory.class).debug("Custom Use [{}] Logger.", logFactory.name);
currentLogFactory = logFactory;
return currentLogFactory;
}
}

View File

@ -28,7 +28,7 @@ import org.dromara.hutool.log.level.WarnLog;
*/ */
public interface Log extends TraceLog, DebugLog, InfoLog, WarnLog, ErrorLog { public interface Log extends TraceLog, DebugLog, InfoLog, WarnLog, ErrorLog {
//------------------------------------------------------------------------ Static method start // region Static method
/** /**
* 获得Log * 获得Log
* *
@ -36,7 +36,7 @@ public interface Log extends TraceLog, DebugLog, InfoLog, WarnLog, ErrorLog {
* @return Log * @return Log
*/ */
static Log get(final Class<?> clazz) { static Log get(final Class<?> clazz) {
return LogFactory.get(clazz); return LogFactory.getLog(clazz);
} }
/** /**
@ -47,7 +47,7 @@ public interface Log extends TraceLog, DebugLog, InfoLog, WarnLog, ErrorLog {
* @since 5.0.0 * @since 5.0.0
*/ */
static Log get(final String name) { static Log get(final String name) {
return LogFactory.get(name); return LogFactory.getLog(name);
} }
/** /**
@ -55,9 +55,9 @@ public interface Log extends TraceLog, DebugLog, InfoLog, WarnLog, ErrorLog {
* @since 5.0.0 * @since 5.0.0
*/ */
static Log get() { static Log get() {
return LogFactory.get(CallerUtil.getCallerCaller()); return LogFactory.getLog(CallerUtil.getCallerCaller());
} }
//------------------------------------------------------------------------ Static method end // endregion
/** /**
* @return 日志对象的Name * @return 日志对象的Name

View File

@ -12,60 +12,24 @@
package org.dromara.hutool.log; package org.dromara.hutool.log;
import org.dromara.hutool.core.io.resource.ResourceUtil; import org.dromara.hutool.core.lang.Singleton;
import org.dromara.hutool.core.lang.caller.CallerUtil; import org.dromara.hutool.log.engine.LogEngineFactory;
import org.dromara.hutool.core.map.SafeConcurrentHashMap;
import org.dromara.hutool.core.spi.SpiUtil;
import org.dromara.hutool.log.engine.console.ConsoleLogFactory;
import org.dromara.hutool.log.engine.jdk.JdkLogFactory;
import java.net.URL;
import java.util.Map;
/** /**
* 日志工厂类 * 日志简单工厂类提供带有缓存的日志对象创建
* *
* @author Looly * @author Looly
*/ */
public abstract class LogFactory { public abstract class LogFactory {
/**
* 日志框架名用于打印当前所用日志框架
*/
protected String name;
/**
* 日志对象缓存
*/
private final Map<Object, Log> logCache;
/**
* 构造
*
* @param name 日志框架名
*/
public LogFactory(final String name) {
this.name = name;
logCache = new SafeConcurrentHashMap<>();
}
/**
* 获取日志框架名用于打印当前所用日志框架
*
* @return 日志框架名
* @since 4.1.21
*/
public String getName() {
return this.name;
}
/** /**
* 获得日志对象 * 获得日志对象
* *
* @param name 日志对象名 * @param name 日志对象名
* @return 日志对象 * @return 日志对象
*/ */
public Log getLog(final String name) { public static Log getLog(final String name) {
return logCache.computeIfAbsent(name, o -> createLog((String)o)); return Singleton.get(name, () -> LogEngineFactory.getEngine().createLog(name));
} }
/** /**
@ -74,122 +38,7 @@ public abstract class LogFactory {
* @param clazz 日志对应类 * @param clazz 日志对应类
* @return 日志对象 * @return 日志对象
*/ */
public Log getLog(final Class<?> clazz) { public static Log getLog(final Class<?> clazz) {
return logCache.computeIfAbsent(clazz, o -> createLog((Class<?>)o)); return Singleton.get(clazz.getName(), () -> LogEngineFactory.getEngine().createLog(clazz));
} }
/**
* 创建日志对象
*
* @param name 日志对象名
* @return 日志对象
*/
public abstract Log createLog(String name);
/**
* 创建日志对象
*
* @param clazz 日志对应类
* @return 日志对象
*/
public abstract Log createLog(Class<?> clazz);
/**
* 检查日志实现是否存在<br>
* 此方法仅用于检查所提供的日志相关类是否存在当传入的日志类类不存在时抛出ClassNotFoundException<br>
* 此方法的作用是在detectLogFactory方法自动检测所用日志时如果实现类不存在调用此方法会自动抛出异常从而切换到下一种日志的检测
*
* @param logClassName 日志实现相关类
*/
protected void checkLogExist(final Class<?> logClassName) {
// 不做任何操作
}
// ------------------------------------------------------------------------- Static start
/**
* @return 当前使用的日志工厂
*/
public static LogFactory getCurrentLogFactory() {
return GlobalLogFactory.get();
}
/**
* 自定义日志实现
*
* @param logFactoryClass 日志工厂类
* @return 自定义的日志工厂类
*/
public static LogFactory setCurrentLogFactory(final Class<? extends LogFactory> logFactoryClass) {
return GlobalLogFactory.set(logFactoryClass);
}
/**
* 自定义日志实现
*
* @param logFactory 日志工厂类对象
* @return 自定义的日志工厂类
*/
public static LogFactory setCurrentLogFactory(final LogFactory logFactory) {
return GlobalLogFactory.set(logFactory);
}
/**
* 获得日志对象
*
* @param name 日志对象名
* @return 日志对象
*/
public static Log get(final String name) {
return getCurrentLogFactory().getLog(name);
}
/**
* 获得日志对象
*
* @param clazz 日志对应类
* @return 日志对象
*/
public static Log get(final Class<?> clazz) {
return getCurrentLogFactory().getLog(clazz);
}
/**
* @return 获得调用者的日志
*/
public static Log get() {
return get(CallerUtil.getCallerCaller());
}
/**
* 决定日志实现
* <p>
* 依次按照顺序检查日志库的jar是否被引入如果未引入任何日志库则检查ClassPath下的logging.properties存在则使用JdkLogFactory否则使用ConsoleLogFactory
*
* @return 日志实现类
*/
public static LogFactory of() {
final LogFactory factory = doCreate();
factory.getLog(LogFactory.class).debug("Use [{}] Logger As Default.", factory.name);
return factory;
}
/**
* 决定日志实现
* <p>
* 依次按照顺序检查日志库的jar是否被引入如果未引入任何日志库则检查ClassPath下的logging.properties存在则使用JdkLogFactory否则使用ConsoleLogFactory
*
* @return 日志实现类
*/
private static LogFactory doCreate() {
final LogFactory factory = SpiUtil.loadFirstAvailable(LogFactory.class);
if (null != factory) {
return factory;
}
// 未找到任何可支持的日志库时判断依据当JDK Logging的配置文件位于classpath中使用JDK Logging否则使用Console
final URL url = ResourceUtil.getResourceUrl("logging.properties");
return (null != url) ? new JdkLogFactory() : new ConsoleLogFactory();
}
// ------------------------------------------------------------------------- Static end
} }

View File

@ -20,32 +20,29 @@ import org.dromara.hutool.log.level.Level;
* 静态日志类用于在不引入日志对象的情况下打印日志 * 静态日志类用于在不引入日志对象的情况下打印日志
* *
* @author Looly * @author Looly
*
*/ */
public final class StaticLog { public class StaticLog {
private static final String FQCN = StaticLog.class.getName(); private static final String FQCN = StaticLog.class.getName();
private StaticLog() {
}
// ----------------------------------------------------------- Log method start // ----------------------------------------------------------- Log method start
// ------------------------ Trace // ------------------------ Trace
/** /**
* Trace等级日志小于debug<br> * Trace等级日志小于debug<br>
* 由于动态获取Log效率较低建议在非频繁调用的情况下使用 * 由于动态获取Log效率较低建议在非频繁调用的情况下使用
* *
* @param format 格式文本{} 代表变量 * @param format 格式文本{} 代表变量
* @param arguments 变量对应的参数 * @param arguments 变量对应的参数
*/ */
public static void trace(final String format, final Object... arguments) { public static void trace(final String format, final Object... arguments) {
trace(LogFactory.get(CallerUtil.getCallerCaller()), format, arguments); trace(LogFactory.getLog(CallerUtil.getCallerCaller()), format, arguments);
} }
/** /**
* Trace等级日志小于Debug * Trace等级日志小于Debug
* *
* @param log 日志对象 * @param log 日志对象
* @param format 格式文本{} 代表变量 * @param format 格式文本{} 代表变量
* @param arguments 变量对应的参数 * @param arguments 变量对应的参数
*/ */
public static void trace(final Log log, final String format, final Object... arguments) { public static void trace(final Log log, final String format, final Object... arguments) {
@ -53,22 +50,23 @@ public final class StaticLog {
} }
// ------------------------ debug // ------------------------ debug
/** /**
* Debug等级日志小于Info<br> * Debug等级日志小于Info<br>
* 由于动态获取Log效率较低建议在非频繁调用的情况下使用 * 由于动态获取Log效率较低建议在非频繁调用的情况下使用
* *
* @param format 格式文本{} 代表变量 * @param format 格式文本{} 代表变量
* @param arguments 变量对应的参数 * @param arguments 变量对应的参数
*/ */
public static void debug(final String format, final Object... arguments) { public static void debug(final String format, final Object... arguments) {
debug(LogFactory.get(CallerUtil.getCallerCaller()), format, arguments); debug(LogFactory.getLog(CallerUtil.getCallerCaller()), format, arguments);
} }
/** /**
* Debug等级日志小于Info * Debug等级日志小于Info
* *
* @param log 日志对象 * @param log 日志对象
* @param format 格式文本{} 代表变量 * @param format 格式文本{} 代表变量
* @param arguments 变量对应的参数 * @param arguments 变量对应的参数
*/ */
public static void debug(final Log log, final String format, final Object... arguments) { public static void debug(final Log log, final String format, final Object... arguments) {
@ -76,22 +74,23 @@ public final class StaticLog {
} }
// ------------------------ info // ------------------------ info
/** /**
* Info等级日志小于Warn<br> * Info等级日志小于Warn<br>
* 由于动态获取Log效率较低建议在非频繁调用的情况下使用 * 由于动态获取Log效率较低建议在非频繁调用的情况下使用
* *
* @param format 格式文本{} 代表变量 * @param format 格式文本{} 代表变量
* @param arguments 变量对应的参数 * @param arguments 变量对应的参数
*/ */
public static void info(final String format, final Object... arguments) { public static void info(final String format, final Object... arguments) {
info(LogFactory.get(CallerUtil.getCallerCaller()), format, arguments); info(LogFactory.getLog(CallerUtil.getCallerCaller()), format, arguments);
} }
/** /**
* Info等级日志小于Warn * Info等级日志小于Warn
* *
* @param log 日志对象 * @param log 日志对象
* @param format 格式文本{} 代表变量 * @param format 格式文本{} 代表变量
* @param arguments 变量对应的参数 * @param arguments 变量对应的参数
*/ */
public static void info(final Log log, final String format, final Object... arguments) { public static void info(final Log log, final String format, final Object... arguments) {
@ -99,34 +98,35 @@ public final class StaticLog {
} }
// ------------------------ warn // ------------------------ warn
/** /**
* Warn等级日志小于Error<br> * Warn等级日志小于Error<br>
* 由于动态获取Log效率较低建议在非频繁调用的情况下使用 * 由于动态获取Log效率较低建议在非频繁调用的情况下使用
* *
* @param format 格式文本{} 代表变量 * @param format 格式文本{} 代表变量
* @param arguments 变量对应的参数 * @param arguments 变量对应的参数
*/ */
public static void warn(final String format, final Object... arguments) { public static void warn(final String format, final Object... arguments) {
warn(LogFactory.get(CallerUtil.getCallerCaller()), format, arguments); warn(LogFactory.getLog(CallerUtil.getCallerCaller()), format, arguments);
} }
/** /**
* Warn等级日志小于Error<br> * Warn等级日志小于Error<br>
* 由于动态获取Log效率较低建议在非频繁调用的情况下使用 * 由于动态获取Log效率较低建议在非频繁调用的情况下使用
* *
* @param e 需在日志中堆栈打印的异常 * @param e 需在日志中堆栈打印的异常
* @param format 格式文本{} 代表变量 * @param format 格式文本{} 代表变量
* @param arguments 变量对应的参数 * @param arguments 变量对应的参数
*/ */
public static void warn(final Throwable e, final String format, final Object... arguments) { public static void warn(final Throwable e, final String format, final Object... arguments) {
warn(LogFactory.get(CallerUtil.getCallerCaller()), e, StrUtil.format(format, arguments)); warn(LogFactory.getLog(CallerUtil.getCallerCaller()), e, StrUtil.format(format, arguments));
} }
/** /**
* Warn等级日志小于Error * Warn等级日志小于Error
* *
* @param log 日志对象 * @param log 日志对象
* @param format 格式文本{} 代表变量 * @param format 格式文本{} 代表变量
* @param arguments 变量对应的参数 * @param arguments 变量对应的参数
*/ */
public static void warn(final Log log, final String format, final Object... arguments) { public static void warn(final Log log, final String format, final Object... arguments) {
@ -136,9 +136,9 @@ public final class StaticLog {
/** /**
* Warn等级日志小于Error * Warn等级日志小于Error
* *
* @param log 日志对象 * @param log 日志对象
* @param e 需在日志中堆栈打印的异常 * @param e 需在日志中堆栈打印的异常
* @param format 格式文本{} 代表变量 * @param format 格式文本{} 代表变量
* @param arguments 变量对应的参数 * @param arguments 变量对应的参数
*/ */
public static void warn(final Log log, final Throwable e, final String format, final Object... arguments) { public static void warn(final Log log, final Throwable e, final String format, final Object... arguments) {
@ -146,6 +146,7 @@ public final class StaticLog {
} }
// ------------------------ error // ------------------------ error
/** /**
* Error等级日志<br> * Error等级日志<br>
* 由于动态获取Log效率较低建议在非频繁调用的情况下使用 * 由于动态获取Log效率较低建议在非频繁调用的情况下使用
@ -153,37 +154,37 @@ public final class StaticLog {
* @param e 需在日志中堆栈打印的异常 * @param e 需在日志中堆栈打印的异常
*/ */
public static void error(final Throwable e) { public static void error(final Throwable e) {
error(LogFactory.get(CallerUtil.getCallerCaller()), e); error(LogFactory.getLog(CallerUtil.getCallerCaller()), e);
} }
/** /**
* Error等级日志<br> * Error等级日志<br>
* 由于动态获取Log效率较低建议在非频繁调用的情况下使用 * 由于动态获取Log效率较低建议在非频繁调用的情况下使用
* *
* @param format 格式文本{} 代表变量 * @param format 格式文本{} 代表变量
* @param arguments 变量对应的参数 * @param arguments 变量对应的参数
*/ */
public static void error(final String format, final Object... arguments) { public static void error(final String format, final Object... arguments) {
error(LogFactory.get(CallerUtil.getCallerCaller()), format, arguments); error(LogFactory.getLog(CallerUtil.getCallerCaller()), format, arguments);
} }
/** /**
* Error等级日志<br> * Error等级日志<br>
* 由于动态获取Log效率较低建议在非频繁调用的情况下使用 * 由于动态获取Log效率较低建议在非频繁调用的情况下使用
* *
* @param e 需在日志中堆栈打印的异常 * @param e 需在日志中堆栈打印的异常
* @param format 格式文本{} 代表变量 * @param format 格式文本{} 代表变量
* @param arguments 变量对应的参数 * @param arguments 变量对应的参数
*/ */
public static void error(final Throwable e, final String format, final Object... arguments) { public static void error(final Throwable e, final String format, final Object... arguments) {
error(LogFactory.get(CallerUtil.getCallerCaller()), e, format, arguments); error(LogFactory.getLog(CallerUtil.getCallerCaller()), e, format, arguments);
} }
/** /**
* Error等级日志<br> * Error等级日志<br>
* *
* @param log 日志对象 * @param log 日志对象
* @param e 需在日志中堆栈打印的异常 * @param e 需在日志中堆栈打印的异常
*/ */
public static void error(final Log log, final Throwable e) { public static void error(final Log log, final Throwable e) {
error(log, e, e.getMessage()); error(log, e, e.getMessage());
@ -192,8 +193,8 @@ public final class StaticLog {
/** /**
* Error等级日志<br> * Error等级日志<br>
* *
* @param log 日志对象 * @param log 日志对象
* @param format 格式文本{} 代表变量 * @param format 格式文本{} 代表变量
* @param arguments 变量对应的参数 * @param arguments 变量对应的参数
*/ */
public static void error(final Log log, final String format, final Object... arguments) { public static void error(final Log log, final String format, final Object... arguments) {
@ -203,9 +204,9 @@ public final class StaticLog {
/** /**
* Error等级日志<br> * Error等级日志<br>
* *
* @param log 日志对象 * @param log 日志对象
* @param e 需在日志中堆栈打印的异常 * @param e 需在日志中堆栈打印的异常
* @param format 格式文本{} 代表变量 * @param format 格式文本{} 代表变量
* @param arguments 变量对应的参数 * @param arguments 变量对应的参数
*/ */
public static void error(final Log log, final Throwable e, final String format, final Object... arguments) { public static void error(final Log log, final Throwable e, final String format, final Object... arguments) {
@ -213,16 +214,17 @@ public final class StaticLog {
} }
// ------------------------ Log // ------------------------ Log
/** /**
* 打印日志<br> * 打印日志<br>
* *
* @param level 日志级别 * @param level 日志级别
* @param t 需在日志中堆栈打印的异常 * @param t 需在日志中堆栈打印的异常
* @param format 格式文本{} 代表变量 * @param format 格式文本{} 代表变量
* @param arguments 变量对应的参数 * @param arguments 变量对应的参数
*/ */
public static void log(final Level level, final Throwable t, final String format, final Object... arguments) { public static void log(final Level level, final Throwable t, final String format, final Object... arguments) {
LogFactory.get(CallerUtil.getCallerCaller()).log(FQCN, level, t, format, arguments); LogFactory.getLog(CallerUtil.getCallerCaller()).log(FQCN, level, t, format, arguments);
} }
// ----------------------------------------------------------- Log method end // ----------------------------------------------------------- Log method end

View File

@ -0,0 +1,47 @@
/*
* Copyright (c) 2023 looly(loolly@aliyun.com)
* Hutool is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package org.dromara.hutool.log.engine;
import org.dromara.hutool.log.Log;
/**
* 日期引擎接口
*
* @author looly
* @since 6.0.0
*/
public interface LogEngine {
/**
* 获取日志框架名用于打印当前所用日志框架
*
* @return 日志框架名
*/
String getName();
/**
* 创建日志对象
*
* @param name 日志对象名
* @return 日志对象
*/
Log createLog(String name);
/**
* 创建日志对象
*
* @param clazz 日志对应类
* @return 日志对象
*/
Log createLog(Class<?> clazz);
}

View File

@ -0,0 +1,113 @@
/*
* Copyright (c) 2023 looly(loolly@aliyun.com)
* Hutool is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package org.dromara.hutool.log.engine;
import org.dromara.hutool.core.io.resource.ResourceUtil;
import org.dromara.hutool.core.lang.Singleton;
import org.dromara.hutool.core.reflect.ConstructorUtil;
import org.dromara.hutool.core.spi.SpiUtil;
import org.dromara.hutool.log.LogFactory;
import org.dromara.hutool.log.engine.commons.ApacheCommonsLogEngine;
import org.dromara.hutool.log.engine.console.ConsoleLogEngine;
import org.dromara.hutool.log.engine.jdk.JdkLogEngine;
import org.dromara.hutool.log.engine.log4j.Log4jLogEngine;
import org.dromara.hutool.log.engine.log4j2.Log4j2LogEngine;
import org.dromara.hutool.log.engine.slf4j.Slf4jLogEngine;
import java.net.URL;
/**
* 日志引擎简单工厂静态工厂
*
* @author looly
* @since 6.0.0
*/
public class LogEngineFactory {
/**
* 根据用户引入的模板引擎jar自动创建对应的模板引擎对象<br>
* 获得的是单例的TemplateEngine
*
* @return 单例的TemplateEngine
*/
public static LogEngine getEngine() {
return Singleton.get(LogEngineFactory.class.getName(), LogEngineFactory::createEngine);
}
/**
* 自定义默认日志实现
*
* @param logEngineClass 日志工厂类
* @see Slf4jLogEngine
* @see Log4jLogEngine
* @see Log4j2LogEngine
* @see ApacheCommonsLogEngine
* @see JdkLogEngine
* @see ConsoleLogEngine
*/
public static void setDefaultEngine(final Class<? extends LogEngine> logEngineClass) {
try {
setDefaultEngine(ConstructorUtil.newInstance(logEngineClass));
} catch (final Exception e) {
throw new IllegalArgumentException("Can not instance LogFactory class!", e);
}
}
/**
* 自定义日志实现
*
* @param logEngine 日志引擎对象
* @see Slf4jLogEngine
* @see Log4jLogEngine
* @see Log4j2LogEngine
* @see ApacheCommonsLogEngine
* @see JdkLogEngine
* @see ConsoleLogEngine
*/
public static void setDefaultEngine(final LogEngine logEngine) {
Singleton.put(LogEngineFactory.class.getName(), logEngine);
logEngine.createLog(LogEngineFactory.class).debug("Custom Use [{}] Logger.", logEngine.getName());
}
/**
* 决定日志实现
* <p>
* 依次按照顺序检查日志库的jar是否被引入如果未引入任何日志库则检查ClassPath下的logging.properties<br>
* 存在则使用JdkLogFactory否则使用ConsoleLogFactory
*
* @return 日志实现类
*/
public static LogEngine createEngine() {
final LogEngine engine = doCreateEngine();
engine.createLog(LogFactory.class).debug("Use [{}] Logger As Default.", engine.getName());
return engine;
}
/**
* 决定日志实现
* <p>
* 依次按照顺序检查日志库的jar是否被引入如果未引入任何日志库则检查ClassPath下的logging.properties存在则使用JdkLogFactory否则使用ConsoleLogFactory
*
* @return 日志实现类
*/
private static LogEngine doCreateEngine() {
final LogEngine engine = SpiUtil.loadFirstAvailable(LogEngine.class);
if (null != engine) {
return engine;
}
// 未找到任何可支持的日志库时判断依据当JDK Logging的配置文件位于classpath中使用JDK Logging否则使用Console
final URL url = ResourceUtil.getResourceUrl("logging.properties");
return (null != url) ? new JdkLogEngine() : new ConsoleLogEngine();
}
}

View File

@ -12,20 +12,20 @@
package org.dromara.hutool.log.engine.commons; package org.dromara.hutool.log.engine.commons;
import org.dromara.hutool.log.AbsLogEngine;
import org.dromara.hutool.log.Log; import org.dromara.hutool.log.Log;
import org.dromara.hutool.log.LogFactory;
/** /**
* Apache Commons Logging * Apache Commons Logging
* @author Looly * @author Looly
* *
*/ */
public class ApacheCommonsLogFactory extends LogFactory{ public class ApacheCommonsLogEngine extends AbsLogEngine {
/** /**
* 构造 * 构造
*/ */
public ApacheCommonsLogFactory() { public ApacheCommonsLogEngine() {
super("Apache Common Logging"); super("Apache Common Logging");
checkLogExist(org.apache.commons.logging.LogFactory.class); checkLogExist(org.apache.commons.logging.LogFactory.class);
} }
@ -52,6 +52,6 @@ public class ApacheCommonsLogFactory extends LogFactory{
protected void checkLogExist(final Class<?> logClassName) { protected void checkLogExist(final Class<?> logClassName) {
super.checkLogExist(logClassName); super.checkLogExist(logClassName);
//Commons Logging在调用getLog时才检查是否有日志实现在此提前检查如果没有实现则跳过之 //Commons Logging在调用getLog时才检查是否有日志实现在此提前检查如果没有实现则跳过之
getLog(ApacheCommonsLogFactory.class); createLog(ApacheCommonsLogEngine.class);
} }
} }

View File

@ -12,8 +12,8 @@
package org.dromara.hutool.log.engine.console; package org.dromara.hutool.log.engine.console;
import org.dromara.hutool.log.AbsLogEngine;
import org.dromara.hutool.log.Log; import org.dromara.hutool.log.Log;
import org.dromara.hutool.log.LogFactory;
/** /**
* 利用System.out.println()打印彩色日志 * 利用System.out.println()打印彩色日志
@ -21,9 +21,12 @@ import org.dromara.hutool.log.LogFactory;
* @author hongda.li * @author hongda.li
* @since 5.8.0 * @since 5.8.0
*/ */
public class ConsoleColorLogFactory extends LogFactory { public class ConsoleColorLogEngine extends AbsLogEngine {
public ConsoleColorLogFactory() { /**
* 构造
*/
public ConsoleColorLogEngine() {
super("Hutool Console Color Logging"); super("Hutool Console Color Logging");
} }

View File

@ -12,17 +12,20 @@
package org.dromara.hutool.log.engine.console; package org.dromara.hutool.log.engine.console;
import org.dromara.hutool.log.AbsLogEngine;
import org.dromara.hutool.log.Log; import org.dromara.hutool.log.Log;
import org.dromara.hutool.log.LogFactory;
/** /**
* 利用System.out.println()打印日志 * 利用System.out.println()打印日志
* @author Looly * @author Looly
* *
*/ */
public class ConsoleLogFactory extends LogFactory { public class ConsoleLogEngine extends AbsLogEngine {
public ConsoleLogFactory() { /**
* 构造
*/
public ConsoleLogEngine() {
super("Hutool Console Logging"); super("Hutool Console Logging");
} }

View File

@ -12,8 +12,8 @@
package org.dromara.hutool.log.engine.jboss; package org.dromara.hutool.log.engine.jboss;
import org.dromara.hutool.log.AbsLogEngine;
import org.dromara.hutool.log.Log; import org.dromara.hutool.log.Log;
import org.dromara.hutool.log.LogFactory;
/** /**
* <a href="https://github.com/jboss-logging">Jboss-Logging</a> log. * <a href="https://github.com/jboss-logging">Jboss-Logging</a> log.
@ -21,12 +21,12 @@ import org.dromara.hutool.log.LogFactory;
* @author Looly * @author Looly
* @since 4.1.21 * @since 4.1.21
*/ */
public class JbossLogFactory extends LogFactory { public class JbossLogEngine extends AbsLogEngine {
/** /**
* 构造 * 构造
*/ */
public JbossLogFactory() { public JbossLogEngine() {
super("JBoss Logging"); super("JBoss Logging");
checkLogExist(org.jboss.logging.Logger.class); checkLogExist(org.jboss.logging.Logger.class);
} }

View File

@ -18,18 +18,21 @@ import java.util.logging.LogManager;
import org.dromara.hutool.core.io.IoUtil; import org.dromara.hutool.core.io.IoUtil;
import org.dromara.hutool.core.io.resource.ResourceUtil; import org.dromara.hutool.core.io.resource.ResourceUtil;
import org.dromara.hutool.core.lang.Console; import org.dromara.hutool.core.lang.Console;
import org.dromara.hutool.log.AbsLogEngine;
import org.dromara.hutool.log.Log; import org.dromara.hutool.log.Log;
import org.dromara.hutool.log.LogFactory;
/** /**
* JDK日志工厂类 * JDK日志工厂类
* <a href="http://java.sun.com/javase/6/docs/technotes/guides/logging/index.html">java.util.logging</a> log. * <a href="http://java.sun.com/javase/6/docs/technotes/guides/logging/index.html">java.util.logging</a> log.
* @author Looly
* *
* @author Looly
*/ */
public class JdkLogFactory extends LogFactory{ public class JdkLogEngine extends AbsLogEngine {
public JdkLogFactory() { /**
* 构造
*/
public JdkLogEngine() {
super("JDK Logging"); super("JDK Logging");
readConfig(); readConfig();
} }
@ -50,7 +53,7 @@ public class JdkLogFactory extends LogFactory{
private void readConfig() { private void readConfig() {
//避免循环引用Log初始化的时候不使用相关工具类 //避免循环引用Log初始化的时候不使用相关工具类
final InputStream in = ResourceUtil.getStreamSafe("logging.properties"); final InputStream in = ResourceUtil.getStreamSafe("logging.properties");
if(null == in){ if (null == in) {
System.err.println("[WARN] Can not find [logging.properties], use [%JRE_HOME%/lib/logging.properties] as default!"); System.err.println("[WARN] Can not find [logging.properties], use [%JRE_HOME%/lib/logging.properties] as default!");
return; return;
} }

View File

@ -12,17 +12,21 @@
package org.dromara.hutool.log.engine.log4j; package org.dromara.hutool.log.engine.log4j;
import org.dromara.hutool.log.AbsLogEngine;
import org.dromara.hutool.log.Log; import org.dromara.hutool.log.Log;
import org.dromara.hutool.log.LogFactory; import org.dromara.hutool.log.LogFactory;
/** /**
* <a href="http://logging.apache.org/log4j/1.2/index.html">Apache Log4J</a> log.<br> * <a href="http://logging.apache.org/log4j/1.2/index.html">Apache Log4J</a> log.<br>
* @author Looly
* *
* @author Looly
*/ */
public class Log4jLogFactory extends LogFactory{ public class Log4jLogEngine extends AbsLogEngine {
public Log4jLogFactory() { /**
* 构造
*/
public Log4jLogEngine() {
super("Log4j"); super("Log4j");
checkLogExist(org.apache.log4j.Logger.class); checkLogExist(org.apache.log4j.Logger.class);
} }

View File

@ -12,17 +12,20 @@
package org.dromara.hutool.log.engine.log4j2; package org.dromara.hutool.log.engine.log4j2;
import org.dromara.hutool.log.AbsLogEngine;
import org.dromara.hutool.log.Log; import org.dromara.hutool.log.Log;
import org.dromara.hutool.log.LogFactory;
/** /**
* <a href="http://logging.apache.org/log4j/2.x/index.html">Apache Log4J 2</a> log.<br> * <a href="http://logging.apache.org/log4j/2.x/index.html">Apache Log4J 2</a> log.<br>
* @author Looly * @author Looly
* *
*/ */
public class Log4j2LogFactory extends LogFactory{ public class Log4j2LogEngine extends AbsLogEngine {
public Log4j2LogFactory() { /**
* 构造
*/
public Log4j2LogEngine() {
super("Log4j2"); super("Log4j2");
checkLogExist(org.apache.logging.log4j.LogManager.class); checkLogExist(org.apache.logging.log4j.LogManager.class);
} }

View File

@ -12,18 +12,20 @@
package org.dromara.hutool.log.engine.logtube; package org.dromara.hutool.log.engine.logtube;
import org.dromara.hutool.log.AbsLogEngine;
import org.dromara.hutool.log.Log; import org.dromara.hutool.log.Log;
import org.dromara.hutool.log.LogFactory;
/** /**
* <a href="https://github.com/logtube/logtube-java">LogTube</a> log. 封装<br> * <a href="https://github.com/logtube/logtube-java">LogTube</a> log. 封装<br>
* *
* @author Looly * @author Looly
* @since 5.6.6
*/ */
public class LogTubeLogFactory extends LogFactory { public class LogTubeLogEngine extends AbsLogEngine {
public LogTubeLogFactory() { /**
* 构造
*/
public LogTubeLogEngine() {
super("LogTube"); super("LogTube");
checkLogExist(io.github.logtube.Logtube.class); checkLogExist(io.github.logtube.Logtube.class);
} }

View File

@ -16,22 +16,24 @@ import java.io.OutputStream;
import java.io.PrintStream; import java.io.PrintStream;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import org.dromara.hutool.log.AbsLogEngine;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.slf4j.helpers.NOPLoggerFactory; import org.slf4j.helpers.NOPLoggerFactory;
import org.dromara.hutool.log.Log; import org.dromara.hutool.log.Log;
import org.dromara.hutool.log.LogFactory;
/** /**
* <a href="http://www.slf4j.org/">SLF4J</a> log.<br> * <a href="http://www.slf4j.org/">SLF4J</a> log.<br>
* 同样无缝支持 <a href="http://logback.qos.ch/">LogBack</a> * 同样无缝支持 <a href="http://logback.qos.ch/">LogBack</a>
* *
* @author Looly * @author Looly
*
*/ */
public class Slf4jLogFactory extends LogFactory { public class Slf4jLogEngine extends AbsLogEngine {
public Slf4jLogFactory() { /**
* 构造
*/
public Slf4jLogEngine() {
this(true); this(true);
} }
@ -40,10 +42,10 @@ public class Slf4jLogFactory extends LogFactory {
* *
* @param failIfNOP 如果未找到桥接包是否报错 * @param failIfNOP 如果未找到桥接包是否报错
*/ */
public Slf4jLogFactory(final boolean failIfNOP) { public Slf4jLogEngine(final boolean failIfNOP) {
super("Slf4j"); super("Slf4j");
checkLogExist(LoggerFactory.class); checkLogExist(LoggerFactory.class);
if(!failIfNOP){ if (!failIfNOP) {
return; return;
} }
@ -52,7 +54,7 @@ public class Slf4jLogFactory extends LogFactory {
final StringBuilder buf = new StringBuilder(); final StringBuilder buf = new StringBuilder();
final PrintStream err = System.err; final PrintStream err = System.err;
try { try {
System.setErr(new PrintStream(new OutputStream(){ System.setErr(new PrintStream(new OutputStream() {
@Override @Override
public void write(final int b) { public void write(final int b) {
buf.append((char) b); buf.append((char) b);

View File

@ -12,8 +12,8 @@
package org.dromara.hutool.log.engine.tinylog; package org.dromara.hutool.log.engine.tinylog;
import org.dromara.hutool.log.AbsLogEngine;
import org.dromara.hutool.log.Log; import org.dromara.hutool.log.Log;
import org.dromara.hutool.log.LogFactory;
/** /**
* <a href="http://www.tinylog.org/">TinyLog2</a> log.<br> * <a href="http://www.tinylog.org/">TinyLog2</a> log.<br>
@ -21,12 +21,12 @@ import org.dromara.hutool.log.LogFactory;
* @author Looly * @author Looly
* *
*/ */
public class TinyLog2Factory extends LogFactory { public class TinyLog2Engine extends AbsLogEngine {
/** /**
* 构造 * 构造
*/ */
public TinyLog2Factory() { public TinyLog2Engine() {
super("TinyLog"); super("TinyLog");
checkLogExist(org.tinylog.Logger.class); checkLogExist(org.tinylog.Logger.class);
} }

View File

@ -12,8 +12,8 @@
package org.dromara.hutool.log.engine.tinylog; package org.dromara.hutool.log.engine.tinylog;
import org.dromara.hutool.log.AbsLogEngine;
import org.dromara.hutool.log.Log; import org.dromara.hutool.log.Log;
import org.dromara.hutool.log.LogFactory;
/** /**
* <a href="http://www.tinylog.org/">TinyLog</a> log.<br> * <a href="http://www.tinylog.org/">TinyLog</a> log.<br>
@ -21,12 +21,12 @@ import org.dromara.hutool.log.LogFactory;
* @author Looly * @author Looly
* *
*/ */
public class TinyLogFactory extends LogFactory { public class TinyLogEngine extends AbsLogEngine {
/** /**
* 构造 * 构造
*/ */
public TinyLogFactory() { public TinyLogEngine() {
super("TinyLog"); super("TinyLog");
checkLogExist(org.pmw.tinylog.Logger.class); checkLogExist(org.pmw.tinylog.Logger.class);
} }

View File

@ -10,11 +10,11 @@
# See the Mulan PSL v2 for more details. # See the Mulan PSL v2 for more details.
# #
org.dromara.hutool.log.engine.logtube.LogTubeLogFactory org.dromara.hutool.log.engine.logtube.LogTubeLogEngine
org.dromara.hutool.log.engine.slf4j.Slf4jLogFactory org.dromara.hutool.log.engine.slf4j.Slf4jLogEngine
org.dromara.hutool.log.engine.log4j2.Log4j2LogFactory org.dromara.hutool.log.engine.log4j2.Log4j2LogEngine
org.dromara.hutool.log.engine.log4j.Log4jLogFactory org.dromara.hutool.log.engine.log4j.Log4jLogEngine
org.dromara.hutool.log.engine.commons.ApacheCommonsLogFactory org.dromara.hutool.log.engine.commons.ApacheCommonsLogEngine
org.dromara.hutool.log.engine.tinylog.TinyLog2Factory org.dromara.hutool.log.engine.tinylog.TinyLog2Engine
org.dromara.hutool.log.engine.tinylog.TinyLogFactory org.dromara.hutool.log.engine.tinylog.TinyLogEngine
org.dromara.hutool.log.engine.jboss.JbossLogFactory org.dromara.hutool.log.engine.jboss.JbossLogEngine

View File

@ -1,14 +1,16 @@
package org.dromara.hutool.log; package org.dromara.hutool.log;
import org.dromara.hutool.log.engine.commons.ApacheCommonsLogFactory; import org.dromara.hutool.log.engine.LogEngine;
import org.dromara.hutool.log.engine.console.ConsoleLogFactory; import org.dromara.hutool.log.engine.LogEngineFactory;
import org.dromara.hutool.log.engine.jboss.JbossLogFactory; import org.dromara.hutool.log.engine.commons.ApacheCommonsLogEngine;
import org.dromara.hutool.log.engine.jdk.JdkLogFactory; import org.dromara.hutool.log.engine.console.ConsoleLogEngine;
import org.dromara.hutool.log.engine.log4j.Log4jLogFactory; import org.dromara.hutool.log.engine.jboss.JbossLogEngine;
import org.dromara.hutool.log.engine.log4j2.Log4j2LogFactory; import org.dromara.hutool.log.engine.jdk.JdkLogEngine;
import org.dromara.hutool.log.engine.slf4j.Slf4jLogFactory; import org.dromara.hutool.log.engine.log4j.Log4jLogEngine;
import org.dromara.hutool.log.engine.tinylog.TinyLog2Factory; import org.dromara.hutool.log.engine.log4j2.Log4j2LogEngine;
import org.dromara.hutool.log.engine.tinylog.TinyLogFactory; import org.dromara.hutool.log.engine.slf4j.Slf4jLogEngine;
import org.dromara.hutool.log.engine.tinylog.TinyLog2Engine;
import org.dromara.hutool.log.engine.tinylog.TinyLogEngine;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
/** /**
@ -22,18 +24,18 @@ public class CustomLogTest {
@Test @Test
public void consoleLogTest(){ public void consoleLogTest(){
final LogFactory factory = new ConsoleLogFactory(); final LogEngine engine = new ConsoleLogEngine();
LogFactory.setCurrentLogFactory(factory); LogEngineFactory.setDefaultEngine(engine);
final Log log = LogFactory.get(); final Log log = Log.get();
log.info("This is custom '{}' log\n{}", factory.getName(), LINE); log.info("This is custom '{}' log\n{}", engine.getName(), LINE);
} }
@Test @Test
public void consoleLogNullTest(){ public void consoleLogNullTest(){
final LogFactory factory = new ConsoleLogFactory(); final LogEngine engine = new ConsoleLogEngine();
LogFactory.setCurrentLogFactory(factory); LogEngineFactory.setDefaultEngine(engine);
final Log log = LogFactory.get(); final Log log = Log.get();
log.info(null); log.info(null);
log.info((String)null); log.info((String)null);
@ -41,92 +43,92 @@ public class CustomLogTest {
@Test @Test
public void commonsLogTest(){ public void commonsLogTest(){
final LogFactory factory = new ApacheCommonsLogFactory(); final LogEngine engine = new ApacheCommonsLogEngine();
LogFactory.setCurrentLogFactory(factory); LogEngineFactory.setDefaultEngine(engine);
final Log log = LogFactory.get(); final Log log = Log.get();
log.info(null); log.info(null);
log.info((String)null); log.info((String)null);
log.info("This is custom '{}' log\n{}", factory.getName(), LINE); log.info("This is custom '{}' log\n{}", engine.getName(), LINE);
} }
@Test @Test
public void tinyLogTest(){ public void tinyLogTest(){
final LogFactory factory = new TinyLogFactory(); final LogEngine engine = new TinyLogEngine();
LogFactory.setCurrentLogFactory(factory); LogEngineFactory.setDefaultEngine(engine);
final Log log = LogFactory.get(); final Log log = Log.get();
log.info(null); log.info(null);
log.info((String)null); log.info((String)null);
log.info("This is custom '{}' log\n{}", factory.getName(), LINE); log.info("This is custom '{}' log\n{}", engine.getName(), LINE);
} }
@Test @Test
public void tinyLog2Test(){ public void tinyLog2Test(){
final LogFactory factory = new TinyLog2Factory(); final LogEngine engine = new TinyLog2Engine();
LogFactory.setCurrentLogFactory(factory); LogEngineFactory.setDefaultEngine(engine);
final Log log = LogFactory.get(); final Log log = Log.get();
log.info(null); log.info(null);
log.info((String)null); log.info((String)null);
log.info("This is custom '{}' log\n{}", factory.getName(), LINE); log.info("This is custom '{}' log\n{}", engine.getName(), LINE);
} }
@Test @Test
public void log4j2LogTest(){ public void log4j2LogTest(){
final LogFactory factory = new Log4j2LogFactory(); final LogEngine engine = new Log4j2LogEngine();
LogFactory.setCurrentLogFactory(factory); LogEngineFactory.setDefaultEngine(engine);
final Log log = LogFactory.get(); final Log log = Log.get();
log.debug(null); log.debug(null);
log.debug("This is custom '{}' log\n{}", factory.getName(), LINE); log.debug("This is custom '{}' log\n{}", engine.getName(), LINE);
log.info(null); log.info(null);
log.info((String)null); log.info((String)null);
log.info("This is custom '{}' log\n{}", factory.getName(), LINE); log.info("This is custom '{}' log\n{}", engine.getName(), LINE);
} }
@Test @Test
public void log4jLogTest(){ public void log4jLogTest(){
final LogFactory factory = new Log4jLogFactory(); final LogEngine engine = new Log4jLogEngine();
LogFactory.setCurrentLogFactory(factory); LogEngineFactory.setDefaultEngine(engine);
final Log log = LogFactory.get(); final Log log = Log.get();
log.info(null); log.info(null);
log.info((String)null); log.info((String)null);
log.info("This is custom '{}' log\n{}", factory.getName(), LINE); log.info("This is custom '{}' log\n{}", engine.getName(), LINE);
} }
@Test @Test
public void jbossLogTest(){ public void jbossLogTest(){
final LogFactory factory = new JbossLogFactory(); final LogEngine engine = new JbossLogEngine();
LogFactory.setCurrentLogFactory(factory); LogEngineFactory.setDefaultEngine(engine);
final Log log = LogFactory.get(); final Log log = Log.get();
log.info(null); log.info(null);
log.info((String)null); log.info((String)null);
log.info("This is custom '{}' log\n{}", factory.getName(), LINE); log.info("This is custom '{}' log\n{}", engine.getName(), LINE);
} }
@Test @Test
public void jdkLogTest(){ public void jdkLogTest(){
final LogFactory factory = new JdkLogFactory(); final LogEngine engine = new JdkLogEngine();
LogFactory.setCurrentLogFactory(factory); LogEngineFactory.setDefaultEngine(engine);
final Log log = LogFactory.get(); final Log log = Log.get();
log.info(null); log.info(null);
log.info((String)null); log.info((String)null);
log.info("This is custom '{}' log\n{}", factory.getName(), LINE); log.info("This is custom '{}' log\n{}", engine.getName(), LINE);
} }
@Test @Test
public void slf4jTest(){ public void slf4jTest(){
final LogFactory factory = new Slf4jLogFactory(false); final LogEngine engine = new Slf4jLogEngine(false);
LogFactory.setCurrentLogFactory(factory); LogEngineFactory.setDefaultEngine(engine);
final Log log = LogFactory.get(); final Log log = Log.get();
log.info(null); log.info(null);
log.info((String)null); log.info((String)null);
log.info("This is custom '{}' log\n{}", factory.getName(), LINE); log.info("This is custom '{}' log\n{}", engine.getName(), LINE);
} }
} }

View File

@ -13,7 +13,7 @@ public class LogTest {
@Test @Test
public void logTest(){ public void logTest(){
final Log log = LogFactory.get(); final Log log = Log.get();
// 自动选择日志实现 // 自动选择日志实现
log.debug("This is {} log", Level.DEBUG); log.debug("This is {} log", Level.DEBUG);
@ -27,7 +27,7 @@ public class LogTest {
@Test @Test
@Disabled @Disabled
public void logWithExceptionTest() { public void logWithExceptionTest() {
final Log log = LogFactory.get(); final Log log = Log.get();
final Exception e = new Exception("test Exception"); final Exception e = new Exception("test Exception");
log.error("我是错误消息", e); log.error("我是错误消息", e);
} }

View File

@ -1,15 +1,17 @@
package org.dromara.hutool.log; package org.dromara.hutool.log;
import org.dromara.hutool.log.engine.logtube.LogTubeLogFactory; import org.dromara.hutool.log.engine.LogEngine;
import org.dromara.hutool.log.engine.LogEngineFactory;
import org.dromara.hutool.log.engine.logtube.LogTubeLogEngine;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
public class LogTubeTest { public class LogTubeTest {
@Test @Test
public void logTest(){ public void logTest(){
final LogFactory factory = new LogTubeLogFactory(); final LogEngine engine = new LogTubeLogEngine();
LogFactory.setCurrentLogFactory(factory); LogEngineFactory.setDefaultEngine(engine);
final Log log = LogFactory.get(); final Log log = Log.get();
log.debug("LogTube debug test."); log.debug("LogTube debug test.");
} }
} }

View File

@ -1,20 +1,30 @@
package org.dromara.hutool.log; package org.dromara.hutool.log;
import org.dromara.hutool.log.engine.console.ConsoleColorLogFactory; import org.dromara.hutool.log.engine.LogEngineFactory;
import org.dromara.hutool.log.engine.console.ConsoleLogFactory; import org.dromara.hutool.log.engine.console.ConsoleColorLogEngine;
import org.dromara.hutool.log.engine.console.ConsoleLogEngine;
import org.dromara.hutool.log.engine.log4j2.Log4j2LogEngine;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
public class StaticLogTest { public class StaticLogTest {
@Test
public void staticLog4j2Test() {
LogEngineFactory.setDefaultEngine(Log4j2LogEngine.class);
StaticLog.debug("This is static {} log", "debug");
StaticLog.info("This is static {} log", "info");
}
@Test @Test
public void test() { public void test() {
LogFactory.setCurrentLogFactory(ConsoleLogFactory.class); LogEngineFactory.setDefaultEngine(ConsoleLogEngine.class);
StaticLog.debug("This is static {} log", "debug"); StaticLog.debug("This is static {} log", "debug");
StaticLog.info("This is static {} log", "info"); StaticLog.info("This is static {} log", "info");
} }
@Test @Test
public void colorTest(){ public void colorTest(){
LogFactory.setCurrentLogFactory(ConsoleColorLogFactory.class); LogEngineFactory.setDefaultEngine(ConsoleColorLogEngine.class);
StaticLog.debug("This is static {} log", "debug"); StaticLog.debug("This is static {} log", "debug");
StaticLog.info("This is static {} log", "info"); StaticLog.info("This is static {} log", "info");
StaticLog.error("This is static {} log", "error"); StaticLog.error("This is static {} log", "error");