mirror of
https://gitee.com/dromara/hutool.git
synced 2025-04-30 04:45:55 +08:00
fix code
This commit is contained in:
parent
49a05b6908
commit
0d47cf1a89
61
hutool-log/src/main/java/org/dromara/hutool/log/AbsLogEngine.java
Executable file
61
hutool-log/src/main/java/org/dromara/hutool/log/AbsLogEngine.java
Executable 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) {
|
||||
// 不做任何操作
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
@ -28,7 +28,7 @@ import org.dromara.hutool.log.level.WarnLog;
|
||||
*/
|
||||
public interface Log extends TraceLog, DebugLog, InfoLog, WarnLog, ErrorLog {
|
||||
|
||||
//------------------------------------------------------------------------ Static method start
|
||||
// region Static method
|
||||
/**
|
||||
* 获得Log
|
||||
*
|
||||
@ -36,7 +36,7 @@ public interface Log extends TraceLog, DebugLog, InfoLog, WarnLog, ErrorLog {
|
||||
* @return Log
|
||||
*/
|
||||
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
|
||||
*/
|
||||
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
|
||||
*/
|
||||
static Log get() {
|
||||
return LogFactory.get(CallerUtil.getCallerCaller());
|
||||
return LogFactory.getLog(CallerUtil.getCallerCaller());
|
||||
}
|
||||
//------------------------------------------------------------------------ Static method end
|
||||
// endregion
|
||||
|
||||
/**
|
||||
* @return 日志对象的Name
|
||||
|
@ -12,60 +12,24 @@
|
||||
|
||||
package org.dromara.hutool.log;
|
||||
|
||||
import org.dromara.hutool.core.io.resource.ResourceUtil;
|
||||
import org.dromara.hutool.core.lang.caller.CallerUtil;
|
||||
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;
|
||||
import org.dromara.hutool.core.lang.Singleton;
|
||||
import org.dromara.hutool.log.engine.LogEngineFactory;
|
||||
|
||||
/**
|
||||
* 日志工厂类
|
||||
* 日志简单工厂类,提供带有缓存的日志对象创建
|
||||
*
|
||||
* @author Looly
|
||||
*/
|
||||
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 日志对象名
|
||||
* @return 日志对象
|
||||
*/
|
||||
public Log getLog(final String name) {
|
||||
return logCache.computeIfAbsent(name, o -> createLog((String)o));
|
||||
public static Log getLog(final String name) {
|
||||
return Singleton.get(name, () -> LogEngineFactory.getEngine().createLog(name));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -74,122 +38,7 @@ public abstract class LogFactory {
|
||||
* @param clazz 日志对应类
|
||||
* @return 日志对象
|
||||
*/
|
||||
public Log getLog(final Class<?> clazz) {
|
||||
return logCache.computeIfAbsent(clazz, o -> createLog((Class<?>)o));
|
||||
public static Log getLog(final Class<?> clazz) {
|
||||
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
|
||||
}
|
||||
|
@ -20,32 +20,29 @@ import org.dromara.hutool.log.level.Level;
|
||||
* 静态日志类,用于在不引入日志对象的情况下打印日志
|
||||
*
|
||||
* @author Looly
|
||||
*
|
||||
*/
|
||||
public final class StaticLog {
|
||||
public class StaticLog {
|
||||
private static final String FQCN = StaticLog.class.getName();
|
||||
|
||||
private StaticLog() {
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------- Log method start
|
||||
// ------------------------ Trace
|
||||
|
||||
/**
|
||||
* Trace等级日志,小于debug<br>
|
||||
* 由于动态获取Log,效率较低,建议在非频繁调用的情况下使用!!
|
||||
*
|
||||
* @param format 格式文本,{} 代表变量
|
||||
* @param format 格式文本,{} 代表变量
|
||||
* @param 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
|
||||
*
|
||||
* @param log 日志对象
|
||||
* @param format 格式文本,{} 代表变量
|
||||
* @param log 日志对象
|
||||
* @param format 格式文本,{} 代表变量
|
||||
* @param arguments 变量对应的参数
|
||||
*/
|
||||
public static void trace(final Log log, final String format, final Object... arguments) {
|
||||
@ -53,22 +50,23 @@ public final class StaticLog {
|
||||
}
|
||||
|
||||
// ------------------------ debug
|
||||
|
||||
/**
|
||||
* Debug等级日志,小于Info<br>
|
||||
* 由于动态获取Log,效率较低,建议在非频繁调用的情况下使用!!
|
||||
*
|
||||
* @param format 格式文本,{} 代表变量
|
||||
* @param format 格式文本,{} 代表变量
|
||||
* @param 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
|
||||
*
|
||||
* @param log 日志对象
|
||||
* @param format 格式文本,{} 代表变量
|
||||
* @param log 日志对象
|
||||
* @param format 格式文本,{} 代表变量
|
||||
* @param arguments 变量对应的参数
|
||||
*/
|
||||
public static void debug(final Log log, final String format, final Object... arguments) {
|
||||
@ -76,22 +74,23 @@ public final class StaticLog {
|
||||
}
|
||||
|
||||
// ------------------------ info
|
||||
|
||||
/**
|
||||
* Info等级日志,小于Warn<br>
|
||||
* 由于动态获取Log,效率较低,建议在非频繁调用的情况下使用!!
|
||||
*
|
||||
* @param format 格式文本,{} 代表变量
|
||||
* @param format 格式文本,{} 代表变量
|
||||
* @param 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
|
||||
*
|
||||
* @param log 日志对象
|
||||
* @param format 格式文本,{} 代表变量
|
||||
* @param log 日志对象
|
||||
* @param format 格式文本,{} 代表变量
|
||||
* @param arguments 变量对应的参数
|
||||
*/
|
||||
public static void info(final Log log, final String format, final Object... arguments) {
|
||||
@ -99,34 +98,35 @@ public final class StaticLog {
|
||||
}
|
||||
|
||||
// ------------------------ warn
|
||||
|
||||
/**
|
||||
* Warn等级日志,小于Error<br>
|
||||
* 由于动态获取Log,效率较低,建议在非频繁调用的情况下使用!!
|
||||
*
|
||||
* @param format 格式文本,{} 代表变量
|
||||
* @param format 格式文本,{} 代表变量
|
||||
* @param 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>
|
||||
* 由于动态获取Log,效率较低,建议在非频繁调用的情况下使用!!
|
||||
*
|
||||
* @param e 需在日志中堆栈打印的异常
|
||||
* @param format 格式文本,{} 代表变量
|
||||
* @param e 需在日志中堆栈打印的异常
|
||||
* @param format 格式文本,{} 代表变量
|
||||
* @param 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
|
||||
*
|
||||
* @param log 日志对象
|
||||
* @param format 格式文本,{} 代表变量
|
||||
* @param log 日志对象
|
||||
* @param format 格式文本,{} 代表变量
|
||||
* @param arguments 变量对应的参数
|
||||
*/
|
||||
public static void warn(final Log log, final String format, final Object... arguments) {
|
||||
@ -136,9 +136,9 @@ public final class StaticLog {
|
||||
/**
|
||||
* Warn等级日志,小于Error
|
||||
*
|
||||
* @param log 日志对象
|
||||
* @param e 需在日志中堆栈打印的异常
|
||||
* @param format 格式文本,{} 代表变量
|
||||
* @param log 日志对象
|
||||
* @param e 需在日志中堆栈打印的异常
|
||||
* @param format 格式文本,{} 代表变量
|
||||
* @param 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等级日志<br>
|
||||
* 由于动态获取Log,效率较低,建议在非频繁调用的情况下使用!!
|
||||
@ -153,37 +154,37 @@ public final class StaticLog {
|
||||
* @param e 需在日志中堆栈打印的异常
|
||||
*/
|
||||
public static void error(final Throwable e) {
|
||||
error(LogFactory.get(CallerUtil.getCallerCaller()), e);
|
||||
error(LogFactory.getLog(CallerUtil.getCallerCaller()), e);
|
||||
}
|
||||
|
||||
/**
|
||||
* Error等级日志<br>
|
||||
* 由于动态获取Log,效率较低,建议在非频繁调用的情况下使用!!
|
||||
*
|
||||
* @param format 格式文本,{} 代表变量
|
||||
* @param format 格式文本,{} 代表变量
|
||||
* @param 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>
|
||||
* 由于动态获取Log,效率较低,建议在非频繁调用的情况下使用!!
|
||||
*
|
||||
* @param e 需在日志中堆栈打印的异常
|
||||
* @param format 格式文本,{} 代表变量
|
||||
* @param e 需在日志中堆栈打印的异常
|
||||
* @param format 格式文本,{} 代表变量
|
||||
* @param 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>
|
||||
*
|
||||
* @param log 日志对象
|
||||
* @param e 需在日志中堆栈打印的异常
|
||||
* @param e 需在日志中堆栈打印的异常
|
||||
*/
|
||||
public static void error(final Log log, final Throwable e) {
|
||||
error(log, e, e.getMessage());
|
||||
@ -192,8 +193,8 @@ public final class StaticLog {
|
||||
/**
|
||||
* Error等级日志<br>
|
||||
*
|
||||
* @param log 日志对象
|
||||
* @param format 格式文本,{} 代表变量
|
||||
* @param log 日志对象
|
||||
* @param format 格式文本,{} 代表变量
|
||||
* @param arguments 变量对应的参数
|
||||
*/
|
||||
public static void error(final Log log, final String format, final Object... arguments) {
|
||||
@ -203,9 +204,9 @@ public final class StaticLog {
|
||||
/**
|
||||
* Error等级日志<br>
|
||||
*
|
||||
* @param log 日志对象
|
||||
* @param e 需在日志中堆栈打印的异常
|
||||
* @param format 格式文本,{} 代表变量
|
||||
* @param log 日志对象
|
||||
* @param e 需在日志中堆栈打印的异常
|
||||
* @param format 格式文本,{} 代表变量
|
||||
* @param 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
|
||||
|
||||
/**
|
||||
* 打印日志<br>
|
||||
*
|
||||
* @param level 日志级别
|
||||
* @param t 需在日志中堆栈打印的异常
|
||||
* @param format 格式文本,{} 代表变量
|
||||
* @param level 日志级别
|
||||
* @param t 需在日志中堆栈打印的异常
|
||||
* @param format 格式文本,{} 代表变量
|
||||
* @param 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
|
||||
|
47
hutool-log/src/main/java/org/dromara/hutool/log/engine/LogEngine.java
Executable file
47
hutool-log/src/main/java/org/dromara/hutool/log/engine/LogEngine.java
Executable 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);
|
||||
}
|
113
hutool-log/src/main/java/org/dromara/hutool/log/engine/LogEngineFactory.java
Executable file
113
hutool-log/src/main/java/org/dromara/hutool/log/engine/LogEngineFactory.java
Executable 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();
|
||||
}
|
||||
}
|
@ -12,20 +12,20 @@
|
||||
|
||||
package org.dromara.hutool.log.engine.commons;
|
||||
|
||||
import org.dromara.hutool.log.AbsLogEngine;
|
||||
import org.dromara.hutool.log.Log;
|
||||
import org.dromara.hutool.log.LogFactory;
|
||||
|
||||
/**
|
||||
* Apache Commons Logging
|
||||
* @author Looly
|
||||
*
|
||||
*/
|
||||
public class ApacheCommonsLogFactory extends LogFactory{
|
||||
public class ApacheCommonsLogEngine extends AbsLogEngine {
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*/
|
||||
public ApacheCommonsLogFactory() {
|
||||
public ApacheCommonsLogEngine() {
|
||||
super("Apache Common Logging");
|
||||
checkLogExist(org.apache.commons.logging.LogFactory.class);
|
||||
}
|
||||
@ -52,6 +52,6 @@ public class ApacheCommonsLogFactory extends LogFactory{
|
||||
protected void checkLogExist(final Class<?> logClassName) {
|
||||
super.checkLogExist(logClassName);
|
||||
//Commons Logging在调用getLog时才检查是否有日志实现,在此提前检查,如果没有实现则跳过之
|
||||
getLog(ApacheCommonsLogFactory.class);
|
||||
createLog(ApacheCommonsLogEngine.class);
|
||||
}
|
||||
}
|
@ -12,8 +12,8 @@
|
||||
|
||||
package org.dromara.hutool.log.engine.console;
|
||||
|
||||
import org.dromara.hutool.log.AbsLogEngine;
|
||||
import org.dromara.hutool.log.Log;
|
||||
import org.dromara.hutool.log.LogFactory;
|
||||
|
||||
/**
|
||||
* 利用System.out.println()打印彩色日志
|
||||
@ -21,9 +21,12 @@ import org.dromara.hutool.log.LogFactory;
|
||||
* @author hongda.li
|
||||
* @since 5.8.0
|
||||
*/
|
||||
public class ConsoleColorLogFactory extends LogFactory {
|
||||
public class ConsoleColorLogEngine extends AbsLogEngine {
|
||||
|
||||
public ConsoleColorLogFactory() {
|
||||
/**
|
||||
* 构造
|
||||
*/
|
||||
public ConsoleColorLogEngine() {
|
||||
super("Hutool Console Color Logging");
|
||||
}
|
||||
|
@ -12,17 +12,20 @@
|
||||
|
||||
package org.dromara.hutool.log.engine.console;
|
||||
|
||||
import org.dromara.hutool.log.AbsLogEngine;
|
||||
import org.dromara.hutool.log.Log;
|
||||
import org.dromara.hutool.log.LogFactory;
|
||||
|
||||
/**
|
||||
* 利用System.out.println()打印日志
|
||||
* @author Looly
|
||||
*
|
||||
*/
|
||||
public class ConsoleLogFactory extends LogFactory {
|
||||
public class ConsoleLogEngine extends AbsLogEngine {
|
||||
|
||||
public ConsoleLogFactory() {
|
||||
/**
|
||||
* 构造
|
||||
*/
|
||||
public ConsoleLogEngine() {
|
||||
super("Hutool Console Logging");
|
||||
}
|
||||
|
@ -12,8 +12,8 @@
|
||||
|
||||
package org.dromara.hutool.log.engine.jboss;
|
||||
|
||||
import org.dromara.hutool.log.AbsLogEngine;
|
||||
import org.dromara.hutool.log.Log;
|
||||
import org.dromara.hutool.log.LogFactory;
|
||||
|
||||
/**
|
||||
* <a href="https://github.com/jboss-logging">Jboss-Logging</a> log.
|
||||
@ -21,12 +21,12 @@ import org.dromara.hutool.log.LogFactory;
|
||||
* @author Looly
|
||||
* @since 4.1.21
|
||||
*/
|
||||
public class JbossLogFactory extends LogFactory {
|
||||
public class JbossLogEngine extends AbsLogEngine {
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*/
|
||||
public JbossLogFactory() {
|
||||
public JbossLogEngine() {
|
||||
super("JBoss Logging");
|
||||
checkLogExist(org.jboss.logging.Logger.class);
|
||||
}
|
@ -18,18 +18,21 @@ import java.util.logging.LogManager;
|
||||
import org.dromara.hutool.core.io.IoUtil;
|
||||
import org.dromara.hutool.core.io.resource.ResourceUtil;
|
||||
import org.dromara.hutool.core.lang.Console;
|
||||
import org.dromara.hutool.log.AbsLogEngine;
|
||||
import org.dromara.hutool.log.Log;
|
||||
import org.dromara.hutool.log.LogFactory;
|
||||
|
||||
/**
|
||||
* JDK日志工厂类
|
||||
* <a href="http://java.sun.com/javase/6/docs/technotes/guides/logging/index.html">java.util.logging</a> log.
|
||||
* @author Looly
|
||||
* <a href="http://java.sun.com/javase/6/docs/technotes/guides/logging/index.html">java.util.logging</a> log.
|
||||
*
|
||||
* @author Looly
|
||||
*/
|
||||
public class JdkLogFactory extends LogFactory{
|
||||
public class JdkLogEngine extends AbsLogEngine {
|
||||
|
||||
public JdkLogFactory() {
|
||||
/**
|
||||
* 构造
|
||||
*/
|
||||
public JdkLogEngine() {
|
||||
super("JDK Logging");
|
||||
readConfig();
|
||||
}
|
||||
@ -50,7 +53,7 @@ public class JdkLogFactory extends LogFactory{
|
||||
private void readConfig() {
|
||||
//避免循环引用,Log初始化的时候不使用相关工具类
|
||||
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!");
|
||||
return;
|
||||
}
|
@ -12,17 +12,21 @@
|
||||
|
||||
package org.dromara.hutool.log.engine.log4j;
|
||||
|
||||
import org.dromara.hutool.log.AbsLogEngine;
|
||||
import org.dromara.hutool.log.Log;
|
||||
import org.dromara.hutool.log.LogFactory;
|
||||
|
||||
/**
|
||||
* <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");
|
||||
checkLogExist(org.apache.log4j.Logger.class);
|
||||
}
|
@ -12,17 +12,20 @@
|
||||
|
||||
package org.dromara.hutool.log.engine.log4j2;
|
||||
|
||||
import org.dromara.hutool.log.AbsLogEngine;
|
||||
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>
|
||||
* @author Looly
|
||||
*
|
||||
*/
|
||||
public class Log4j2LogFactory extends LogFactory{
|
||||
public class Log4j2LogEngine extends AbsLogEngine {
|
||||
|
||||
public Log4j2LogFactory() {
|
||||
/**
|
||||
* 构造
|
||||
*/
|
||||
public Log4j2LogEngine() {
|
||||
super("Log4j2");
|
||||
checkLogExist(org.apache.logging.log4j.LogManager.class);
|
||||
}
|
@ -12,18 +12,20 @@
|
||||
|
||||
package org.dromara.hutool.log.engine.logtube;
|
||||
|
||||
import org.dromara.hutool.log.AbsLogEngine;
|
||||
import org.dromara.hutool.log.Log;
|
||||
import org.dromara.hutool.log.LogFactory;
|
||||
|
||||
/**
|
||||
* <a href="https://github.com/logtube/logtube-java">LogTube</a> log. 封装<br>
|
||||
*
|
||||
* @author Looly
|
||||
* @since 5.6.6
|
||||
*/
|
||||
public class LogTubeLogFactory extends LogFactory {
|
||||
public class LogTubeLogEngine extends AbsLogEngine {
|
||||
|
||||
public LogTubeLogFactory() {
|
||||
/**
|
||||
* 构造
|
||||
*/
|
||||
public LogTubeLogEngine() {
|
||||
super("LogTube");
|
||||
checkLogExist(io.github.logtube.Logtube.class);
|
||||
}
|
@ -16,22 +16,24 @@ import java.io.OutputStream;
|
||||
import java.io.PrintStream;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
|
||||
import org.dromara.hutool.log.AbsLogEngine;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.slf4j.helpers.NOPLoggerFactory;
|
||||
|
||||
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://logback.qos.ch/">LogBack</a>
|
||||
*
|
||||
* @author Looly
|
||||
*
|
||||
*/
|
||||
public class Slf4jLogFactory extends LogFactory {
|
||||
public class Slf4jLogEngine extends AbsLogEngine {
|
||||
|
||||
public Slf4jLogFactory() {
|
||||
/**
|
||||
* 构造
|
||||
*/
|
||||
public Slf4jLogEngine() {
|
||||
this(true);
|
||||
}
|
||||
|
||||
@ -40,10 +42,10 @@ public class Slf4jLogFactory extends LogFactory {
|
||||
*
|
||||
* @param failIfNOP 如果未找到桥接包是否报错
|
||||
*/
|
||||
public Slf4jLogFactory(final boolean failIfNOP) {
|
||||
public Slf4jLogEngine(final boolean failIfNOP) {
|
||||
super("Slf4j");
|
||||
checkLogExist(LoggerFactory.class);
|
||||
if(!failIfNOP){
|
||||
if (!failIfNOP) {
|
||||
return;
|
||||
}
|
||||
|
||||
@ -52,7 +54,7 @@ public class Slf4jLogFactory extends LogFactory {
|
||||
final StringBuilder buf = new StringBuilder();
|
||||
final PrintStream err = System.err;
|
||||
try {
|
||||
System.setErr(new PrintStream(new OutputStream(){
|
||||
System.setErr(new PrintStream(new OutputStream() {
|
||||
@Override
|
||||
public void write(final int b) {
|
||||
buf.append((char) b);
|
@ -12,8 +12,8 @@
|
||||
|
||||
package org.dromara.hutool.log.engine.tinylog;
|
||||
|
||||
import org.dromara.hutool.log.AbsLogEngine;
|
||||
import org.dromara.hutool.log.Log;
|
||||
import org.dromara.hutool.log.LogFactory;
|
||||
|
||||
/**
|
||||
* <a href="http://www.tinylog.org/">TinyLog2</a> log.<br>
|
||||
@ -21,12 +21,12 @@ import org.dromara.hutool.log.LogFactory;
|
||||
* @author Looly
|
||||
*
|
||||
*/
|
||||
public class TinyLog2Factory extends LogFactory {
|
||||
public class TinyLog2Engine extends AbsLogEngine {
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*/
|
||||
public TinyLog2Factory() {
|
||||
public TinyLog2Engine() {
|
||||
super("TinyLog");
|
||||
checkLogExist(org.tinylog.Logger.class);
|
||||
}
|
@ -12,8 +12,8 @@
|
||||
|
||||
package org.dromara.hutool.log.engine.tinylog;
|
||||
|
||||
import org.dromara.hutool.log.AbsLogEngine;
|
||||
import org.dromara.hutool.log.Log;
|
||||
import org.dromara.hutool.log.LogFactory;
|
||||
|
||||
/**
|
||||
* <a href="http://www.tinylog.org/">TinyLog</a> log.<br>
|
||||
@ -21,12 +21,12 @@ import org.dromara.hutool.log.LogFactory;
|
||||
* @author Looly
|
||||
*
|
||||
*/
|
||||
public class TinyLogFactory extends LogFactory {
|
||||
public class TinyLogEngine extends AbsLogEngine {
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*/
|
||||
public TinyLogFactory() {
|
||||
public TinyLogEngine() {
|
||||
super("TinyLog");
|
||||
checkLogExist(org.pmw.tinylog.Logger.class);
|
||||
}
|
@ -10,11 +10,11 @@
|
||||
# See the Mulan PSL v2 for more details.
|
||||
#
|
||||
|
||||
org.dromara.hutool.log.engine.logtube.LogTubeLogFactory
|
||||
org.dromara.hutool.log.engine.slf4j.Slf4jLogFactory
|
||||
org.dromara.hutool.log.engine.log4j2.Log4j2LogFactory
|
||||
org.dromara.hutool.log.engine.log4j.Log4jLogFactory
|
||||
org.dromara.hutool.log.engine.commons.ApacheCommonsLogFactory
|
||||
org.dromara.hutool.log.engine.tinylog.TinyLog2Factory
|
||||
org.dromara.hutool.log.engine.tinylog.TinyLogFactory
|
||||
org.dromara.hutool.log.engine.jboss.JbossLogFactory
|
||||
org.dromara.hutool.log.engine.logtube.LogTubeLogEngine
|
||||
org.dromara.hutool.log.engine.slf4j.Slf4jLogEngine
|
||||
org.dromara.hutool.log.engine.log4j2.Log4j2LogEngine
|
||||
org.dromara.hutool.log.engine.log4j.Log4jLogEngine
|
||||
org.dromara.hutool.log.engine.commons.ApacheCommonsLogEngine
|
||||
org.dromara.hutool.log.engine.tinylog.TinyLog2Engine
|
||||
org.dromara.hutool.log.engine.tinylog.TinyLogEngine
|
||||
org.dromara.hutool.log.engine.jboss.JbossLogEngine
|
@ -1,14 +1,16 @@
|
||||
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.jboss.JbossLogFactory;
|
||||
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;
|
||||
import org.dromara.hutool.log.engine.tinylog.TinyLog2Factory;
|
||||
import org.dromara.hutool.log.engine.tinylog.TinyLogFactory;
|
||||
import org.dromara.hutool.log.engine.LogEngine;
|
||||
import org.dromara.hutool.log.engine.LogEngineFactory;
|
||||
import org.dromara.hutool.log.engine.commons.ApacheCommonsLogEngine;
|
||||
import org.dromara.hutool.log.engine.console.ConsoleLogEngine;
|
||||
import org.dromara.hutool.log.engine.jboss.JbossLogEngine;
|
||||
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 org.dromara.hutool.log.engine.tinylog.TinyLog2Engine;
|
||||
import org.dromara.hutool.log.engine.tinylog.TinyLogEngine;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
@ -22,18 +24,18 @@ public class CustomLogTest {
|
||||
|
||||
@Test
|
||||
public void consoleLogTest(){
|
||||
final LogFactory factory = new ConsoleLogFactory();
|
||||
LogFactory.setCurrentLogFactory(factory);
|
||||
final Log log = LogFactory.get();
|
||||
final LogEngine engine = new ConsoleLogEngine();
|
||||
LogEngineFactory.setDefaultEngine(engine);
|
||||
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
|
||||
public void consoleLogNullTest(){
|
||||
final LogFactory factory = new ConsoleLogFactory();
|
||||
LogFactory.setCurrentLogFactory(factory);
|
||||
final Log log = LogFactory.get();
|
||||
final LogEngine engine = new ConsoleLogEngine();
|
||||
LogEngineFactory.setDefaultEngine(engine);
|
||||
final Log log = Log.get();
|
||||
|
||||
log.info(null);
|
||||
log.info((String)null);
|
||||
@ -41,92 +43,92 @@ public class CustomLogTest {
|
||||
|
||||
@Test
|
||||
public void commonsLogTest(){
|
||||
final LogFactory factory = new ApacheCommonsLogFactory();
|
||||
LogFactory.setCurrentLogFactory(factory);
|
||||
final Log log = LogFactory.get();
|
||||
final LogEngine engine = new ApacheCommonsLogEngine();
|
||||
LogEngineFactory.setDefaultEngine(engine);
|
||||
final Log log = Log.get();
|
||||
|
||||
log.info(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
|
||||
public void tinyLogTest(){
|
||||
final LogFactory factory = new TinyLogFactory();
|
||||
LogFactory.setCurrentLogFactory(factory);
|
||||
final Log log = LogFactory.get();
|
||||
final LogEngine engine = new TinyLogEngine();
|
||||
LogEngineFactory.setDefaultEngine(engine);
|
||||
final Log log = Log.get();
|
||||
|
||||
log.info(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
|
||||
public void tinyLog2Test(){
|
||||
final LogFactory factory = new TinyLog2Factory();
|
||||
LogFactory.setCurrentLogFactory(factory);
|
||||
final Log log = LogFactory.get();
|
||||
final LogEngine engine = new TinyLog2Engine();
|
||||
LogEngineFactory.setDefaultEngine(engine);
|
||||
final Log log = Log.get();
|
||||
|
||||
log.info(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
|
||||
public void log4j2LogTest(){
|
||||
final LogFactory factory = new Log4j2LogFactory();
|
||||
LogFactory.setCurrentLogFactory(factory);
|
||||
final Log log = LogFactory.get();
|
||||
final LogEngine engine = new Log4j2LogEngine();
|
||||
LogEngineFactory.setDefaultEngine(engine);
|
||||
final Log log = Log.get();
|
||||
|
||||
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((String)null);
|
||||
log.info("This is custom '{}' log\n{}", factory.getName(), LINE);
|
||||
log.info("This is custom '{}' log\n{}", engine.getName(), LINE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void log4jLogTest(){
|
||||
final LogFactory factory = new Log4jLogFactory();
|
||||
LogFactory.setCurrentLogFactory(factory);
|
||||
final Log log = LogFactory.get();
|
||||
final LogEngine engine = new Log4jLogEngine();
|
||||
LogEngineFactory.setDefaultEngine(engine);
|
||||
final Log log = Log.get();
|
||||
|
||||
log.info(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
|
||||
public void jbossLogTest(){
|
||||
final LogFactory factory = new JbossLogFactory();
|
||||
LogFactory.setCurrentLogFactory(factory);
|
||||
final Log log = LogFactory.get();
|
||||
final LogEngine engine = new JbossLogEngine();
|
||||
LogEngineFactory.setDefaultEngine(engine);
|
||||
final Log log = Log.get();
|
||||
|
||||
log.info(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
|
||||
public void jdkLogTest(){
|
||||
final LogFactory factory = new JdkLogFactory();
|
||||
LogFactory.setCurrentLogFactory(factory);
|
||||
final Log log = LogFactory.get();
|
||||
final LogEngine engine = new JdkLogEngine();
|
||||
LogEngineFactory.setDefaultEngine(engine);
|
||||
final Log log = Log.get();
|
||||
|
||||
log.info(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
|
||||
public void slf4jTest(){
|
||||
final LogFactory factory = new Slf4jLogFactory(false);
|
||||
LogFactory.setCurrentLogFactory(factory);
|
||||
final Log log = LogFactory.get();
|
||||
final LogEngine engine = new Slf4jLogEngine(false);
|
||||
LogEngineFactory.setDefaultEngine(engine);
|
||||
final Log log = Log.get();
|
||||
|
||||
log.info(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);
|
||||
}
|
||||
}
|
||||
|
@ -13,7 +13,7 @@ public class LogTest {
|
||||
|
||||
@Test
|
||||
public void logTest(){
|
||||
final Log log = LogFactory.get();
|
||||
final Log log = Log.get();
|
||||
|
||||
// 自动选择日志实现
|
||||
log.debug("This is {} log", Level.DEBUG);
|
||||
@ -27,7 +27,7 @@ public class LogTest {
|
||||
@Test
|
||||
@Disabled
|
||||
public void logWithExceptionTest() {
|
||||
final Log log = LogFactory.get();
|
||||
final Log log = Log.get();
|
||||
final Exception e = new Exception("test Exception");
|
||||
log.error("我是错误消息", e);
|
||||
}
|
||||
|
@ -1,15 +1,17 @@
|
||||
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;
|
||||
|
||||
public class LogTubeTest {
|
||||
|
||||
@Test
|
||||
public void logTest(){
|
||||
final LogFactory factory = new LogTubeLogFactory();
|
||||
LogFactory.setCurrentLogFactory(factory);
|
||||
final Log log = LogFactory.get();
|
||||
final LogEngine engine = new LogTubeLogEngine();
|
||||
LogEngineFactory.setDefaultEngine(engine);
|
||||
final Log log = Log.get();
|
||||
log.debug("LogTube debug test.");
|
||||
}
|
||||
}
|
||||
|
@ -1,20 +1,30 @@
|
||||
package org.dromara.hutool.log;
|
||||
|
||||
import org.dromara.hutool.log.engine.console.ConsoleColorLogFactory;
|
||||
import org.dromara.hutool.log.engine.console.ConsoleLogFactory;
|
||||
import org.dromara.hutool.log.engine.LogEngineFactory;
|
||||
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;
|
||||
|
||||
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
|
||||
public void test() {
|
||||
LogFactory.setCurrentLogFactory(ConsoleLogFactory.class);
|
||||
LogEngineFactory.setDefaultEngine(ConsoleLogEngine.class);
|
||||
StaticLog.debug("This is static {} log", "debug");
|
||||
StaticLog.info("This is static {} log", "info");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void colorTest(){
|
||||
LogFactory.setCurrentLogFactory(ConsoleColorLogFactory.class);
|
||||
LogEngineFactory.setDefaultEngine(ConsoleColorLogEngine.class);
|
||||
StaticLog.debug("This is static {} log", "debug");
|
||||
StaticLog.info("This is static {} log", "info");
|
||||
StaticLog.error("This is static {} log", "error");
|
||||
|
Loading…
Reference in New Issue
Block a user