Java Code Examples for org.slf4j.helpers.FormattingTuple#getMessage()

The following examples show how to use org.slf4j.helpers.FormattingTuple#getMessage() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JDK14LoggerAdapter.java    From HttpSessionReplacer with MIT License 6 votes vote down vote up
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;
}
 
Example 2
Source File: MessageValueSupplier.java    From slf4android with MIT License 6 votes vote down vote up
@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);
}
 
Example 3
Source File: JobLogger.java    From datax-web with MIT License 5 votes vote down vote up
/**
 * 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);
}
 
Example 4
Source File: XxlJobLogger.java    From microservices-platform with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example 5
Source File: XxlJobLogger.java    From zuihou-admin-boot with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example 6
Source File: LocalizationMessage.java    From PeonyFramwork with Apache License 2.0 5 votes vote down vote up
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();
}
 
Example 7
Source File: XxlJobLogger.java    From xmfcn-spring-cloud with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example 8
Source File: XxlJobLogger.java    From zuihou-admin-cloud with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example 9
Source File: JavaLoggerFacade.java    From eclair with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 10
Source File: XxlJobLogger.java    From xxl-job with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 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);
}
 
Example 11
Source File: DefaultLogAdapter.java    From catnip with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@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;
            }
        }
    }
}
 
Example 12
Source File: StringUtility.java    From jstarcraft-core with Apache License 2.0 2 votes vote down vote up
/**
 * 使用指定参数格式化指定模板,并转换为字符串
 * 
 * @param template
 * @param parameters
 * @return
 */
public static final String format(String template, Object... parameters) {
    FormattingTuple formatter = MessageFormatter.arrayFormat(template, parameters);
    return formatter.getMessage();
}
 
Example 13
Source File: Slf4jUtil.java    From feilong-core with Apache License 2.0 2 votes vote down vote up
/**
 * 格式化字符串,此方法是抽取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();
}
 
Example 14
Source File: Slf4jAdapter.java    From ph-schematron with Apache License 2.0 2 votes vote down vote up
/**
 * 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 ();
}
 
Example 15
Source File: Slf4jAdapter.java    From ph-schematron with Apache License 2.0 2 votes vote down vote up
/**
 * 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 ();
}
 
Example 16
Source File: Slf4jAdapter.java    From ph-schematron with Apache License 2.0 2 votes vote down vote up
/**
 * 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 ();
}