google-cloud-java icon indicating copy to clipboard operation
google-cloud-java copied to clipboard

Structured Logging with Stack Driver Logback Appender

Open bspradling opened this issue 9 years ago • 7 comments

Currently there is a Java Logback Appender that allows Java Applications to utilize the SLF4J interface and log to StackDriver.

SLF4J has the concept of Marker objects that allows users to include objects to enrich the log messages. It seems that the StackDriver Logback Appender is ignoring these Marker objects while logging to StackDriver.

To show an example, the following log statement:

logger.with(anObject).info("The request was successful");

will result a Logstash with the following structure:

{
   labels: { ... labels here ... },
   logName: 'java.log',
   resource: { ... resource here ... },
   severity: 'INFO',
   textPayload: "The request was successful",
   timestamp: "2017-11-30T01:21:59.205Z
}

It seems like the Appender is completely ignoring these Marker objects when the should be getting jumped into the jsonPayload attribute of the com.google.logging LogEntry class.

Any help surrounding getting structured logging working with the StackDriver Logback Appender working would be greatly appreciated!

bspradling avatar Nov 30 '17 07:11 bspradling

As an update, it appears the issue would be here:

private LogEntry logEntryFor(ILoggingEvent e) {
    String payload = e.getFormattedMessage();
    Level level = e.getLevel();
    LogEntry.Builder builder =
        LogEntry.newBuilder(Payload.StringPayload.of(payload))
            .setTimestamp(e.getTimeStamp())
            .setSeverity(severityFor(level));

    builder
        .addLabel(LEVEL_NAME_KEY, level.toString())
        .addLabel(LEVEL_VALUE_KEY, String.valueOf(level.toInt()));

    if (loggingEnhancers != null) {
      for (LoggingEnhancer enhancer : loggingEnhancers) {
        enhancer.enhanceLogEntry(builder);
      }
    }

    return builder.build();
  }

It assumes that all data coming into the LoggingEvent is a StringPayload

bspradling avatar Nov 30 '17 19:11 bspradling

@jabubake @andreamlin I'm not too sure how severe this problem is. I tagged P1, but please change if appropriate.

pongad avatar Dec 07 '17 02:12 pongad

@bspradling8 : Markers is not currently supported, but we will look into adding them in a release soon.

jabubake avatar Dec 11 '17 17:12 jabubake

@dzlier-gcp FYI

jabubake avatar Dec 11 '17 18:12 jabubake

@jabubake I think @andreamlin is now working on the autogen-clients, so I think this feature is on my plate. I'll self-assign.

pongad avatar Dec 11 '17 22:12 pongad

Move this issue to feature backlog and close it here. The place can continue to be used for discussion.

yihanzhen avatar Jan 30 '18 01:01 yihanzhen

Is this still in backlog? or has it been fixed?

sschwell avatar Feb 01 '21 16:02 sschwell