关键词:Slf4j、log4j、可变参数、源码、懒加载、正则
想在打印日志后重复使用要输出的日志结果,所以想封装一下Slf4j可变参数打印的方法,同时拿到打印结果字符串;
本来思路很简单,将可变参数依次填入占位符即可,但是想看看Slf4j是怎么实现的,中间遇到了些问题,记录一下。
1、简单实现
暂时没有想到好方法通过输入的日志级别来动态调用log.info log.error等方法,所以写了三种不同级别的方法;
FILL_STRING用来判断是否还有未填充的占位符;FILL_FORMAT用来做String.replaceFirst的入参,表示正则表达式匹配"{}"的;
private static final String FILL_STRING = "{}";
private static final String FILL_FORMAT = "\\{\\}";
public static void info(String format, Object... argument) {
log.info(format, argument);
String logInfo = getLog(format, argument);
System.out.println("log is " + logInfo);
}
public static void error(String format, Object... argument) {
log.error(format, argument);
String logInfo = getLog(format, argument);
System.out.println("log is " + logInfo);
}
public static void warn(String format, Object... argument) {
log.warn(format, argument);
String logInfo = getLog(format, argument);
System.out.println("log is " + logInfo);
}
private static String getLog(String format, Object[] arg) {
// 防止空指针
if (arg == null) {
if (!format.contains(FILL_STRING)) {
return format;
}
return format.replaceFirst(FILL_FORMAT, "null");
}
StringBuilder formatBuilder = new StringBuilder(format);
for (Object anArg : arg) {
// 打印异常堆栈信息
if (anArg instanceof Throwable) {
Throwable temp = (Throwable) anArg;
formatBuilder.append(temp.toString()).append("\n");
StackTraceElement[] stackTrace = temp.getStackTrace();
for (StackTraceElement aStackTrace : stackTrace) {
formatBuilder.append(aStackTrace).append("\n");
}
continue;
}
formatBuilder = new StringBuilder(formatBuilder.toString().replaceFirst(FILL_FORMAT, anArg == null ? "null" : anArg.toString()));
}
format = formatBuilder.toString();
return format;
}2、查看Slf4j源码
跟踪调用链,跟到org.apache.logging.log4j.core.impl.Log4jLogEvent#Log4jLogEvent构造方法,第412行
this.message = message;
这句话执行前,message对象的formattedMessage还是null,执行后值就变成填充后的数据了;
刚开始还不能理解,一个赋值操作为什么右边的对象值会改变,后来才知道因为message对象是懒加载,在赋值的时候调用getFormattedMessage方法,如果值为null就进行填充可变参数操作。
附上源码:
public String getFormattedMessage() {
if (formattedMessage == null) {
final StringBuilder buffer = getThreadLocalStringBuilder();
// 这里填充可变参数
formatTo(buffer);
formattedMessage = buffer.toString();
StringBuilders.trimToMaxSize(buffer, Constants.MAX_REUSABLE_MESSAGE_SIZE);
}
return formattedMessage;
}
@Override
public void formatTo(final StringBuilder buffer) {
if (formattedMessage != null) {
buffer.append(formattedMessage);
} else {
if (indices[0] < 0) {
ParameterFormatter.formatMessage(buffer, messagePattern, argArray, usedCount);
} else {
ParameterFormatter.formatMessage2(buffer, messagePattern, argArray, usedCount, indices);
}
}
}
/**
* Replace placeholders in the given messagePattern with arguments.
*
* @param buffer the buffer to write the formatted message into
* @param messagePattern the message pattern containing placeholders.
* @param arguments the arguments to be used to replace placeholders.
*/
static void formatMessage2(final StringBuilder buffer, final String messagePattern,
final Object[] arguments, final int argCount, final int[] indices) {
if (messagePattern == null || arguments == null || argCount == 0) {
buffer.append(messagePattern);
return;
}
int previous = 0;
for (int i = 0; i < argCount; i++) {
buffer.append(messagePattern, previous, indices[i]);
previous = indices[i] + 2;
recursiveDeepToString(arguments[i], buffer, null);
}
buffer.append(messagePattern, previous, messagePattern.length());
}
版权声明:本文为ComeHereCH原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。