1717package com .google .cloud .logging ;
1818
1919import static com .google .common .base .MoreObjects .firstNonNull ;
20+ import static com .google .common .base .Preconditions .checkNotNull ;
2021
2122import com .google .cloud .MonitoredResource ;
2223import com .google .cloud .logging .Logging .WriteOption ;
2526import com .google .common .collect .Iterables ;
2627import java .time .Instant ;
2728import java .util .ArrayList ;
29+ import java .util .Arrays ;
2830import java .util .Collections ;
2931import java .util .LinkedList ;
3032import java .util .List ;
109111 * else "global").
110112 * <li>{@code com.google.cloud.logging.Synchronicity} the synchronicity of the write method to use
111113 * to write logs to the Cloud Logging service (defaults to {@link Synchronicity#ASYNC}).
114+ * <li>{@code com.google.cloud.logging.LoggingHandler.autoPopulateMetadata} is a boolean flag that
115+ * opts-out the population of the log entries metadata before the logs are sent to Cloud
116+ * Logging (defaults to {@code true}).
112117 * </ul>
113118 *
114119 * <p>To add a {@code LoggingHandler} to an existing {@link Logger} and be sure to avoid infinite
@@ -139,6 +144,8 @@ public class LoggingHandler extends Handler {
139144
140145 private volatile Level flushLevel ;
141146
147+ private volatile Boolean autoPopulateMetadata ;
148+
142149 private WriteOption [] defaultWriteOptions ;
143150
144151 /** Creates an handler that publishes messages to Cloud Logging. */
@@ -196,7 +203,10 @@ public LoggingHandler(
196203 }
197204
198205 /**
199- * Creates a handler that publishes messages to Cloud Logging.
206+ * Creates a handler that publishes messages to Cloud Logging. Auto-population of the logs
207+ * metadata can be opted-out in {@code options} argument or in the configuration file. At least
208+ * one flag {@link LoggingOptions} or {@link LoggingConfig} has to be explicitly set to {@code
209+ * false} in order to opt-out the metadata auto-population.
200210 *
201211 * @param log the name of the log to which log entries are written
202212 * @param options options for the Cloud Logging service
@@ -222,14 +232,18 @@ public LoggingHandler(
222232 setLevel (level );
223233 baseLevel = level .equals (Level .ALL ) ? Level .FINEST : level ;
224234 flushLevel = config .getFlushLevel ();
235+ Boolean f1 = options .getAutoPopulateMetadata ();
236+ Boolean f2 = config .getAutoPopulateMetadata ();
237+ autoPopulateMetadata = isTrueOrNull (f1 ) && isTrueOrNull (f2 );
225238 String logName = log != null ? log : config .getLogName ();
226-
227239 MonitoredResource resource =
228240 firstNonNull (
229241 monitoredResource , config .getMonitoredResource (loggingOptions .getProjectId ()));
230242 List <WriteOption > writeOptions = new ArrayList <WriteOption >();
231243 writeOptions .add (WriteOption .logName (logName ));
232- writeOptions .add (WriteOption .resource (resource ));
244+ if (resource != null ) {
245+ writeOptions .add (WriteOption .resource (resource ));
246+ }
233247 writeOptions .add (
234248 WriteOption .labels (
235249 ImmutableMap .of (
@@ -240,6 +254,7 @@ public LoggingHandler(
240254 if (destination != null ) {
241255 writeOptions .add (WriteOption .destination (destination ));
242256 }
257+ writeOptions .add (WriteOption .autoPopulateMetadata (autoPopulateMetadata ));
243258 defaultWriteOptions = Iterables .toArray (writeOptions , WriteOption .class );
244259
245260 getLogging ().setFlushSeverity (severityFor (flushLevel ));
@@ -365,6 +380,28 @@ public Synchronicity getSynchronicity() {
365380 return getLogging ().getWriteSynchronicity ();
366381 }
367382
383+ /**
384+ * Sets the metadata auto population flag.
385+ */
386+ public void setAutoPopulateMetadata (Boolean value ) {
387+ checkNotNull (value );
388+ this .autoPopulateMetadata = value ;
389+ List <WriteOption > writeOptions = Arrays .asList (defaultWriteOptions );
390+ for (int i = 0 ; i < writeOptions .size (); i ++) {
391+ if (writeOptions .get (i ).getOptionType () == WriteOption .OptionType .AUTO_POPULATE_METADATA ) {
392+ writeOptions .remove (i );
393+ break ;
394+ }
395+ }
396+ writeOptions .add (WriteOption .autoPopulateMetadata (value ));
397+ defaultWriteOptions = Iterables .toArray (writeOptions , WriteOption .class );
398+ }
399+
400+ /** Gets the metadata auto population flag. */
401+ public Boolean getAutoPopulateMetadata () {
402+ return this .autoPopulateMetadata ;
403+ }
404+
368405 /**
369406 * Adds the provided {@code LoggingHandler} to {@code logger}. Use this method to register Cloud
370407 * Logging handlers instead of {@link Logger#addHandler(Handler)} to avoid infinite recursion when
@@ -417,4 +454,8 @@ private Logging getLogging() {
417454 }
418455 return logging ;
419456 }
457+
458+ private static boolean isTrueOrNull (Boolean b ) {
459+ return b == null || b == Boolean .TRUE ;
460+ }
420461}
0 commit comments