From 4901aaa98c3d6f67443585144ec6265d3b845b51 Mon Sep 17 00:00:00 2001 From: zanmagerl Date: Sun, 18 Jun 2023 11:46:23 +0200 Subject: [PATCH 1/3] Add option to configure GCP logging extension's logging target --- .../logging/deployment/LoggingBuildSteps.java | 13 +++++++++++++ .../logging/runtime/LoggingConfiguration.java | 18 +++++++++++++++++- .../logging/runtime/LoggingHandler.java | 16 +++++++++++++--- 3 files changed, 43 insertions(+), 4 deletions(-) diff --git a/logging/deployment/src/main/java/io/quarkiverse/googlecloudservices/logging/deployment/LoggingBuildSteps.java b/logging/deployment/src/main/java/io/quarkiverse/googlecloudservices/logging/deployment/LoggingBuildSteps.java index 5d5cba34..3e7ff8df 100644 --- a/logging/deployment/src/main/java/io/quarkiverse/googlecloudservices/logging/deployment/LoggingBuildSteps.java +++ b/logging/deployment/src/main/java/io/quarkiverse/googlecloudservices/logging/deployment/LoggingBuildSteps.java @@ -11,10 +11,12 @@ import io.quarkus.deployment.annotations.Record; import io.quarkus.deployment.builditem.FeatureBuildItem; import io.quarkus.deployment.builditem.LogHandlerBuildItem; +import io.quarkus.deployment.builditem.RunTimeConfigurationDefaultBuildItem; public class LoggingBuildSteps { private static final String FEATURE = "google-cloud-logging"; + private static final String QUARKUS_CONSOLE_LOGGING_CONFIG_KEY = "quarkus.log.console.enable"; @BuildStep public FeatureBuildItem feature() { @@ -38,4 +40,15 @@ public UnremovableBeanBuildItem helperClasses() { public LogHandlerBuildItem handler(LoggingConfiguration config, LoggingHandlerFactory factory) { return new LogHandlerBuildItem(factory.create(config)); } + + @BuildStep + public RunTimeConfigurationDefaultBuildItem configurationDefaultBuildItem(LoggingConfiguration config) { + boolean enableConsoleLogging = true; + // We should use configuration only if the GCP logging extension is enabled + if (config.enabled) { + enableConsoleLogging = config.enableConsoleLogging; + } + return new RunTimeConfigurationDefaultBuildItem(QUARKUS_CONSOLE_LOGGING_CONFIG_KEY, + Boolean.toString(enableConsoleLogging)); + } } diff --git a/logging/runtime/src/main/java/io/quarkiverse/googlecloudservices/logging/runtime/LoggingConfiguration.java b/logging/runtime/src/main/java/io/quarkiverse/googlecloudservices/logging/runtime/LoggingConfiguration.java index 08a34f44..5601ebc1 100644 --- a/logging/runtime/src/main/java/io/quarkiverse/googlecloudservices/logging/runtime/LoggingConfiguration.java +++ b/logging/runtime/src/main/java/io/quarkiverse/googlecloudservices/logging/runtime/LoggingConfiguration.java @@ -3,6 +3,7 @@ import java.util.Map; import java.util.Optional; +import com.google.cloud.logging.LoggingHandler.LogTarget; import com.google.cloud.logging.Severity; import com.google.cloud.logging.Synchronicity; @@ -11,7 +12,7 @@ import io.quarkus.runtime.annotations.ConfigPhase; import io.quarkus.runtime.annotations.ConfigRoot; -@ConfigRoot(name = "google.cloud.logging", phase = ConfigPhase.RUN_TIME) +@ConfigRoot(name = "google.cloud.logging", phase = ConfigPhase.BUILD_AND_RUN_TIME_FIXED) public class LoggingConfiguration { /** @@ -26,6 +27,12 @@ public class LoggingConfiguration { @ConfigItem(defaultValue = "true") public boolean enabled; + /** + * Enable or disable default Quarkus console logging. + */ + @ConfigItem + public boolean enableConsoleLogging; + /** * Configure base formatting to be either plain text or * structured json. Allowed values: TEXT|JSON @@ -74,6 +81,14 @@ public class LoggingConfiguration { @ConfigItem public StructuredConfig structured; + /** + * Configures if logs should be written to stdout or stderr instead of using Google Cloud Operations API. + * Useful if app is deployed to managed Google Cloud Platform environment with installed logger agent. + * Possible values: STDOUT, STDERR and CLOUD_LOGGING. + */ + @ConfigItem(defaultValue = "STDOUT") + public LogTarget logTarget; + @ConfigGroup public static class StructuredConfig { @@ -198,4 +213,5 @@ public enum LogFormat { TEXT, JSON } + } \ No newline at end of file diff --git a/logging/runtime/src/main/java/io/quarkiverse/googlecloudservices/logging/runtime/LoggingHandler.java b/logging/runtime/src/main/java/io/quarkiverse/googlecloudservices/logging/runtime/LoggingHandler.java index 40b70aaf..4344717b 100644 --- a/logging/runtime/src/main/java/io/quarkiverse/googlecloudservices/logging/runtime/LoggingHandler.java +++ b/logging/runtime/src/main/java/io/quarkiverse/googlecloudservices/logging/runtime/LoggingHandler.java @@ -59,10 +59,20 @@ public void doPublish(ExtLogRecord record) { TraceInfo trace = traceExtractor.extract(record); LogEntry logEntry = transform(record, trace); if (logEntry != null) { - l.write(ImmutableList.of(logEntry), defaultWriteOptions); + switch (config.logTarget) { + case STDOUT: + System.out.println(logEntry.toStructuredJsonString()); + break; + case STDERR: + System.err.println(logEntry.toStructuredJsonString()); + break; + case CLOUD_LOGGING: + l.write(ImmutableList.of(logEntry), defaultWriteOptions); + break; + } } } catch (Exception ex) { - getErrorManager().error("Failed to publish record to GCP", ex, ErrorManager.WRITE_FAILURE); + getErrorManager().error("Failed to write logs", ex, ErrorManager.WRITE_FAILURE); } } @@ -138,7 +148,7 @@ private void initDefaultWriteOptions() { private MonitoredResource createMonitoredResource() { MonitoredResource.Builder b = MonitoredResource.newBuilder(this.config.resource.type); if (this.config.resource.label != null) { - this.config.resource.label.forEach((k, v) -> b.addLabel(k, v)); + this.config.resource.label.forEach(b::addLabel); } return b.build(); } From c5ca0d46fb8d4b33767ed1c93adcb6d8e757ef47 Mon Sep 17 00:00:00 2001 From: zanmagerl Date: Mon, 19 Jun 2023 16:35:28 +0200 Subject: [PATCH 2/3] Change default log target to CLOUD_LOGGING --- .../logging/runtime/LoggingConfiguration.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/logging/runtime/src/main/java/io/quarkiverse/googlecloudservices/logging/runtime/LoggingConfiguration.java b/logging/runtime/src/main/java/io/quarkiverse/googlecloudservices/logging/runtime/LoggingConfiguration.java index 5601ebc1..931569b5 100644 --- a/logging/runtime/src/main/java/io/quarkiverse/googlecloudservices/logging/runtime/LoggingConfiguration.java +++ b/logging/runtime/src/main/java/io/quarkiverse/googlecloudservices/logging/runtime/LoggingConfiguration.java @@ -86,7 +86,7 @@ public class LoggingConfiguration { * Useful if app is deployed to managed Google Cloud Platform environment with installed logger agent. * Possible values: STDOUT, STDERR and CLOUD_LOGGING. */ - @ConfigItem(defaultValue = "STDOUT") + @ConfigItem(defaultValue = "CLOUD_LOGGING") public LogTarget logTarget; @ConfigGroup From 78bac84daf3e5375523cc97cffd3a00e0f07b520 Mon Sep 17 00:00:00 2001 From: zanmagerl Date: Wed, 28 Jun 2023 18:44:53 +0200 Subject: [PATCH 3/3] Remove console logging config --- .../logging/deployment/LoggingBuildSteps.java | 13 ------------- .../logging/runtime/LoggingConfiguration.java | 8 +------- 2 files changed, 1 insertion(+), 20 deletions(-) diff --git a/logging/deployment/src/main/java/io/quarkiverse/googlecloudservices/logging/deployment/LoggingBuildSteps.java b/logging/deployment/src/main/java/io/quarkiverse/googlecloudservices/logging/deployment/LoggingBuildSteps.java index 3e7ff8df..5d5cba34 100644 --- a/logging/deployment/src/main/java/io/quarkiverse/googlecloudservices/logging/deployment/LoggingBuildSteps.java +++ b/logging/deployment/src/main/java/io/quarkiverse/googlecloudservices/logging/deployment/LoggingBuildSteps.java @@ -11,12 +11,10 @@ import io.quarkus.deployment.annotations.Record; import io.quarkus.deployment.builditem.FeatureBuildItem; import io.quarkus.deployment.builditem.LogHandlerBuildItem; -import io.quarkus.deployment.builditem.RunTimeConfigurationDefaultBuildItem; public class LoggingBuildSteps { private static final String FEATURE = "google-cloud-logging"; - private static final String QUARKUS_CONSOLE_LOGGING_CONFIG_KEY = "quarkus.log.console.enable"; @BuildStep public FeatureBuildItem feature() { @@ -40,15 +38,4 @@ public UnremovableBeanBuildItem helperClasses() { public LogHandlerBuildItem handler(LoggingConfiguration config, LoggingHandlerFactory factory) { return new LogHandlerBuildItem(factory.create(config)); } - - @BuildStep - public RunTimeConfigurationDefaultBuildItem configurationDefaultBuildItem(LoggingConfiguration config) { - boolean enableConsoleLogging = true; - // We should use configuration only if the GCP logging extension is enabled - if (config.enabled) { - enableConsoleLogging = config.enableConsoleLogging; - } - return new RunTimeConfigurationDefaultBuildItem(QUARKUS_CONSOLE_LOGGING_CONFIG_KEY, - Boolean.toString(enableConsoleLogging)); - } } diff --git a/logging/runtime/src/main/java/io/quarkiverse/googlecloudservices/logging/runtime/LoggingConfiguration.java b/logging/runtime/src/main/java/io/quarkiverse/googlecloudservices/logging/runtime/LoggingConfiguration.java index 931569b5..aa9c8312 100644 --- a/logging/runtime/src/main/java/io/quarkiverse/googlecloudservices/logging/runtime/LoggingConfiguration.java +++ b/logging/runtime/src/main/java/io/quarkiverse/googlecloudservices/logging/runtime/LoggingConfiguration.java @@ -12,7 +12,7 @@ import io.quarkus.runtime.annotations.ConfigPhase; import io.quarkus.runtime.annotations.ConfigRoot; -@ConfigRoot(name = "google.cloud.logging", phase = ConfigPhase.BUILD_AND_RUN_TIME_FIXED) +@ConfigRoot(name = "google.cloud.logging", phase = ConfigPhase.RUN_TIME) public class LoggingConfiguration { /** @@ -27,12 +27,6 @@ public class LoggingConfiguration { @ConfigItem(defaultValue = "true") public boolean enabled; - /** - * Enable or disable default Quarkus console logging. - */ - @ConfigItem - public boolean enableConsoleLogging; - /** * Configure base formatting to be either plain text or * structured json. Allowed values: TEXT|JSON