From df573a01d273b706fc4e355296ccaa5bbe0e35d7 Mon Sep 17 00:00:00 2001 From: minherz Date: Thu, 16 Sep 2021 14:20:07 +0000 Subject: [PATCH 1/4] feat: add support for write synchronicity config add new adapter configuration to allow setting writeSynchronicity --- .../example/logging/logback/UsingContext.java | 40 +++++++++++++++++++ .../logging/logback/LoggingAppender.java | 32 +++++++++++++-- 2 files changed, 69 insertions(+), 3 deletions(-) create mode 100644 samples/snippets/src/main/java/com/example/logging/logback/UsingContext.java diff --git a/samples/snippets/src/main/java/com/example/logging/logback/UsingContext.java b/samples/snippets/src/main/java/com/example/logging/logback/UsingContext.java new file mode 100644 index 000000000..c1f440d04 --- /dev/null +++ b/samples/snippets/src/main/java/com/example/logging/logback/UsingContext.java @@ -0,0 +1,40 @@ +/* + * Copyright 2021 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.logging.logback; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import java.io.IOException; +import io.grpc.Context.CancellableContext; + +public class UsingContext { + private static final Logger logger = LoggerFactory.getLogger(UsingContext.class.getName()); + + private static void runCancelled(Runnable runnable) { + CancellableContext withCancellation = io.grpc.Context.current().withCancellation(); + withCancellation.run(() -> { + withCancellation.cancel(new RuntimeException("Test cancel")); + runnable.run(); + }); + } + + public static void main(String[] args) throws IOException { + runCancelled(() -> { + logger.info("Please send me"); + }); + } +} \ No newline at end of file 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..85ba1cc3a 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; @@ -64,7 +65,8 @@ * <flushLevel>WARNING</flushLevel> * * <!-- 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 +98,7 @@ public class LoggingAppender extends UnsynchronizedAppenderBase { private String log; private String resourceType; private String credentialsFile; + private Synchronicity writeSyncFlag; private final Set enhancerClassNames = new HashSet<>(); private final Set loggingEventEnhancerClassNames = new HashSet<>(); @@ -122,8 +125,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 +148,20 @@ public void setCredentialsFile(String credentialsFile) { this.credentialsFile = credentialsFile; } + /** + * Define synchronization mode for writing log entries. + * + * @param log flag + */ + public void setWriteSynchronicity(String flag) { + System.out.println("##### Captured writeSynchronicity config = " + flag); + try { + this.writeSyncFlag = Synchronicity.valueOf(flag); + } catch (IllegalArgumentException e) { + this.writeSyncFlag = Synchronicity.ASYNC; // use default value + } + } + /** Add extra labels using classes that implement {@link LoggingEnhancer}. */ public void addEnhancer(String enhancerClassName) { this.enhancerClassNames.add(enhancerClassName); @@ -161,6 +179,12 @@ String getLogName() { return (log != null) ? log : "java.log"; } + public String getWriteSynchronicity() { + return (this.writeSyncFlag != null) + ? this.writeSyncFlag.toString() + : Synchronicity.ASYNC.toString(); + } + MonitoredResource getMonitoredResource(String projectId) { return MonitoredResourceUtil.getResource(projectId, resourceType); } @@ -253,6 +277,8 @@ Logging getLogging() { synchronized (this) { if (logging == null) { logging = getLoggingOptions().getService(); + logging.setWriteSynchronicity(writeSyncFlag); + System.out.println("####### Setting write synchronicity for logging to " + writeSyncFlag); } } } From b8983522d779f4137c3b63ef13f8ac7fd852c61b Mon Sep 17 00:00:00 2001 From: minherz Date: Thu, 16 Sep 2021 15:46:08 +0000 Subject: [PATCH 2/4] chore: set default value and remove debug output remove debug prints add option description into class javadoc setup default value of the instance --- .../com/google/cloud/logging/logback/LoggingAppender.java | 7 ++++--- 1 file changed, 4 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 85ba1cc3a..d14e477a8 100644 --- a/src/main/java/com/google/cloud/logging/logback/LoggingAppender.java +++ b/src/main/java/com/google/cloud/logging/logback/LoggingAppender.java @@ -64,6 +64,9 @@ * <!-- 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 --> @@ -98,7 +101,7 @@ public class LoggingAppender extends UnsynchronizedAppenderBase { private String log; private String resourceType; private String credentialsFile; - private Synchronicity writeSyncFlag; + private Synchronicity writeSyncFlag = Synchronicity.ASYNC; private final Set enhancerClassNames = new HashSet<>(); private final Set loggingEventEnhancerClassNames = new HashSet<>(); @@ -154,7 +157,6 @@ public void setCredentialsFile(String credentialsFile) { * @param log flag */ public void setWriteSynchronicity(String flag) { - System.out.println("##### Captured writeSynchronicity config = " + flag); try { this.writeSyncFlag = Synchronicity.valueOf(flag); } catch (IllegalArgumentException e) { @@ -278,7 +280,6 @@ Logging getLogging() { if (logging == null) { logging = getLoggingOptions().getService(); logging.setWriteSynchronicity(writeSyncFlag); - System.out.println("####### Setting write synchronicity for logging to " + writeSyncFlag); } } } From 24f4bf4d533c300df1c80368ea7f40341fafd06e Mon Sep 17 00:00:00 2001 From: minherz Date: Thu, 16 Sep 2021 15:48:44 +0000 Subject: [PATCH 3/4] chore: remove async testing from samples --- .../example/logging/logback/UsingContext.java | 40 ------------------- 1 file changed, 40 deletions(-) delete mode 100644 samples/snippets/src/main/java/com/example/logging/logback/UsingContext.java diff --git a/samples/snippets/src/main/java/com/example/logging/logback/UsingContext.java b/samples/snippets/src/main/java/com/example/logging/logback/UsingContext.java deleted file mode 100644 index c1f440d04..000000000 --- a/samples/snippets/src/main/java/com/example/logging/logback/UsingContext.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright 2021 Google LLC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.example.logging.logback; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import java.io.IOException; -import io.grpc.Context.CancellableContext; - -public class UsingContext { - private static final Logger logger = LoggerFactory.getLogger(UsingContext.class.getName()); - - private static void runCancelled(Runnable runnable) { - CancellableContext withCancellation = io.grpc.Context.current().withCancellation(); - withCancellation.run(() -> { - withCancellation.cancel(new RuntimeException("Test cancel")); - runnable.run(); - }); - } - - public static void main(String[] args) throws IOException { - runCancelled(() -> { - logger.info("Please send me"); - }); - } -} \ No newline at end of file From 9b6e58892093e0fbcb1e323e7563ecdb083f7847 Mon Sep 17 00:00:00 2001 From: minherz Date: Thu, 16 Sep 2021 18:31:41 +0000 Subject: [PATCH 4/4] chore: change type of writeSynchronicity to enum --- .../cloud/logging/logback/LoggingAppender.java | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 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 d14e477a8..e1fb253aa 100644 --- a/src/main/java/com/google/cloud/logging/logback/LoggingAppender.java +++ b/src/main/java/com/google/cloud/logging/logback/LoggingAppender.java @@ -64,7 +64,7 @@ * <!-- Optional: defaults to "ERROR" --> * <flushLevel>WARNING</flushLevel> * - * <!-- Optional: defaults to "ASYNC" --> + * <!-- Optional: defaults to ASYNC --> * <writeSynchronicity>SYNC</writeSynchronicity> * * <!-- Optional: auto detects on App Engine Flex, Standard, GCE and GKE, defaults to "global". See