Skip to content

Commit 65ab6f8

Browse files
authored
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
1 parent 288a368 commit 65ab6f8

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

src/main/java/com/google/cloud/logging/logback/LoggingAppender.java

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import com.google.cloud.logging.MonitoredResourceUtil;
3434
import com.google.cloud.logging.Payload;
3535
import com.google.cloud.logging.Severity;
36+
import com.google.cloud.logging.Synchronicity;
3637
import com.google.common.base.Strings;
3738
import com.google.common.collect.ImmutableList;
3839
import java.io.FileInputStream;
@@ -63,8 +64,12 @@
6364
* <!-- Optional: defaults to "ERROR" -->
6465
* <flushLevel>WARNING</flushLevel>
6566
*
67+
* <!-- Optional: defaults to ASYNC -->
68+
* <writeSynchronicity>SYNC</writeSynchronicity>
69+
*
6670
* &lt;!-- Optional: auto detects on App Engine Flex, Standard, GCE and GKE, defaults to "global". See <a
67-
* href="https://cloud.google.com/logging/docs/api/v2/resource-list">supported resource types</a> --&gt;
71+
* href=
72+
* "https://cloud.google.com/logging/docs/api/v2/resource-list">supported resource types</a> --&gt;
6873
* &lt;resourceType&gt;&lt;/resourceType&gt;
6974
*
7075
* &lt;!-- Optional: defaults to the default credentials of the environment --&gt;
@@ -96,6 +101,7 @@ public class LoggingAppender extends UnsynchronizedAppenderBase<ILoggingEvent> {
96101
private String log;
97102
private String resourceType;
98103
private String credentialsFile;
104+
private Synchronicity writeSyncFlag = Synchronicity.ASYNC;
99105
private final Set<String> enhancerClassNames = new HashSet<>();
100106
private final Set<String> loggingEventEnhancerClassNames = new HashSet<>();
101107

@@ -122,8 +128,9 @@ public void setLog(String log) {
122128
/**
123129
* Sets the name of the monitored resource (Optional).
124130
*
125-
* <p>Must be a <a href="https://cloud.google.com/logging/docs/api/v2/resource-list">supported</a>
126-
* resource type. gae_app, gce_instance and container are auto-detected.
131+
* <p>Must be a <a href=
132+
* "https://cloud.google.com/logging/docs/api/v2/resource-list">supported</a> resource type.
133+
* gae_app, gce_instance and container are auto-detected.
127134
*
128135
* <p>Defaults to "global"
129136
*
@@ -144,6 +151,15 @@ public void setCredentialsFile(String credentialsFile) {
144151
this.credentialsFile = credentialsFile;
145152
}
146153

154+
/**
155+
* Define synchronization mode for writing log entries.
156+
*
157+
* @param flag to set {@code Synchronicity} value.
158+
*/
159+
public void setWriteSynchronicity(Synchronicity flag) {
160+
this.writeSyncFlag = flag;
161+
}
162+
147163
/** Add extra labels using classes that implement {@link LoggingEnhancer}. */
148164
public void addEnhancer(String enhancerClassName) {
149165
this.enhancerClassNames.add(enhancerClassName);
@@ -161,6 +177,10 @@ String getLogName() {
161177
return (log != null) ? log : "java.log";
162178
}
163179

180+
public Synchronicity getWriteSynchronicity() {
181+
return (this.writeSyncFlag != null) ? this.writeSyncFlag : Synchronicity.ASYNC;
182+
}
183+
164184
MonitoredResource getMonitoredResource(String projectId) {
165185
return MonitoredResourceUtil.getResource(projectId, resourceType);
166186
}
@@ -253,6 +273,7 @@ Logging getLogging() {
253273
synchronized (this) {
254274
if (logging == null) {
255275
logging = getLoggingOptions().getService();
276+
logging.setWriteSynchronicity(writeSyncFlag);
256277
}
257278
}
258279
}

0 commit comments

Comments
 (0)