Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

import com.google.cloud.MonitoredResource;
import com.google.cloud.logging.Logging.WriteOption;
import com.google.api.gax.core.ApiFutures;
import com.google.api.gax.core.ApiFutureCallback;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import java.util.ArrayList;
Expand Down Expand Up @@ -106,7 +108,6 @@ public class LoggingHandler extends Handler {

private final LoggingOptions options;
private final WriteOption[] writeOptions;
private List<LogEntry> buffer = new LinkedList<>();
private volatile Logging logging;
private Level flushLevel;
private long flushSize;
Expand Down Expand Up @@ -372,22 +373,9 @@ public void publish(LogRecord record) {

try {
LogEntry entry = entryFor(record);

List<LogEntry> flushBuffer = null;
WriteOption[] flushWriteOptions = null;

synchronized (this) {
if (entry != null) {
buffer.add(entry);
}
if (buffer.size() >= flushSize || record.getLevel().intValue() >= flushLevel.intValue()) {
flushBuffer = buffer;
flushWriteOptions = writeOptions;
buffer = new LinkedList<>();
}
if (entry != null) {
write(entry, writeOptions);

This comment was marked as spam.

This comment was marked as spam.

}

flush(flushBuffer, flushWriteOptions);
} finally {
inPublishCall.remove();
}
Expand Down Expand Up @@ -459,45 +447,38 @@ private static Severity severityFor(Level level) {
* Writes the provided list of log entries to Stackdriver Logging. Override this method to change
* how entries should be written.
*/
void write(List<LogEntry> entries, WriteOption... options) {
void write(LogEntry entry, WriteOption... options) {
List<LogEntry> entryList = Collections.singletonList(entry);
switch (this.synchronicity) {
case SYNC:
getLogging().write(entries, options);
try {
getLogging().write(entryList, options);
} catch (Exception ex) {
reportError(null, ex, ErrorManager.FLUSH_FAILURE);
}
break;
case ASYNC:
default:
getLogging().writeAsync(entries, options);
ApiFutures.addCallback(getLogging().writeAsync(entryList, options), new ApiFutureCallback<Void>() {
@Override
public void onSuccess(Void v) {}

@Override
public void onFailure(Throwable t) {
if (t instanceof Exception) {
reportError(null, (Exception) t, ErrorManager.FLUSH_FAILURE);
} else {
reportError(null, new Exception(t), ErrorManager.FLUSH_FAILURE);
}
}
});
break;
}
}

@Override
public void flush() {
List<LogEntry> flushBuffer;
WriteOption[] flushWriteOptions;

synchronized (this) {
if (buffer.isEmpty()) {
return;
}
flushBuffer = buffer;
flushWriteOptions = writeOptions;
buffer = new LinkedList<>();
}

flush(flushBuffer, flushWriteOptions);
}

private void flush(List<LogEntry> flushBuffer, WriteOption[] flushWriteOptions) {
if (flushBuffer == null) {
return;
}
try {
write(flushBuffer, flushWriteOptions);
} catch (Exception ex) {
// writing can fail but we should not throw an exception, we report the error instead
reportError(null, ex, ErrorManager.FLUSH_FAILURE);
}
// BUG(1795): flush is broken, need support from batching implementation.
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ public void testReportFlushError() {
EasyMock.expect(options.getService()).andReturn(logging);
RuntimeException ex = new RuntimeException();
logging.writeAsync(ImmutableList.of(FINEST_ENTRY), DEFAULT_OPTIONS);
EasyMock.expectLastCall().andThrow(ex);
EasyMock.expectLastCall().andReturn(ApiFutures.immediateFailedFuture(ex));
EasyMock.replay(options, logging);
ErrorManager errorManager = EasyMock.createStrictMock(ErrorManager.class);
errorManager.error(null, ex, ErrorManager.FLUSH_FAILURE);
Expand Down Expand Up @@ -356,29 +356,8 @@ public void testReportFormatError() {
EasyMock.verify(errorManager, formatter);
}

@Test
public void testFlushSize() {
EasyMock.expect(options.getProjectId()).andReturn(PROJECT).anyTimes();
EasyMock.expect(options.getService()).andReturn(logging);
logging.writeAsync(
ImmutableList.of(
FINEST_ENTRY, FINER_ENTRY, FINE_ENTRY, CONFIG_ENTRY, INFO_ENTRY, WARNING_ENTRY),
DEFAULT_OPTIONS);
EasyMock.expectLastCall().andReturn(ApiFutures.immediateFuture(null));
EasyMock.replay(options, logging);
LoggingHandler handler = new LoggingHandler(LOG_NAME, options);
handler.setLevel(Level.ALL);
handler.setFlushSize(6);
handler.setFormatter(new TestFormatter());
handler.publish(newLogRecord(Level.FINEST, MESSAGE));
handler.publish(newLogRecord(Level.FINER, MESSAGE));
handler.publish(newLogRecord(Level.FINE, MESSAGE));
handler.publish(newLogRecord(Level.CONFIG, MESSAGE));
handler.publish(newLogRecord(Level.INFO, MESSAGE));
handler.publish(newLogRecord(Level.WARNING, MESSAGE));
}

@Test
// BUG(1795): rewrite this test when flush actually works.
// @Test
public void testFlushLevel() {
EasyMock.expect(options.getProjectId()).andReturn(PROJECT).anyTimes();
EasyMock.expect(options.getService()).andReturn(logging);
Expand Down