2121import com .google .cloud .MonitoredResource ;
2222import com .google .cloud .logging .Logging .WriteOption ;
2323import com .google .common .collect .ImmutableList ;
24-
2524import java .util .ArrayList ;
2625import java .util .Collections ;
2726import java .util .LinkedList ;
8382 * a {@link MonitoredResource} or {@link LogEntry} instance (defaults to empty list).
8483 * <li>{@code com.google.cloud.logging.LoggingHandler.resourceType} the type name to use when
8584 * creating the default {@link MonitoredResource} (defaults to "global").
85+ * <li>{@code com.google.cloud.logging.Synchronicity} the synchronicity of the write method to use
86+ * to write logs to the Stackdriver Logging service (defaults to {@link Synchronicity#ASYNC}).
8687 * </ul>
8788 *
8889 * <p>To add a {@code LoggingHandler} to an existing {@link Logger} and be sure to avoid infinite
@@ -106,6 +107,7 @@ public class LoggingHandler extends Handler {
106107 private volatile Logging logging ;
107108 private Level flushLevel ;
108109 private long flushSize ;
110+ private Synchronicity synchronicity ;
109111 private final List <Enhancer > enhancers ;
110112
111113 /**
@@ -169,6 +171,8 @@ public LoggingHandler(String log, LoggingOptions options, MonitoredResource moni
169171 this .options = options != null ? options : LoggingOptions .getDefaultInstance ();
170172 this .flushLevel = helper .getLevelProperty (className + ".flushLevel" , LoggingLevel .ERROR );
171173 this .flushSize = helper .getLongProperty (className + ".flushSize" , 1L );
174+ this .synchronicity =
175+ helper .getSynchronicityProperty (className + ".synchronicity" , Synchronicity .ASYNC );
172176 setLevel (helper .getLevelProperty (className + ".level" , Level .INFO ));
173177 setFilter (helper .getFilterProperty (className + ".filter" , null ));
174178 setFormatter (helper .getFormatterProperty (className + ".formatter" , new SimpleFormatter ()));
@@ -296,6 +300,16 @@ List<Enhancer> getEnhancerProperty(String name) {
296300 }
297301 return Collections .emptyList ();
298302 }
303+
304+ Synchronicity getSynchronicityProperty (String name , Synchronicity defaultValue ) {
305+ String synchronicity = manager .getProperty (name );
306+ try {
307+ return Synchronicity .valueOf (synchronicity );
308+ } catch (Exception ex ) {
309+ // If we cannot create the Synchronicity we fall back to default value
310+ }
311+ return defaultValue ;
312+ }
299313 }
300314
301315 /**
@@ -419,7 +433,15 @@ private static Severity severityFor(Level level) {
419433 * how entries should be written.
420434 */
421435 void write (List <LogEntry > entries , WriteOption ... options ) {
422- getLogging ().writeAsync (entries , options );
436+ switch (this .synchronicity ) {
437+ case SYNC :
438+ getLogging ().write (entries , options );
439+ break ;
440+ case ASYNC :
441+ default :
442+ getLogging ().writeAsync (entries , options );
443+ break ;
444+ }
423445 }
424446
425447 @ Override
@@ -476,6 +498,11 @@ public synchronized Level setFlushLevel(Level flushLevel) {
476498 return flushLevel ;
477499 }
478500
501+ /** Get the flush log level. */
502+ public Level getFlushLevel () {
503+ return this .flushLevel ;
504+ }
505+
479506 /**
480507 * Sets the maximum size of the log buffer. Once the maximum size of the buffer is reached, logs
481508 * are transmitted to the Stackdriver Logging service. If not set, a log is sent to the service as
@@ -486,6 +513,28 @@ public synchronized long setFlushSize(long flushSize) {
486513 return flushSize ;
487514 }
488515
516+ /** Get the maximum size of the log buffer. */
517+ public long getFlushSize () {
518+ return this .flushSize ;
519+ }
520+
521+ /**
522+ * Sets the synchronicity of the write method used to write logs to the Stackdriver Logging
523+ * service. Defaults to {@link Synchronicity#ASYNC}.
524+ */
525+ public synchronized Synchronicity setSynchronicity (Synchronicity synchronicity ) {
526+ this .synchronicity = synchronicity ;
527+ return synchronicity ;
528+ }
529+
530+ /**
531+ * Get the synchronicity of the write method used to write logs to the Stackdriver Logging
532+ * service.
533+ */
534+ public Synchronicity getSynchronicity () {
535+ return this .synchronicity ;
536+ }
537+
489538 /**
490539 * Adds the provided {@code LoggingHandler} to {@code logger}. Use this method to register Cloud
491540 * Logging handlers instead of {@link Logger#addHandler(Handler)} to avoid infinite recursion
0 commit comments