Skip to content

Commit

Permalink
Add async/sync setting to logging handler (#1716)
Browse files Browse the repository at this point in the history
* Add async/sync setting to logging handler
  • Loading branch information
michaelbausor committed Mar 9, 2017
1 parent 9322afe commit ff24b05
Show file tree
Hide file tree
Showing 6 changed files with 204 additions and 191 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import com.google.cloud.MonitoredResource;
import com.google.cloud.logging.Logging.WriteOption;
import com.google.common.collect.ImmutableList;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
Expand Down Expand Up @@ -83,6 +82,8 @@
* a {@link MonitoredResource} or {@link LogEntry} instance (defaults to empty list).
* <li>{@code com.google.cloud.logging.LoggingHandler.resourceType} the type name to use when
* creating the default {@link MonitoredResource} (defaults to "global").
* <li>{@code com.google.cloud.logging.Synchronicity} the synchronicity of the write method to use
* to write logs to the Stackdriver Logging service (defaults to {@link Synchronicity#ASYNC}).
* </ul>
*
* <p>To add a {@code LoggingHandler} to an existing {@link Logger} and be sure to avoid infinite
Expand All @@ -106,6 +107,7 @@ public class LoggingHandler extends Handler {
private volatile Logging logging;
private Level flushLevel;
private long flushSize;
private Synchronicity synchronicity;
private final List<Enhancer> enhancers;

/**
Expand Down Expand Up @@ -169,6 +171,8 @@ public LoggingHandler(String log, LoggingOptions options, MonitoredResource moni
this.options = options != null ? options : LoggingOptions.getDefaultInstance();
this.flushLevel = helper.getLevelProperty(className + ".flushLevel", LoggingLevel.ERROR);
this.flushSize = helper.getLongProperty(className + ".flushSize", 1L);
this.synchronicity =
helper.getSynchronicityProperty(className + ".synchronicity", Synchronicity.ASYNC);
setLevel(helper.getLevelProperty(className + ".level", Level.INFO));
setFilter(helper.getFilterProperty(className + ".filter", null));
setFormatter(helper.getFormatterProperty(className + ".formatter", new SimpleFormatter()));
Expand Down Expand Up @@ -296,6 +300,16 @@ List<Enhancer> getEnhancerProperty(String name) {
}
return Collections.emptyList();
}

Synchronicity getSynchronicityProperty(String name, Synchronicity defaultValue) {
String synchronicity = manager.getProperty(name);
try {
return Synchronicity.valueOf(synchronicity);
} catch (Exception ex) {
// If we cannot create the Synchronicity we fall back to default value
}
return defaultValue;
}
}

/**
Expand Down Expand Up @@ -419,7 +433,15 @@ private static Severity severityFor(Level level) {
* how entries should be written.
*/
void write(List<LogEntry> entries, WriteOption... options) {
getLogging().writeAsync(entries, options);
switch (this.synchronicity) {
case SYNC:
getLogging().write(entries, options);
break;
case ASYNC:
default:
getLogging().writeAsync(entries, options);
break;
}
}

@Override
Expand Down Expand Up @@ -476,6 +498,11 @@ public synchronized Level setFlushLevel(Level flushLevel) {
return flushLevel;
}

/** Get the flush log level. */
public Level getFlushLevel() {
return this.flushLevel;
}

/**
* Sets the maximum size of the log buffer. Once the maximum size of the buffer is reached, logs
* are transmitted to the Stackdriver Logging service. If not set, a log is sent to the service as
Expand All @@ -486,6 +513,28 @@ public synchronized long setFlushSize(long flushSize) {
return flushSize;
}

/** Get the maximum size of the log buffer. */
public long getFlushSize() {
return this.flushSize;
}

/**
* Sets the synchronicity of the write method used to write logs to the Stackdriver Logging
* service. Defaults to {@link Synchronicity#ASYNC}.
*/
public synchronized Synchronicity setSynchronicity(Synchronicity synchronicity) {
this.synchronicity = synchronicity;
return synchronicity;
}

/**
* Get the synchronicity of the write method used to write logs to the Stackdriver Logging
* service.
*/
public Synchronicity getSynchronicity() {
return this.synchronicity;
}

/**
* Adds the provided {@code LoggingHandler} to {@code logger}. Use this method to register Cloud
* Logging handlers instead of {@link Logger#addHandler(Handler)} to avoid infinite recursion
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright 2017 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.cloud.logging;

/**
* Used to specify the behavior of write calls to the Stackdriver Logging service. Specifying SYNC
* will make synchronous calls; specifying ASYNC will make asynchronous calls. The default behavior
* is ASYNC.
*/
public enum Synchronicity {
SYNC,
ASYNC,
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,15 @@
import com.google.common.collect.Sets;
import com.google.protobuf.Any;
import com.google.protobuf.StringValue;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.rules.Timeout;

import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.rules.Timeout;

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

@Test
public void testAsyncLoggingHandler() throws InterruptedException {
String logName = formatForTest("test-async-logging-handler");
public void testSyncLoggingHandler() throws InterruptedException {
String logName = formatForTest("test-sync-logging-handler");
LoggingOptions options = logging().getOptions();
MonitoredResource resource = MonitoredResource.of("gce_instance",
ImmutableMap.of("project_id", options.getProjectId(),
"instance_id", "instance",
"zone", "us-central1-a"));
LoggingHandler handler = new AsyncLoggingHandler(logName, options, resource);
LoggingHandler handler = new LoggingHandler(logName, options, resource);
handler.setLevel(Level.WARNING);
handler.setSynchronicity(Synchronicity.SYNC);
Logger logger = Logger.getLogger(getClass().getName());
logger.addHandler(handler);
logger.setLevel(Level.WARNING);
Expand Down
Loading

0 comments on commit ff24b05

Please sign in to comment.