SimpleMessageFormatter

public final class SimpleMessageFormatter

Helper class for formatting LogData as text. This class is useful for any logging backend which performs unstructured, text only, logging. Note however that it makes several assumptions regarding metadata and formatting, which may not apply to every text based logging backend.

This primarily exists to support both the JDK logging classes and text only Android backends. Code in here may be factored out as necessary to support other use cases in future.

If a text based logger backend is not performance critical, then it should just append the log message and metadata to a local buffer. For example:


MetadataProcessor metadata =
    MetadataProcessor.forScopeAndLogSite(Platform.getInjectedMetadata(), logData.getMetadata());
StringBuilder buffer = new StringBuilder();
// Optional prefix goes here...
SimpleMessageFormatter.getDefaultFormatter().append(logData, metadata, buffer);
// Optional suffix goes here...
String message = buffer.toString();

If additional metadata keys, other than the cause are to be omitted, then getSimpleFormatterIgnoring can be used to obtain a static formatter, instead of using the default.

Types

Link copied to clipboard
public interface SimpleLogHandler

Functions

Link copied to clipboard
@CanIgnoreReturnValue()
public static StringBuilder appendContext(MetadataProcessor metadataProcessor, MetadataHandler<MetadataKey.KeyValueHandler> metadataHandler, StringBuilder buffer)
Appends formatted context information to the given buffer using the supplied metadata handler.
Link copied to clipboard
public static void format(LogData logData, SimpleMessageFormatter.SimpleLogHandler receiver)
Link copied to clipboard
Returns the singleton default log message formatter.
Link copied to clipboard
public static String getLiteralLogMessage(LogData logData)
Returns the single literal value as a string.
Link copied to clipboard
public static LogMessageFormatter getSimpleFormatterIgnoring(Array<MetadataKey<? extends Object>> extraIgnoredKeys)
Returns a log message formatter which formats log messages in the form:

Log message [CONTEXT key="value" id=42 ]
with context from the log data and scope, merged together in a sequence of key/value pairs after the formatted message.
Link copied to clipboard
public static boolean mustBeFormatted(LogData logData, MetadataProcessor metadata, Set<MetadataKey<? extends Object>> keysToIgnore)
An internal helper method for logger backends which are aggressively optimized for performance.