Skip to content

Commit

Permalink
#3215 allow user to specify custom LoggingOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
olavloite committed Mar 23, 2019
1 parent db18d86 commit c4fd4da
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
-----------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.annotation.Nonnull;

/**
* <a href="https://logback.qos.ch/">Logback</a> appender for StackDriver Cloud Logging.
Expand All @@ -60,6 +61,10 @@
* <li>&lt;enhancer&gt;com.example.enhancer2&lt;/enhancer&gt;
* <li>&lt;/appender&gt;
* </ul>
*
* 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<ILoggingEvent> {

Expand Down Expand Up @@ -186,7 +191,7 @@ public synchronized void start() {
}

String getProjectId() {
return LoggingOptions.getDefaultInstance().getProjectId();
return createLoggingOptions().getProjectId();
}

@Override
Expand All @@ -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 <code>null</code>.
*/
@Nonnull
public LoggingOptions createLoggingOptions() {
return LoggingOptions.getDefaultInstance();
}

private LogEntry logEntryFor(ILoggingEvent e) {
StringBuilder payload = new StringBuilder(e.getFormattedMessage()).append('\n');
writeStack(e.getThrowableProxy(), "", payload);
Expand Down

0 comments on commit c4fd4da

Please sign in to comment.