下面列出了怎么用org.slf4j.helpers.FormattingTuple的API类实例代码及写法,或者点击链接到github查看源代码。
@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);
}
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;
}
/**
* 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);
}
/**
* This prints the level and formatted message to
* System.out.
*/
public void formatAndLog(int level, String format, Object... arguments)
{
if (logOutput)
{
FormattingTuple tp = MessageFormatter.arrayFormat(format, arguments);
log(level, tp.getMessage());
}
}
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);
}
@SuppressWarnings("ThrowableResultOfMethodCallIgnored")
private void doWarn(FormattingTuple tuple) {
if (tuple.getThrowable() == null) {
log.warn(tuple.getMessage());
} else {
log.warn(tuple.getMessage(), tuple.getThrowable());
}
}
/**
* 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);
}
private void formatAndLog(int level, String format, Object arg1, Object arg2) {
if (!isLevelEnabled(level)) {
return;
}
FormattingTuple tp = MessageFormatter.format(format, arg1, arg2);
log(level, tp.getMessage(), tp.getThrowable());
}
public void info(String msg, Object... arguments)
{
if (isInfoEnabled())
{
FormattingTuple ft = MessageFormatter.arrayFormat(
msg, evaluateLambdaArgs(arguments));
this.info(SecretDetector.maskSecrets(ft.getMessage()));
}
}
@Override
public void error(String format, Object... args) {
if (isErrorEnabled()) {
FormattingTuple ft = MessageFormatter.arrayFormat(format, args);
JbootExceptionHolder.hold(ft.getMessage(), ft.getThrowable());
log.error(format, args);
}
}
@Override
public void trace(String format, Object... args) {
if (isTraceEnabled()) {
FormattingTuple ft = MessageFormatter.arrayFormat(format, args);
log.log(null, callerFQCN, LocationAwareLogger.TRACE_INT, ft.getMessage(), NULL_ARGS, ft.getThrowable());
}
}
@Override
public void debug(String format, Object... args) {
if (isDebugEnabled()) {
FormattingTuple ft = MessageFormatter.arrayFormat(format, args);
log.log(null, callerFQCN, LocationAwareLogger.DEBUG_INT, ft.getMessage(), NULL_ARGS, ft.getThrowable());
}
}
@Override
public void info(String format, Object... args) {
if (isInfoEnabled()) {
FormattingTuple ft = MessageFormatter.arrayFormat(format, args);
log.log(null, callerFQCN, LocationAwareLogger.INFO_INT, ft.getMessage(), NULL_ARGS, ft.getThrowable());
}
}
@Override
public void warn(String format, Object... args) {
if (isWarnEnabled()) {
FormattingTuple ft = MessageFormatter.arrayFormat(format, args);
log.log(null, callerFQCN, LocationAwareLogger.WARN_INT, ft.getMessage(), NULL_ARGS, ft.getThrowable());
}
}
private void formatAndAppend(String format, Object... arguments) {
if (appender == null) {
return;
}
FormattingTuple tp = MessageFormatter.arrayFormat(format, arguments);
append(tp.getMessage());
}
public void error(String msg, Object... arguments)
{
if (isErrorEnabled())
{
FormattingTuple ft = MessageFormatter.arrayFormat(
msg, evaluateLambdaArgs(arguments));
this.error(SecretDetector.maskSecrets(ft.getMessage()));
}
}
private void add(Message.Level level, FormattingTuple tp) {
if (tp.getThrowable() == null) {
add(level, tp.getMessage());
} else {
add(level, tp.getMessage(), tp.getThrowable());
}
}
public void warn(String msg, Object... arguments)
{
if (isWarnEnabled())
{
FormattingTuple ft = MessageFormatter.arrayFormat(
msg, evaluateLambdaArgs(arguments));
this.warn(SecretDetector.maskSecrets(ft.getMessage()));
}
}
private void formatAndLog( LogLevel logLevel, String format, Object... arguments ) {
if ( !LogFXLogFactory.INSTANCE.isLogLevelEnabled( logLevel ) ) {
return;
}
FormattingTuple tp = MessageFormatter.arrayFormat( format, arguments );
log( logLevel, tp.getMessage(), tp.getThrowable() );
}
public static void info(org.apache.log4j.Logger logger, String messagePattern, Object object) {
FormattingTuple format = MessageFormatter.format(messagePattern, object);
logger.info(format.getMessage());
}
@Override
public void error(String msg, Throwable t, Object...arguments) {
FormattingTuple ft = MessageFormatter.arrayFormat(msg, arguments);
logger.error(ft.getMessage(), ft.getThrowable());
}
@Override
public void log(String message, Object... arguments) {
FormattingTuple ft = MessageFormatter.arrayFormat(message, arguments);
log(ft.getMessage());
}
@Test
public void messageFormatterTest()
{
FormattingTuple tuple = MessageFormatter.format( "this is a test {} and {}", "test", "foobar" );
assertEquals( "this is a test test and foobar", tuple.getMessage() );
}
public static void dbDebugLog(Logger logger, long startTime, Object result, String sql, Object... objects) {
String messagePattern = sql.replace("?", "{}");
FormattingTuple format = MessageFormatter.arrayFormat(messagePattern, objects);
logger.debug("[openzaly-db] cost:{}ms result:{} sql:{}", System.currentTimeMillis() - startTime, result,
format.getMessage());
}
public void warn(String format, Object[] argArray)
{
FormattingTuple ft = MessageFormatter.arrayFormat(format, argArray);
log(LOG_LEVEL_WARN, ft.getMessage(), ft.getThrowable());
}
private void log(Level level, String s, Object[] objects) {
if (SshdPlugin.instance != null && isEnabled(level)) {
FormattingTuple ft = MessageFormatter.arrayFormat(s, objects);
SshdPlugin.instance.getLogger().log(level, ft.getMessage(), ft.getThrowable());
}
}