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..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 @@ -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; @@ -74,6 +75,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 = "CLOUD_LOGGING") + public LogTarget logTarget; + @ConfigGroup public static class StructuredConfig { @@ -198,4 +207,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(); }