下面列出了org.slf4j.helpers.FormattingTuple#getMessage ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private LogRecord eventToRecord(LoggingEvent event, Level julLevel) {
String format = event.getMessage();
Object[] arguments = event.getArgumentArray();
FormattingTuple ft = MessageFormatter.arrayFormat(format, arguments);
if (ft.getThrowable() != null && event.getThrowable() != null) {
throw new IllegalArgumentException("both last element in argument array and last argument are of type Throwable");
}
Throwable t = event.getThrowable();
if (ft.getThrowable() != null) {
t = ft.getThrowable();
throw new IllegalStateException("fix above code");
}
LogRecord record = new LogRecord(julLevel, ft.getMessage());
record.setLoggerName(event.getLoggerName());
record.setMillis(event.getTimeStamp());
record.setSourceClassName(EventConstants.NA_SUBST);
record.setSourceMethodName(EventConstants.NA_SUBST);
record.setThrown(t);
return record;
}
@SuppressWarnings("ThrowableResultOfMethodCallIgnored")
@Override
public void append(LogRecord record, StringBuilder builder) {
FormattingTuple formattingTuple = MessageFormatter.arrayFormat(record.getMessage(), record.getParameters());
String message = formattingTuple.getMessage();
Throwable throwable = formattingTuple.getThrowable();
if (throwable != null) {
StringWriter writer = new StringWriter(100);
PrintWriter printWriter = new PrintWriter(writer);
throwable.printStackTrace(printWriter);
printWriter.flush();
printWriter.close();
writer.flush();
message += " " + writer.toString();
}
builder.append(message);
}
/**
* append log with pattern
*
* @param appendLogPattern like "aaa {} bbb {} ccc"
* @param appendLogArguments like "111, true"
*/
public static void log(String appendLogPattern, Object... appendLogArguments) {
FormattingTuple ft = MessageFormatter.arrayFormat(appendLogPattern, appendLogArguments);
String appendLog = ft.getMessage();
/*appendLog = appendLogPattern;
if (appendLogArguments!=null && appendLogArguments.length>0) {
appendLog = MessageFormat.format(appendLogPattern, appendLogArguments);
}*/
StackTraceElement callInfo = new Throwable().getStackTrace()[1];
logDetail(callInfo, appendLog);
}
/**
* append log with pattern
*
* @param appendLogPattern like "aaa {} bbb {} ccc"
* @param appendLogArguments like "111, true"
*/
public static void log(String appendLogPattern, Object ... appendLogArguments) {
FormattingTuple ft = MessageFormatter.arrayFormat(appendLogPattern, appendLogArguments);
String appendLog = ft.getMessage();
/*appendLog = appendLogPattern;
if (appendLogArguments!=null && appendLogArguments.length>0) {
appendLog = MessageFormat.format(appendLogPattern, appendLogArguments);
}*/
StackTraceElement callInfo = new Throwable().getStackTrace()[1];
logDetail(callInfo, appendLog);
}
/**
* append log with pattern
*
* @param appendLogPattern like "aaa {} bbb {} ccc"
* @param appendLogArguments like "111, true"
*/
public static void log(String appendLogPattern, Object... appendLogArguments) {
FormattingTuple ft = MessageFormatter.arrayFormat(appendLogPattern, appendLogArguments);
String appendLog = ft.getMessage();
/*appendLog = appendLogPattern;
if (appendLogArguments!=null && appendLogArguments.length>0) {
appendLog = MessageFormat.format(appendLogPattern, appendLogArguments);
}*/
StackTraceElement callInfo = new Throwable().getStackTrace()[1];
logDetail(callInfo, appendLog);
}
public static String getText(String key,Object... args){
String localization = threadLocalization.get();
if(localization == null){
log.warn("localization is not in threadLocalization,check it !");
localization = DefaultLocalization;
}
Properties properties = messages.get(localization);
if(properties == null){
properties = messages.get(DefaultLocalization);
}
FormattingTuple formattingTuple = MessageFormatter.arrayFormat(properties.getProperty(key),args);
return formattingTuple.getMessage();
}
/**
* append log with pattern
*
* @param appendLogPattern like "aaa {} bbb {} ccc"
* @param appendLogArguments like "111, true"
*/
public static void log(String appendLogPattern, Object ... appendLogArguments) {
FormattingTuple ft = MessageFormatter.arrayFormat(appendLogPattern, appendLogArguments);
String appendLog = ft.getMessage();
/**appendLog = appendLogPattern;
if (appendLogArguments!=null && appendLogArguments.length>0) {
appendLog = MessageFormat.format(appendLogPattern, appendLogArguments);
}*/
StackTraceElement callInfo = new Throwable().getStackTrace()[1];
logDetail(callInfo, appendLog);
}
/**
* append log with pattern
*
* @param appendLogPattern like "aaa {} bbb {} ccc"
* @param appendLogArguments like "111, true"
*/
public static void log(String appendLogPattern, Object... appendLogArguments) {
FormattingTuple ft = MessageFormatter.arrayFormat(appendLogPattern, appendLogArguments);
String appendLog = ft.getMessage();
/*appendLog = appendLogPattern;
if (appendLogArguments!=null && appendLogArguments.length>0) {
appendLog = MessageFormat.format(appendLogPattern, appendLogArguments);
}*/
StackTraceElement callInfo = new Throwable().getStackTrace()[1];
logDetail(callInfo, appendLog);
}
@Override
public void log(LogLevel level, String format, Object... arguments) {
FormattingTuple formattingTuple = MessageFormatter.arrayFormat(format, arguments);
String message = formattingTuple.getMessage();
Throwable throwable = formattingTuple.getThrowable();
logger.log(LEVELS.get(level), message, throwable);
}
/**
* append log with pattern
*
* @param appendLogPattern like "aaa {} bbb {} ccc"
* @param appendLogArguments like "111, true"
*/
public static void log(String appendLogPattern, Object ... appendLogArguments) {
FormattingTuple ft = MessageFormatter.arrayFormat(appendLogPattern, appendLogArguments);
String appendLog = ft.getMessage();
/*appendLog = appendLogPattern;
if (appendLogArguments!=null && appendLogArguments.length>0) {
appendLog = MessageFormat.format(appendLogPattern, appendLogArguments);
}*/
StackTraceElement callInfo = new Throwable().getStackTrace()[1];
logDetail(callInfo, appendLog);
}
@Override
public void log(@Nonnull final Level level, @Nonnull final String message, @Nullable final Object... objects) {
//noinspection OptionalGetWithoutIsPresent
final Class<?> caller = stackWalker.walk(s -> s.skip(2).findFirst()).get().getDeclaringClass();
final Logger logger = LoggerFactory.getLogger(caller);
final FormattingTuple tuple = MessageFormatter.arrayFormat(message, objects);
final String formatted = tuple.getMessage();
if(logger != null) {
switch(level) {
case TRACE: {
logger.trace(formatted);
if(tuple.getThrowable() != null) {
logger.trace("Stacktrace: ", tuple.getThrowable());
}
break;
}
case DEBUG: {
logger.debug(formatted);
if(tuple.getThrowable() != null) {
logger.debug("Stacktrace: ", tuple.getThrowable());
}
break;
}
case INFO: {
logger.info(formatted);
if(tuple.getThrowable() != null) {
logger.info("Stacktrace: ", tuple.getThrowable());
}
break;
}
case WARN: {
logger.warn(formatted);
if(tuple.getThrowable() != null) {
logger.warn("Stacktrace: ", tuple.getThrowable());
}
break;
}
case ERROR: {
logger.error(formatted);
if(tuple.getThrowable() != null) {
logger.error("Stacktrace: ", tuple.getThrowable());
}
break;
}
}
}
}
/**
* 使用指定参数格式化指定模板,并转换为字符串
*
* @param template
* @param parameters
* @return
*/
public static final String format(String template, Object... parameters) {
FormattingTuple formatter = MessageFormatter.arrayFormat(template, parameters);
return formatter.getMessage();
}
/**
* 格式化字符串,此方法是抽取slf4j的核心方法.
*
* <p>
* 在java中,常会拼接字符串生成新的字符串值,在字符串拼接过程中容易写错或者位置写错<br>
* <br>
* slf4j的log支持格式化输出log,比如:<br>
* </p>
*
* <ul>
* <li>LOGGER.debug("{}","feilong");</li>
* <li>LOGGER.info("{},{}","feilong","hello");</li>
* </ul>
*
* 这些写法非常简洁且有效,不易出错
*
* <br>
* 因此,你可以在代码中出现这样的写法:
*
* <pre class="code">
* throw new IllegalArgumentException(Slf4jUtil.format(
* "callbackUrl:[{}] ,length:[{}] can't {@code >}{}",
* callbackUrl,
* callbackUrlLength,
* callbackUrlMaxLength)
* </pre>
*
* 又或者
*
* <pre class="code">
* return Slf4jUtil.format("{} [{}]", encode, encode.length());
* </pre>
*
* @param messagePattern
* message的格式,比如 callbackUrl:[{}] ,length:[{}]
* @param args
* 参数
* @return 如果 <code>messagePattern</code> 是null,返回 null<br>
* 如果 <code>args</code> 是null,返回 <code>messagePattern</code><br>
* @see org.slf4j.helpers.FormattingTuple
* @see org.slf4j.helpers.MessageFormatter#arrayFormat(String, Object[])
* @see org.slf4j.helpers.FormattingTuple#getMessage()
* @since 1.6.0
*/
public static String format(String messagePattern,Object...args){
FormattingTuple formattingTuple = MessageFormatter.arrayFormat(messagePattern, args);
return formattingTuple.getMessage();
}
/**
* Format with one object.
*
* @param format
* Format to use
* @param arg
* One argument
* @return The message
*/
private String _format (final String format, final Object arg)
{
final FormattingTuple tuple = MessageFormatter.format (format, arg);
return tuple.getMessage ();
}
/**
* Format with two objects.
*
* @param format
* Format to use
* @param first
* First argument
* @param second
* Second argument
* @return The message
*/
private String _format (final String format, final Object first, final Object second)
{
final FormattingTuple tuple = MessageFormatter.format (format, first, second);
return tuple.getMessage ();
}
/**
* Format with array.
*
* @param format
* Format to use
* @param array
* List of arguments
* @return The message
*/
private String _format (final String format, final Object [] array)
{
final FormattingTuple tuple = MessageFormatter.format (format, array);
return tuple.getMessage ();
}