3333import com .google .cloud .logging .MonitoredResourceUtil ;
3434import com .google .cloud .logging .Payload ;
3535import com .google .cloud .logging .Severity ;
36+ import com .google .cloud .logging .Synchronicity ;
3637import com .google .common .base .Strings ;
3738import com .google .common .collect .ImmutableList ;
3839import java .io .FileInputStream ;
6364 * <!-- Optional: defaults to "ERROR" -->
6465 * <flushLevel>WARNING</flushLevel>
6566 *
67+ * <!-- Optional: defaults to ASYNC -->
68+ * <writeSynchronicity>SYNC</writeSynchronicity>
69+ *
6670 * <!-- 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> -->
71+ * href=
72+ * "https://cloud.google.com/logging/docs/api/v2/resource-list">supported resource types</a> -->
6873 * <resourceType></resourceType>
6974 *
7075 * <!-- Optional: defaults to the default credentials of the environment -->
@@ -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