google/flogger

Support change the context prefix and suffix format of KeyValueFormatter

kuraun opened this issue · 0 comments

Hi, Flogger Team.
I like Flogger very much, but I would like to see one improvement.
Why not allow the KeyValueFormatter prefix and suffix to be set with LogData.getMetaData () instead of constants?
I find it very difficult to use because I can't change the format of [CONTEXT ~].
It is possible to create own KeyValueFormatter by extending AbstractBackend and AbstractLogRecord,
but I would like a more convenient way to change only the prefix and suffix.

For example, like the following.

// AbstractRecord.java
  protected LogMessageFormatter getLogMessageFormatter() {
   return SimpleMessageFormatter.getDefaultFormatter(data.getMetadata());
 }

// SimpleMessageFormatter.java
public static LogMessageFormatter getDefaultFormatter(Metadata metadata) {
   if (metadata == null
           || metadata.equals(Metadata.empty())
           || metadata.findValue(LogContext.Key.KVF_PREFIX) == null
           || metadata.findValue(LogContext.Key.KVF_SUFFIX) == null) {
     return DEFAULT_FORMATTER;
   }
   return newFormatter(DEFAULT_KEYS_TO_IGNORE,
           metadata.findValue(LogContext.Key.KVF_PREFIX),
           metadata.findValue(LogContext.Key.KVF_SUFFIX));
 }

// ExtendedFluentLogger.java
...
 /** Logging context implementing the fully specified API for this logger. */
 // VisibleForTesting
 final class Context extends LogContext<ExtendedFluentLogger, Api> implements Api {
   private Context(Level level, boolean isForced) {
     super(level, isForced);
     addMetadata(Key.KVF_PREFIX, "MyPrefix");
     addMetadata(Key.KVF_SUFFIX, "MySuffix");
   }

Related: #60

Thanks.