diff --git a/google-cloud-clients/google-cloud-contrib/google-cloud-logging-logback/README.md b/google-cloud-clients/google-cloud-contrib/google-cloud-logging-logback/README.md index ba815c51741e..54fc6a62aa75 100644 --- a/google-cloud-clients/google-cloud-contrib/google-cloud-logging-logback/README.md +++ b/google-cloud-clients/google-cloud-contrib/google-cloud-logging-logback/README.md @@ -88,6 +88,23 @@ Authentication See the [Authentication](https://github.com/googleapis/google-cloud-java#authentication) section in the base directory's README. +You can also specify custom credentials and other options by creating a subclass of `com.google.cloud.logging.logback.LoggingAppender` and override the method `createLoggingOptions()`. Your logback.xml configuration file must reference your subclass instead of `com.google.cloud.logging.logback.LoggingAppender`. + +```java +public class CustomLoggingAppender extends LoggingAppender { + @Override + public LoggingOptions createLoggingOptions() { + try { + return LoggingOptions.newBuilder() + .setCredentials(GoogleCredentials.fromStream( + new FileInputStream("/path/to/credentials.json"))) + .build(); + } catch (IOException e) { + throw new RuntimeException("Could not find credentials", e); + } + } +} +``` Limitations ----------- diff --git a/google-cloud-clients/google-cloud-contrib/google-cloud-logging-logback/src/main/java/com/google/cloud/logging/logback/LoggingAppender.java b/google-cloud-clients/google-cloud-contrib/google-cloud-logging-logback/src/main/java/com/google/cloud/logging/logback/LoggingAppender.java index 23d5e4183fdf..ab92cccdf092 100644 --- a/google-cloud-clients/google-cloud-contrib/google-cloud-logging-logback/src/main/java/com/google/cloud/logging/logback/LoggingAppender.java +++ b/google-cloud-clients/google-cloud-contrib/google-cloud-logging-logback/src/main/java/com/google/cloud/logging/logback/LoggingAppender.java @@ -38,6 +38,7 @@ import java.util.List; import java.util.Map; import java.util.Set; +import javax.annotation.Nonnull; /** * Logback appender for StackDriver Cloud Logging. @@ -60,6 +61,10 @@ *
  • <enhancer>com.example.enhancer2</enhancer> *
  • </appender> * + * + * Users can extend this class and override the method {@link #createLoggingOptions()} if you need + * to specify custom {@link LoggingOptions}, and reference the subclass in logback.xml instead of + * {@link LoggingAppender}. */ public class LoggingAppender extends UnsynchronizedAppenderBase { @@ -186,7 +191,7 @@ public synchronized void start() { } String getProjectId() { - return LoggingOptions.getDefaultInstance().getProjectId(); + return createLoggingOptions().getProjectId(); } @Override @@ -212,13 +217,29 @@ Logging getLogging() { if (logging == null) { synchronized (this) { if (logging == null) { - logging = LoggingOptions.getDefaultInstance().getService(); + logging = createLoggingOptions().getService(); } } } return logging; } + /** + * Creates the {@link LoggingOptions} to use for this {@link LoggingAppender}. Users can subclass + * {@link LoggingAppender} and override this method if they need to supply custom {@link + * LoggingOptions}, such as a specific credentials file. The default implementation returns {@link + * LoggingOptions#getDefaultInstance()}. Make sure to reference your own subclass in your logback + * configuration instead of {@link LoggingAppender} if you want to use custom {@link + * LoggingOptions}. + * + * @return the {@link LoggingOptions} that should be used for this {@link LoggingAppender}. May + * not be null. + */ + @Nonnull + public LoggingOptions createLoggingOptions() { + return LoggingOptions.getDefaultInstance(); + } + private LogEntry logEntryFor(ILoggingEvent e) { StringBuilder payload = new StringBuilder(e.getFormattedMessage()).append('\n'); writeStack(e.getThrowableProxy(), "", payload);