GKE & Stackdriver: Java logback logging format?

Viewed 7030

I have a project running Java in a docker image on Kubernetes. Logs are automatically ingested by the fluentd agent and end up in Stackdriver.

However, the format of the logs is wrong: Multiline logs get put into separate log lines in Stackdriver, and all logs have "INFO" log level, even though they are really warning, or error.

I have been searching for information on how to configure logback to output the correct format for this to work properly, but I can find no such guide in the google Stackdriver or GKE documentation.

My guess is that I should be outputting JSON of some form, but where do I find information on the format, or even a guide on how to properly set up this pipeline.

Thanks!

2 Answers

Google has provided a logback appender for Stackdriver, I have enhanced it abit to include the thread name in the logging label, so that it can be searched more easily.

pom.xml

<dependency>
    <groupId>com.google.cloud</groupId>
    <artifactId>google-cloud-logging-logback</artifactId>
    <version>0.116.0-alpha</version>
</dependency>

logback-spring.xml

<springProfile name="prod-gae">
    <appender name="CLOUD"
        class="com.google.cloud.logging.logback.LoggingAppender">
        <log>clicktrade.log</log>
        <loggingEventEnhancer>com.jimmy.clicktrade.arch.CommonEnhancer</loggingEventEnhancer>
        <flushLevel>WARN</flushLevel>
    </appender>
    <root level="info">
        <appender-ref ref="CLOUD" />
    </root>
</springProfile>

CommonEnhancer.java

public class CommonEnhancer implements LoggingEventEnhancer {

    @Override
    public void enhanceLogEntry(Builder builder, ILoggingEvent e) {
        builder.addLabel("thread", e.getThreadName());
    }

}

Surprisingly, the logback appender in MVN repository doesn't align with the github repo source code. I need to dig into the JAR source code for that. The latest version is 0.116.0-alpha, it seems it has version 0.120 in Github

https://github.com/googleapis/google-cloud-java/tree/master/google-cloud-clients/google-cloud-contrib/google-cloud-logging-logback

Related