Skip to content

Commit 0fb4c8b

Browse files
committed
---
yaml --- r: 5435 b: refs/heads/master c: ff24b05 h: refs/heads/master i: 5433: 112c82e 5431: 966dd4e
1 parent 413616d commit 0fb4c8b

7 files changed

Lines changed: 205 additions & 192 deletions

File tree

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
refs/heads/master: 9322afe8bca333a8178d5d50ff5674d9ac96605c
2+
refs/heads/master: ff24b054e3f72e48c54882783f347eacd2abffef
33
refs/heads/travis: dae77e558b884bc1b165155482d76c8e40b0fca4
44
refs/heads/gh-pages: 229631582f8957646f81e92ae5a326504f48ee5b
55
refs/tags/0.0.9: 22f1839238f66c39e67ed4dfdcd273b1ae2e8444

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

Lines changed: 0 additions & 101 deletions
This file was deleted.

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

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import com.google.cloud.MonitoredResource;
2222
import com.google.cloud.logging.Logging.WriteOption;
2323
import com.google.common.collect.ImmutableList;
24-
2524
import java.util.ArrayList;
2625
import java.util.Collections;
2726
import java.util.LinkedList;
@@ -83,6 +82,8 @@
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
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
/**
20+
* Used to specify the behavior of write calls to the Stackdriver Logging service. Specifying SYNC
21+
* will make synchronous calls; specifying ASYNC will make asynchronous calls. The default behavior
22+
* is ASYNC.
23+
*/
24+
public enum Synchronicity {
25+
SYNC,
26+
ASYNC,
27+
}

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

Lines changed: 0 additions & 78 deletions
This file was deleted.

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

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,15 @@
4040
import com.google.common.collect.Sets;
4141
import com.google.protobuf.Any;
4242
import com.google.protobuf.StringValue;
43-
44-
import org.junit.Rule;
45-
import org.junit.Test;
46-
import org.junit.rules.ExpectedException;
47-
import org.junit.rules.Timeout;
48-
4943
import java.util.Iterator;
5044
import java.util.Set;
5145
import java.util.concurrent.ExecutionException;
5246
import java.util.logging.Level;
5347
import java.util.logging.Logger;
48+
import org.junit.Rule;
49+
import org.junit.Test;
50+
import org.junit.rules.ExpectedException;
51+
import org.junit.rules.Timeout;
5452

5553
/**
5654
* A base class for system tests. This class can be extended to run system tests in different
@@ -497,15 +495,16 @@ public void testLoggingHandler() throws InterruptedException {
497495
}
498496

499497
@Test
500-
public void testAsyncLoggingHandler() throws InterruptedException {
501-
String logName = formatForTest("test-async-logging-handler");
498+
public void testSyncLoggingHandler() throws InterruptedException {
499+
String logName = formatForTest("test-sync-logging-handler");
502500
LoggingOptions options = logging().getOptions();
503501
MonitoredResource resource = MonitoredResource.of("gce_instance",
504502
ImmutableMap.of("project_id", options.getProjectId(),
505503
"instance_id", "instance",
506504
"zone", "us-central1-a"));
507-
LoggingHandler handler = new AsyncLoggingHandler(logName, options, resource);
505+
LoggingHandler handler = new LoggingHandler(logName, options, resource);
508506
handler.setLevel(Level.WARNING);
507+
handler.setSynchronicity(Synchronicity.SYNC);
509508
Logger logger = Logger.getLogger(getClass().getName());
510509
logger.addHandler(handler);
511510
logger.setLevel(Level.WARNING);

0 commit comments

Comments
 (0)