|
20 | 20 |
|
21 | 21 | import com.google.cloud.MonitoredResource; |
22 | 22 | import com.google.cloud.logging.Logging.WriteOption; |
| 23 | +import com.google.api.gax.core.ApiFutures; |
| 24 | +import com.google.api.gax.core.ApiFutureCallback; |
23 | 25 | import com.google.common.collect.ImmutableList; |
24 | 26 | import com.google.common.collect.ImmutableMap; |
25 | 27 | import java.util.ArrayList; |
@@ -106,7 +108,6 @@ public class LoggingHandler extends Handler { |
106 | 108 |
|
107 | 109 | private final LoggingOptions options; |
108 | 110 | private final WriteOption[] writeOptions; |
109 | | - private List<LogEntry> buffer = new LinkedList<>(); |
110 | 111 | private volatile Logging logging; |
111 | 112 | private Level flushLevel; |
112 | 113 | private long flushSize; |
@@ -372,22 +373,9 @@ public void publish(LogRecord record) { |
372 | 373 |
|
373 | 374 | try { |
374 | 375 | LogEntry entry = entryFor(record); |
375 | | - |
376 | | - List<LogEntry> flushBuffer = null; |
377 | | - WriteOption[] flushWriteOptions = null; |
378 | | - |
379 | | - synchronized (this) { |
380 | | - if (entry != null) { |
381 | | - buffer.add(entry); |
382 | | - } |
383 | | - if (buffer.size() >= flushSize || record.getLevel().intValue() >= flushLevel.intValue()) { |
384 | | - flushBuffer = buffer; |
385 | | - flushWriteOptions = writeOptions; |
386 | | - buffer = new LinkedList<>(); |
387 | | - } |
| 376 | + if (entry != null) { |
| 377 | + write(entry, writeOptions); |
388 | 378 | } |
389 | | - |
390 | | - flush(flushBuffer, flushWriteOptions); |
391 | 379 | } finally { |
392 | 380 | inPublishCall.remove(); |
393 | 381 | } |
@@ -459,45 +447,38 @@ private static Severity severityFor(Level level) { |
459 | 447 | * Writes the provided list of log entries to Stackdriver Logging. Override this method to change |
460 | 448 | * how entries should be written. |
461 | 449 | */ |
462 | | - void write(List<LogEntry> entries, WriteOption... options) { |
| 450 | + void write(LogEntry entry, WriteOption... options) { |
| 451 | + List<LogEntry> entryList = Collections.singletonList(entry); |
463 | 452 | switch (this.synchronicity) { |
464 | 453 | case SYNC: |
465 | | - getLogging().write(entries, options); |
| 454 | + try { |
| 455 | + getLogging().write(entryList, options); |
| 456 | + } catch (Exception ex) { |
| 457 | + reportError(null, ex, ErrorManager.FLUSH_FAILURE); |
| 458 | + } |
466 | 459 | break; |
467 | 460 | case ASYNC: |
468 | 461 | default: |
469 | | - getLogging().writeAsync(entries, options); |
| 462 | + ApiFutures.addCallback(getLogging().writeAsync(entryList, options), new ApiFutureCallback<Void>() { |
| 463 | + @Override |
| 464 | + public void onSuccess(Void v) {} |
| 465 | + |
| 466 | + @Override |
| 467 | + public void onFailure(Throwable t) { |
| 468 | + if (t instanceof Exception) { |
| 469 | + reportError(null, (Exception) t, ErrorManager.FLUSH_FAILURE); |
| 470 | + } else { |
| 471 | + reportError(null, new Exception(t), ErrorManager.FLUSH_FAILURE); |
| 472 | + } |
| 473 | + } |
| 474 | + }); |
470 | 475 | break; |
471 | 476 | } |
472 | 477 | } |
473 | 478 |
|
474 | 479 | @Override |
475 | 480 | public void flush() { |
476 | | - List<LogEntry> flushBuffer; |
477 | | - WriteOption[] flushWriteOptions; |
478 | | - |
479 | | - synchronized (this) { |
480 | | - if (buffer.isEmpty()) { |
481 | | - return; |
482 | | - } |
483 | | - flushBuffer = buffer; |
484 | | - flushWriteOptions = writeOptions; |
485 | | - buffer = new LinkedList<>(); |
486 | | - } |
487 | | - |
488 | | - flush(flushBuffer, flushWriteOptions); |
489 | | - } |
490 | | - |
491 | | - private void flush(List<LogEntry> flushBuffer, WriteOption[] flushWriteOptions) { |
492 | | - if (flushBuffer == null) { |
493 | | - return; |
494 | | - } |
495 | | - try { |
496 | | - write(flushBuffer, flushWriteOptions); |
497 | | - } catch (Exception ex) { |
498 | | - // writing can fail but we should not throw an exception, we report the error instead |
499 | | - reportError(null, ex, ErrorManager.FLUSH_FAILURE); |
500 | | - } |
| 481 | + // BUG(1795): flush is broken, need support from batching implementation. |
501 | 482 | } |
502 | 483 |
|
503 | 484 | /** |
|
0 commit comments