0
votes

My application is in dropwizard, recently I have uploaded it on gcp. For logging we need to use stackdriver. I am not gcp expert just followed https://cloud.google.com/logging/docs/setup/java . I used logging appender way to show logs on stackdriver.

As much I understood to implement stackdriver logging using logging appender we need to do basic 2 things :

  1. add appender for cloud in logback.xml
  2. add dependancy 'com.google.cloud:google-cloud-logging-logback:0.77.0-alpha' in build.gradle.

I am not able to see logs on stackdriver whenever I am selecting the instance where application is deployed.

1

1 Answers

0
votes

For dropwizard it's slightly more complicated as it's very opinionated. Here are the steps you need to use stackdriver logging with cloud logging. Note: I used version 0.77.0-alpha of the com.google.cloud:google-cloud-logging-logback library.

  1. Create a class that implements AppenderFactory<ILoggingEvent> that has a JsonTypeName annotation:
@JsonTypeName("stackdriver")
public class StackdriverLoggingAppenderFactory implements AppenderFactory<ILoggingEvent> {

    @Override
    public Appender<ILoggingEvent> build(LoggerContext context, String applicationName,
                                         LayoutFactory<ILoggingEvent> layoutFactory,
                                         LevelFilterFactory<ILoggingEvent> levelFilterFactory,
                                         AsyncAppenderFactory<ILoggingEvent> asyncAppenderFactory) {
        final LoggingAppender appender = new LoggingAppender();
        appender.setName("stackdriver");
        appender.setContext(context);
        appender.start();
        return appender;
    }
}
  1. Add the value of the annotation to the dropwizard configuration (yaml) file's logging section:
logging:
  level: INFO
  appenders:
  - type: stackdriver
  1. Add a file called io.dropwizard.logging.AppenderFactory to src/main/resources/META-INF.services. The file should contain the fully-qualified package and class name creted in Step 1, e.g.,
com.acme.gcp.logging.StackdriverLoggingAppenderFactory
  1. Finally, add an environment variable with the key GOOGLE_CLOUD_PROJECT and value of the project id where the instance is run.

I tested this by running an application locally on my laptop and I could see the log entries in the Google Cloud Console under Stackdriver Logging.