From cd706b7b5c845461dccede0ce615310261dd6ab8 Mon Sep 17 00:00:00 2001 From: Mike Eltsufin Date: Tue, 26 Dec 2023 12:02:55 -0500 Subject: [PATCH] fix: delegate to all parent logging options (#2500) Support setting logDestinationProjectId. Fixes: #2496. --- .../cloud/spring/logging/LoggingAppender.java | 9 +-------- .../cloud/spring/logging/LoggingAppenderTests.java | 13 +++++++++++++ 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/spring-cloud-gcp-logging/src/main/java/com/google/cloud/spring/logging/LoggingAppender.java b/spring-cloud-gcp-logging/src/main/java/com/google/cloud/spring/logging/LoggingAppender.java index c56976278c..83e4de1ac3 100644 --- a/spring-cloud-gcp-logging/src/main/java/com/google/cloud/spring/logging/LoggingAppender.java +++ b/spring-cloud-gcp-logging/src/main/java/com/google/cloud/spring/logging/LoggingAppender.java @@ -16,7 +16,6 @@ package com.google.cloud.spring.logging; -import com.google.auth.Credentials; import com.google.cloud.logging.LoggingOptions; import com.google.cloud.spring.core.UserAgentHeaderProvider; @@ -38,13 +37,7 @@ public class LoggingAppender extends com.google.cloud.logging.logback.LoggingApp protected LoggingOptions getLoggingOptions() { if (loggingOptions == null) { - LoggingOptions.Builder loggingOptionsBuilder = LoggingOptions.newBuilder(); - - // only credentials are set in the options of the parent class - Credentials credentials = super.getLoggingOptions().getCredentials(); - if (credentials != null) { - loggingOptionsBuilder.setCredentials(credentials); - } + LoggingOptions.Builder loggingOptionsBuilder = super.getLoggingOptions().toBuilder(); // set User-Agent loggingOptionsBuilder.setHeaderProvider(new UserAgentHeaderProvider(this.getClass())); diff --git a/spring-cloud-gcp-logging/src/test/java/com/google/cloud/spring/logging/LoggingAppenderTests.java b/spring-cloud-gcp-logging/src/test/java/com/google/cloud/spring/logging/LoggingAppenderTests.java index e1d1bc6b5d..cfdb4681f5 100644 --- a/spring-cloud-gcp-logging/src/test/java/com/google/cloud/spring/logging/LoggingAppenderTests.java +++ b/spring-cloud-gcp-logging/src/test/java/com/google/cloud/spring/logging/LoggingAppenderTests.java @@ -38,4 +38,17 @@ void testGetLoggingOptions() { .contains("spring-cloud-gcp-logging") .contains("Spring"); } + + @Test + void testSetLogDestinationProjectId() { + LoggingAppender loggingAppender = new LoggingAppender(); + loggingAppender.setCredentialsFile("src/test/resources/fake-project-key.json"); + loggingAppender.setLogDestinationProjectId("my-log-destination-project"); + assertThat(loggingAppender.getLoggingOptions().getCredentials()).isNotNull(); + assertThat(loggingAppender.getLoggingOptions().getProjectId()).isEqualTo("my-log-destination-project"); + assertThat(loggingAppender.getLoggingOptions().getUserAgent()) + .isNotNull() + .contains("spring-cloud-gcp-logging") + .contains("Spring"); + } }