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.
- 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;
}
}
- Add the value of the annotation to the dropwizard configuration (yaml) file's logging section:
logging:
level: INFO
appenders:
- type: stackdriver
- 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
- 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.