19
19
import static com .google .common .base .MoreObjects .firstNonNull ;
20
20
21
21
import com .google .cloud .MonitoredResource ;
22
- import com .google .cloud .logging .Logging .WriteOption ;
23
22
import com .google .common .collect .ImmutableList ;
24
-
25
23
import java .util .ArrayList ;
26
24
import java .util .Collections ;
27
25
import java .util .LinkedList ;
35
33
import java .util .logging .LogRecord ;
36
34
import java .util .logging .Logger ;
37
35
import java .util .logging .SimpleFormatter ;
36
+ import com .google .cloud .logging .Logging .WriteOption ;
38
37
39
38
/**
40
39
* A logging handler that synchronously outputs logs generated with {@link java.util.logging.Logger}
54
53
* <tr><td>FINEST</td><td>DEBUG</td></tr>
55
54
* </table>
56
55
*
57
- * <p>Original Java logging levels are added as labels (with {@code levelName} and
58
- * {@code levelValue} keys, respectively) to the corresponding Stackdriver Logging {@link LogEntry}.
59
- * You can read entry labels using {@link LogEntry#getLabels()}. To use logging levels that
60
- * correspond to Stackdriver Logging severities you can use {@link LoggingLevel}.
56
+ * <p>Original Java logging levels are added as labels (with {@code levelName} and {@code
57
+ * levelValue} keys, respectively) to the corresponding Stackdriver Logging {@link LogEntry}. You
58
+ * can read entry labels using {@link LogEntry#getLabels()}. To use logging levels that correspond
59
+ * to Stackdriver Logging severities you can use {@link LoggingLevel}.
61
60
*
62
61
* <p><b>Configuration</b>: By default each {@code LoggingHandler} is initialized using the
63
- * following {@code LogManager} configuration properties (that you can set in the
64
- * {@code logging.properties} file). If properties are not defined (or have invalid values) then the
62
+ * following {@code LogManager} configuration properties (that you can set in the {@code
63
+ * logging.properties} file). If properties are not defined (or have invalid values) then the
65
64
* specified default values are used.
65
+ *
66
66
* <ul>
67
- * <li>{@code com.google.cloud.logging.LoggingHandler.log} the log name (defaults to
68
- * {@code java.log}).
69
- * <li>{@code com.google.cloud.logging.LoggingHandler.level} specifies the default level for the
70
- * handler (defaults to {@code Level.INFO}).
71
- * <li>{@code com.google.cloud.logging.LoggingHandler.filter} specifies the name of a {@link Filter}
72
- * class to use (defaults to no filter).
73
- * <li>{@code com.google.cloud.logging.LoggingHandler.formatter} specifies the name of a
74
- * {@link Formatter} class to use (defaults to {@link SimpleFormatter}).
75
- * <li>{@code com.google.cloud.logging.LoggingHandler.flushSize} specifies the maximum size of the
76
- * log buffer. Once reached, logs are transmitted to the Stackdriver Logging service (defaults
77
- * to 1).
78
- * <li>{@code com.google.cloud.logging.LoggingHandler.flushLevel} specifies the flush log level.
79
- * When a log with this level is published, logs are transmitted to the Stackdriver Logging
80
- * service (defaults to {@link LoggingLevel#ERROR}).
81
- * <li>{@code com.google.cloud.logging.LoggingHandler.enhancers} specifies a comma separated list
82
- * of {@link Enhancer} classes. This handler will call each enhancer list whenever it builds
83
- * a {@link MonitoredResource} or {@link LogEntry} instance (defaults to empty list).
84
- * <li>{@code com.google.cloud.logging.LoggingHandler.resourceType} the type name to use when
85
- * creating the default {@link MonitoredResource} (defaults to "global").
67
+ * <li>{@code com.google.cloud.logging.LoggingHandler.log} the log name (defaults to {@code
68
+ * java.log}).
69
+ * <li>{@code com.google.cloud.logging.LoggingHandler.level} specifies the default level for the
70
+ * handler (defaults to {@code Level.INFO}).
71
+ * <li>{@code com.google.cloud.logging.LoggingHandler.filter} specifies the name of a {@link
72
+ * Filter} class to use (defaults to no filter).
73
+ * <li>{@code com.google.cloud.logging.LoggingHandler.formatter} specifies the name of a {@link
74
+ * Formatter} class to use (defaults to {@link SimpleFormatter}).
75
+ * <li>{@code com.google.cloud.logging.LoggingHandler.flushSize} specifies the maximum size of the
76
+ * log buffer. Once reached, logs are transmitted to the Stackdriver Logging service (defaults
77
+ * to 1).
78
+ * <li>{@code com.google.cloud.logging.LoggingHandler.flushLevel} specifies the flush log level.
79
+ * When a log with this level is published, logs are transmitted to the Stackdriver Logging
80
+ * service (defaults to {@link LoggingLevel#ERROR}).
81
+ * <li>{@code com.google.cloud.logging.LoggingHandler.enhancers} specifies a comma separated list
82
+ * of {@link Enhancer} classes. This handler will call each enhancer list whenever it builds a
83
+ * {@link MonitoredResource} or {@link LogEntry} instance (defaults to empty list).
84
+ * <li>{@code com.google.cloud.logging.LoggingHandler.resourceType} the type name to use when
85
+ * creating the default {@link MonitoredResource} (defaults to "global").
86
+ * <li>{@code com.google.cloud.logging.WriteLogMethod} the type of write method to use to write
87
+ * logs to the Stackdriver Logging service (defaults to {@link WriteLogMethod#ASYNC}).
86
88
* </ul>
87
89
*
88
90
* <p>To add a {@code LoggingHandler} to an existing {@link Logger} and be sure to avoid infinite
89
91
* recursion when logging, use the {@link #addHandler(Logger, LoggingHandler)} method. Alternatively
90
92
* you can add the handler via {@code logging.properties}. For example using the following line:
93
+ *
91
94
* <pre>
92
95
* {@code com.example.mypackage.handlers=com.google.cloud.logging.LoggingHandler}
93
96
* </pre>
@@ -106,6 +109,7 @@ public class LoggingHandler extends Handler {
106
109
private volatile Logging logging ;
107
110
private Level flushLevel ;
108
111
private long flushSize ;
112
+ private WriteLogMethod writeLogMethod ;
109
113
private final List <Enhancer > enhancers ;
110
114
111
115
/**
@@ -169,6 +173,8 @@ public LoggingHandler(String log, LoggingOptions options, MonitoredResource moni
169
173
this .options = options != null ? options : LoggingOptions .getDefaultInstance ();
170
174
this .flushLevel = helper .getLevelProperty (className + ".flushLevel" , LoggingLevel .ERROR );
171
175
this .flushSize = helper .getLongProperty (className + ".flushSize" , 1L );
176
+ this .writeLogMethod =
177
+ helper .getWriteLogMethodProperty (className + ".writeLogMethod" , WriteLogMethod .ASYNC );
172
178
setLevel (helper .getLevelProperty (className + ".level" , Level .INFO ));
173
179
setFilter (helper .getFilterProperty (className + ".filter" , null ));
174
180
setFormatter (helper .getFormatterProperty (className + ".formatter" , new SimpleFormatter ()));
@@ -296,6 +302,16 @@ List<Enhancer> getEnhancerProperty(String name) {
296
302
}
297
303
return Collections .emptyList ();
298
304
}
305
+
306
+ WriteLogMethod getWriteLogMethodProperty (String name , WriteLogMethod defaultValue ) {
307
+ String writeLogMethod = manager .getProperty (name );
308
+ try {
309
+ return WriteLogMethod .valueOf (writeLogMethod );
310
+ } catch (Exception ex ) {
311
+ // If we cannot create the WriteLogMethod we fall back to default value
312
+ }
313
+ return defaultValue ;
314
+ }
299
315
}
300
316
301
317
/**
@@ -419,7 +435,15 @@ private static Severity severityFor(Level level) {
419
435
* how entries should be written.
420
436
*/
421
437
void write (List <LogEntry > entries , WriteOption ... options ) {
422
- getLogging ().writeAsync (entries , options );
438
+ switch (this .writeLogMethod ) {
439
+ case SYNC :
440
+ getLogging ().write (entries , options );
441
+ break ;
442
+ case ASYNC :
443
+ default :
444
+ getLogging ().writeAsync (entries , options );
445
+ break ;
446
+ }
423
447
}
424
448
425
449
@ Override
0 commit comments