Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add writeSynchronicity flag to appender configuration #542

Merged
merged 4 commits into from Sep 17, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -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;
Expand Down Expand Up @@ -63,8 +64,12 @@
* <!-- Optional: defaults to "ERROR" -->
* <flushLevel>WARNING</flushLevel>
*
* <!-- Optional: defaults to ASYNC -->
* <writeSynchronicity>SYNC</writeSynchronicity>
*
* &lt;!-- Optional: auto detects on App Engine Flex, Standard, GCE and GKE, defaults to "global". See <a
* href="https://cloud.google.com/logging/docs/api/v2/resource-list">supported resource types</a> --&gt;
* href=
* "https://cloud.google.com/logging/docs/api/v2/resource-list">supported resource types</a> --&gt;
* &lt;resourceType&gt;&lt;/resourceType&gt;
*
* &lt;!-- Optional: defaults to the default credentials of the environment --&gt;
Expand Down Expand Up @@ -96,6 +101,7 @@ public class LoggingAppender extends UnsynchronizedAppenderBase<ILoggingEvent> {
private String log;
private String resourceType;
private String credentialsFile;
private Synchronicity writeSyncFlag = Synchronicity.ASYNC;
private final Set<String> enhancerClassNames = new HashSet<>();
private final Set<String> loggingEventEnhancerClassNames = new HashSet<>();

Expand All @@ -122,8 +128,9 @@ public void setLog(String log) {
/**
* Sets the name of the monitored resource (Optional).
*
* <p>Must be a <a href="https://cloud.google.com/logging/docs/api/v2/resource-list">supported</a>
* resource type. gae_app, gce_instance and container are auto-detected.
* <p>Must be a <a href=
* "https://cloud.google.com/logging/docs/api/v2/resource-list">supported</a> resource type.
* gae_app, gce_instance and container are auto-detected.
*
* <p>Defaults to "global"
*
Expand All @@ -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);
Expand All @@ -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);
}
Expand Down Expand Up @@ -253,6 +273,7 @@ Logging getLogging() {
synchronized (this) {
if (logging == null) {
logging = getLoggingOptions().getService();
logging.setWriteSynchronicity(writeSyncFlag);
}
}
}
Expand Down