Skip to content

Commit af16e2a

Browse files
committed
Add async/sync setting to logging handler
1 parent 59459fc commit af16e2a

File tree

6 files changed

+120
-210
lines changed

6 files changed

+120
-210
lines changed

google-cloud-logging/src/main/java/com/google/cloud/logging/AsyncLoggingHandler.java

-101
This file was deleted.

google-cloud-logging/src/main/java/com/google/cloud/logging/LoggingHandler.java

+52-28
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@
1919
import static com.google.common.base.MoreObjects.firstNonNull;
2020

2121
import com.google.cloud.MonitoredResource;
22-
import com.google.cloud.logging.Logging.WriteOption;
2322
import com.google.common.collect.ImmutableList;
24-
2523
import java.util.ArrayList;
2624
import java.util.Collections;
2725
import java.util.LinkedList;
@@ -35,6 +33,7 @@
3533
import java.util.logging.LogRecord;
3634
import java.util.logging.Logger;
3735
import java.util.logging.SimpleFormatter;
36+
import com.google.cloud.logging.Logging.WriteOption;
3837

3938
/**
4039
* A logging handler that synchronously outputs logs generated with {@link java.util.logging.Logger}
@@ -54,40 +53,44 @@
5453
* <tr><td>FINEST</td><td>DEBUG</td></tr>
5554
* </table>
5655
*
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}.
6160
*
6261
* <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
6564
* specified default values are used.
65+
*
6666
* <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}).
8688
* </ul>
8789
*
8890
* <p>To add a {@code LoggingHandler} to an existing {@link Logger} and be sure to avoid infinite
8991
* recursion when logging, use the {@link #addHandler(Logger, LoggingHandler)} method. Alternatively
9092
* you can add the handler via {@code logging.properties}. For example using the following line:
93+
*
9194
* <pre>
9295
* {@code com.example.mypackage.handlers=com.google.cloud.logging.LoggingHandler}
9396
* </pre>
@@ -106,6 +109,7 @@ public class LoggingHandler extends Handler {
106109
private volatile Logging logging;
107110
private Level flushLevel;
108111
private long flushSize;
112+
private WriteLogMethod writeLogMethod;
109113
private final List<Enhancer> enhancers;
110114

111115
/**
@@ -169,6 +173,8 @@ public LoggingHandler(String log, LoggingOptions options, MonitoredResource moni
169173
this.options = options != null ? options : LoggingOptions.getDefaultInstance();
170174
this.flushLevel = helper.getLevelProperty(className + ".flushLevel", LoggingLevel.ERROR);
171175
this.flushSize = helper.getLongProperty(className + ".flushSize", 1L);
176+
this.writeLogMethod =
177+
helper.getWriteLogMethodProperty(className + ".writeLogMethod", WriteLogMethod.ASYNC);
172178
setLevel(helper.getLevelProperty(className + ".level", Level.INFO));
173179
setFilter(helper.getFilterProperty(className + ".filter", null));
174180
setFormatter(helper.getFormatterProperty(className + ".formatter", new SimpleFormatter()));
@@ -296,6 +302,16 @@ List<Enhancer> getEnhancerProperty(String name) {
296302
}
297303
return Collections.emptyList();
298304
}
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+
}
299315
}
300316

301317
/**
@@ -419,7 +435,15 @@ private static Severity severityFor(Level level) {
419435
* how entries should be written.
420436
*/
421437
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+
}
423447
}
424448

425449
@Override
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Copyright 2017 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.cloud.logging;
18+
19+
public enum WriteLogMethod {
20+
SYNC,
21+
ASYNC,
22+
}

google-cloud-logging/src/test/java/com/google/cloud/logging/AsyncLoggingHandlerTest.java

-78
This file was deleted.

google-cloud-logging/src/test/java/com/google/cloud/logging/BaseSystemTest.java

+12-3
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
import com.google.protobuf.Any;
4242
import com.google.protobuf.StringValue;
4343

44+
import java.io.ByteArrayInputStream;
45+
import java.util.logging.LogManager;
4446
import org.junit.Rule;
4547
import org.junit.Test;
4648
import org.junit.rules.ExpectedException;
@@ -497,14 +499,21 @@ public void testLoggingHandler() throws InterruptedException {
497499
}
498500

499501
@Test
500-
public void testAsyncLoggingHandler() throws InterruptedException {
501-
String logName = formatForTest("test-async-logging-handler");
502+
public void testSyncLoggingHandler() throws InterruptedException {
503+
String logName = formatForTest("test-sync-logging-handler");
504+
try {
505+
LogManager.getLogManager().readConfiguration(
506+
new ByteArrayInputStream(
507+
"com.google.cloud.logging.LoggingHandler.writeLogMethod=SYNC".getBytes()));
508+
} catch (Exception e) {
509+
throw new RuntimeException(e);
510+
}
502511
LoggingOptions options = logging().getOptions();
503512
MonitoredResource resource = MonitoredResource.of("gce_instance",
504513
ImmutableMap.of("project_id", options.getProjectId(),
505514
"instance_id", "instance",
506515
"zone", "us-central1-a"));
507-
LoggingHandler handler = new AsyncLoggingHandler(logName, options, resource);
516+
LoggingHandler handler = new LoggingHandler(logName, options, resource);
508517
handler.setLevel(Level.WARNING);
509518
Logger logger = Logger.getLogger(getClass().getName());
510519
logger.addHandler(handler);

0 commit comments

Comments
 (0)