com.feilong.tools.slf4j.Slf4jUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of feilong Show documentation
Show all versions of feilong Show documentation
feilong is a suite of core and expanded libraries that include utility classes, http, excel,cvs, io classes, and much much more.
/*
* Copyright (C) 2008 feilong
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.feilong.tools.slf4j;
import static com.feilong.core.bean.ConvertUtil.toList;
import static com.feilong.core.util.CollectionsUtil.containsTrimAndIgnoreCase;
import com.feilong.core.lang.StringUtil;
/**
* 对 slf4j 的封装提取.
*
* @author feilong
* @since 1.4.0
*/
public final class Slf4jUtil{
/** Don't let anyone instantiate this class. */
private Slf4jUtil(){
//AssertionError不是必须的. 但它可以避免不小心在类的内部调用构造器. 保证该类在任何情况下都不会被实例化.
//see 《Effective Java》 2nd
throw new AssertionError("No " + getClass().getName() + " instances for you!");
}
//---------------------------------------------------------------
/**
* 格式化字符串,此方法是抽取slf4j的核心方法.
*
*
* 在java中,常会拼接字符串生成新的字符串值,在字符串拼接过程中容易写错或者位置写错
*
* slf4j的log支持格式化输出log,比如:
*
*
*
* - LOGGER.debug("{}","feilong");
* - LOGGER.info("{},{}","feilong","hello");
*
*
* 这些写法非常简洁且有效,不易出错
*
*
* 因此,你可以在代码中出现这样的写法:
*
*
* throw new IllegalArgumentException(Slf4jUtil.format(
* "callbackUrl:[{}] ,length:[{}] can't {@code >}{}",
* callbackUrl,
* callbackUrlLength,
* callbackUrlMaxLength)
*
*
* 又或者
*
*
* return Slf4jUtil.format("{} [{}]", encode, encode.length());
*
*
* @param messagePattern
* message的格式,比如 callbackUrl:[{}] ,length:[{}]
* @param args
* 参数
* @return 如果 messagePattern
是null,返回 null
* 如果 args
是null,返回 messagePattern
* @see org.slf4j.helpers.FormattingTuple
* @see org.slf4j.helpers.MessageFormatter#arrayFormat(String, Object[])
* @see org.slf4j.helpers.FormattingTuple#getMessage()
* @since 1.6.0
* @deprecated since 3.3.8 pls use {@link StringUtil#formatPattern(String, Object...)} see
* https://github.com/ifeilong/feilong/issues/479
*/
@Deprecated
public static String format(String messagePattern,Object...args){
return StringUtil.formatPattern(messagePattern, args);
}
//---------------------------------------------------------------
/**
* 判断指定的logvel 是否小于等于debug级别.
*
*
* 使用场景: 可以使用配置中心来控制类的日志级别, 而不需要每次修改log4j xml 或者logback xml 配置文件再重新发布
*
*
* 示例:
*
*
*
*
*
* Slf4jUtil.isEnabledDebug("debug") = true;
* Slf4jUtil.isEnabledDebug("debug ") = true;
* Slf4jUtil.isEnabledDebug(" debug ") = true;
* Slf4jUtil.isEnabledDebug(" DEBUG ") = true;
* Slf4jUtil.isEnabledDebug(" track ") = true;
* Slf4jUtil.isEnabledDebug(" TRACK ") = true;
* Slf4jUtil.isEnabledDebug("TRACK") = true;
*
* Slf4jUtil.isEnabledDebug("info") = false;
* Slf4jUtil.isEnabledDebug("INFO") = false;
* Slf4jUtil.isEnabledDebug("error") = false;
* Slf4jUtil.isEnabledDebug("ERROR") = false;
*
* Slf4jUtil.isEnabledDebug("track1111") = false;
* Slf4jUtil.isEnabledDebug("debug1111") = false;
*
*
*
*
*
* @param logLevel
* the log level
* @return 如果 logLevel
是null,返回false
* 如果 logLevel
是empty,返回false
* @since 3.3.4
*/
public static boolean isEnabledDebug(String logLevel){
return containsTrimAndIgnoreCase(toList("track", "debug"), logLevel);
}
/**
* 判断指定的logvel 是否小于等于info级别.
*
*
* 使用场景: 可以使用配置中心来控制类的日志级别, 而不需要每次修改log4j xml 或者logback xml 配置文件再重新发布
*
*
* 示例:
*
*
*
*
*
* Slf4jUtil.isEnabledDebug("debug") = true;
* Slf4jUtil.isEnabledDebug("debug ") = true;
* Slf4jUtil.isEnabledDebug(" debug ") = true;
* Slf4jUtil.isEnabledDebug(" DEBUG ") = true;
* Slf4jUtil.isEnabledDebug(" track ") = true;
* Slf4jUtil.isEnabledDebug(" TRACK ") = true;
* Slf4jUtil.isEnabledDebug("TRACK") = true;
*
* Slf4jUtil.isEnabledDebug("info") = true;
* Slf4jUtil.isEnabledDebug("INFO") = true;
*
* Slf4jUtil.isEnabledDebug("error") = false;
* Slf4jUtil.isEnabledDebug("ERROR") = false;
*
* Slf4jUtil.isEnabledDebug("track1111") = false;
* Slf4jUtil.isEnabledDebug("debug1111") = false;
* Slf4jUtil.isEnabledDebug("info1111") = false;
*
*
*
*
*
* @param logLevel
* the log level
* @return 如果 logLevel
是null,返回false
* 如果 logLevel
是empty,返回false
* @since 3.3.4
*/
public static boolean isEnabledInfo(String logLevel){
return containsTrimAndIgnoreCase(toList("track", "debug", "info"), logLevel);
}
}