From 65ab6f8dfbfe9f28538b905db58aadeae0182276 Mon Sep 17 00:00:00 2001 From: minherz Date: Fri, 17 Sep 2021 19:21:54 +0300 Subject: [PATCH] feat: add writeSynchronicity flag to appender configuration (#542) * feat: add support for write synchronicity config add new adapter configuration to allow setting writeSynchronicity * chore: set default value and remove debug output remove debug prints add option description into class javadoc setup default value of the instance * chore: remove async testing from samples * chore: change type of writeSynchronicity to enum --- .../logging/logback/LoggingAppender.java | 27 ++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/google/cloud/logging/logback/LoggingAppender.java b/src/main/java/com/google/cloud/logging/logback/LoggingAppender.java index 3c7b4411c..e1fb253aa 100644 --- a/src/main/java/com/google/cloud/logging/logback/LoggingAppender.java +++ b/src/main/java/com/google/cloud/logging/logback/LoggingAppender.java @@ -33,6 +33,7 @@ import com.google.cloud.logging.MonitoredResourceUtil; import com.google.cloud.logging.Payload; import com.google.cloud.logging.Severity; +import com.google.cloud.logging.Synchronicity; import com.google.common.base.Strings; import com.google.common.collect.ImmutableList; import java.io.FileInputStream; @@ -63,8 +64,12 @@ * <!-- Optional: defaults to "ERROR" --> * <flushLevel>WARNING</flushLevel> * + * <!-- Optional: defaults to ASYNC --> + * <writeSynchronicity>SYNC</writeSynchronicity> + * * <!-- Optional: auto detects on App Engine Flex, Standard, GCE and GKE, defaults to "global". See supported resource types --> + * href= + * "https://cloud.google.com/logging/docs/api/v2/resource-list">supported resource types --> * <resourceType></resourceType> * * <!-- Optional: defaults to the default credentials of the environment --> @@ -96,6 +101,7 @@ public class LoggingAppender extends UnsynchronizedAppenderBase { private String log; private String resourceType; private String credentialsFile; + private Synchronicity writeSyncFlag = Synchronicity.ASYNC; private final Set enhancerClassNames = new HashSet<>(); private final Set loggingEventEnhancerClassNames = new HashSet<>(); @@ -122,8 +128,9 @@ public void setLog(String log) { /** * Sets the name of the monitored resource (Optional). * - *

Must be a supported - * resource type. gae_app, gce_instance and container are auto-detected. + *

Must be a supported resource type. + * gae_app, gce_instance and container are auto-detected. * *

Defaults to "global" * @@ -144,6 +151,15 @@ public void setCredentialsFile(String credentialsFile) { this.credentialsFile = credentialsFile; } + /** + * Define synchronization mode for writing log entries. + * + * @param flag to set {@code Synchronicity} value. + */ + public void setWriteSynchronicity(Synchronicity flag) { + this.writeSyncFlag = flag; + } + /** Add extra labels using classes that implement {@link LoggingEnhancer}. */ public void addEnhancer(String enhancerClassName) { this.enhancerClassNames.add(enhancerClassName); @@ -161,6 +177,10 @@ String getLogName() { return (log != null) ? log : "java.log"; } + public Synchronicity getWriteSynchronicity() { + return (this.writeSyncFlag != null) ? this.writeSyncFlag : Synchronicity.ASYNC; + } + MonitoredResource getMonitoredResource(String projectId) { return MonitoredResourceUtil.getResource(projectId, resourceType); } @@ -253,6 +273,7 @@ Logging getLogging() { synchronized (this) { if (logging == null) { logging = getLoggingOptions().getService(); + logging.setWriteSynchronicity(writeSyncFlag); } } }