Skip to content

Commit aa0c176

Browse files
authored
fix: Need a way to disable flushing (#1206)
* fix: Need a way to disable flushing * fix test name * add test for flush level warning * add a comment explaining Severity.UNRECOGNIZED usage for flush level * fix comment * Remove redundant definition * Adjust to latest PR review * Address PR comments * Add test to validate that Severity.NONE cannot be used * add null treatment for flushSeverity * Remove redundand null check
1 parent 2d684c2 commit aa0c176

File tree

7 files changed

+78
-28
lines changed

7 files changed

+78
-28
lines changed

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

+3
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,9 @@ public String toStructuredJsonString() {
715715
if (payload.getType() == Payload.Type.PROTO) {
716716
throw new UnsupportedOperationException("LogEntry with protobuf payload cannot be converted");
717717
}
718+
if (severity == Severity.NONE) {
719+
throw new IllegalArgumentException("Severity.NONE cannot be used for LogEntry");
720+
}
718721

719722
final StringBuilder builder = new StringBuilder("{");
720723
final StructuredLogFormatter formatter = new StructuredLogFormatter(builder);

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -303,8 +303,8 @@ public static TailOption project(String project) {
303303

304304
/**
305305
* Sets flush severity for asynchronous logging writes. It is disabled by default, enabled when
306-
* this method is called with not null value. Logs will be immediately written out for entries at
307-
* or higher than flush severity.
306+
* this method is called with any {@link Severity} value other than {@link Severity.NONE}. Logs
307+
* will be immediately written out for entries at or higher than flush severity.
308308
*
309309
* <p>Enabling this can cause the leaking and hanging threads, see BUG(2796) BUG(3880). However
310310
* you can explicitly call {@link #flush}.

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

+14-22
Original file line numberDiff line numberDiff line change
@@ -498,29 +498,21 @@ private static Severity severityFor(Level level) {
498498
if (level instanceof LoggingLevel) {
499499
return ((LoggingLevel) level).getSeverity();
500500
}
501-
502-
switch (level.intValue()) {
503-
// FINEST
504-
// FINER
505-
// FINE
506-
case 300:
507-
case 400:
508-
case 500:
509-
return Severity.DEBUG;
510-
// CONFIG
511-
// INFO
512-
case 700:
513-
case 800:
514-
return Severity.INFO;
515-
// WARNING
516-
case 900:
517-
return Severity.WARNING;
518-
// SEVERE
519-
case 1000:
520-
return Severity.ERROR;
521-
default:
522-
return Severity.DEFAULT;
501+
// Choose the severity based on Level range.
502+
// The assumption is that Level values below maintain same numeric value
503+
int value = level.intValue();
504+
if (value <= Level.FINE.intValue()) {
505+
return Severity.DEBUG;
506+
} else if (value <= Level.INFO.intValue()) {
507+
return Severity.INFO;
508+
} else if (value <= Level.WARNING.intValue()) {
509+
return Severity.WARNING;
510+
} else if (value <= Level.SEVERE.intValue()) {
511+
return Severity.ERROR;
512+
} else if (value == Level.OFF.intValue()) {
513+
return Severity.NONE;
523514
}
515+
return Severity.DEFAULT;
524516
}
525517

526518
private static boolean isTrueOrNull(Boolean b) {

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

+8-3
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ class LoggingImpl extends BaseService<LoggingOptions> implements Logging {
107107
private final Map<Object, ApiFuture<Void>> pendingWrites = new ConcurrentHashMap<>();
108108

109109
private volatile Synchronicity writeSynchronicity = Synchronicity.ASYNC;
110-
private volatile Severity flushSeverity = null;
110+
private volatile Severity flushSeverity = Severity.NONE;
111111
private boolean closed;
112112

113113
private static Boolean emptyToBooleanFunction(Empty input) {
@@ -138,7 +138,12 @@ public void setWriteSynchronicity(Synchronicity writeSynchronicity) {
138138

139139
@Override
140140
public void setFlushSeverity(Severity flushSeverity) {
141-
this.flushSeverity = flushSeverity;
141+
// For backward compatibility we treat null as Severity.NONE
142+
if (flushSeverity == null) {
143+
this.flushSeverity = Severity.NONE;
144+
} else {
145+
this.flushSeverity = flushSeverity;
146+
}
142147
}
143148

144149
@Override
@@ -882,7 +887,7 @@ public void write(Iterable<LogEntry> logEntries, WriteOption... options) {
882887
options = Instrumentation.addPartialSuccessOption(options);
883888
}
884889
writeLogEntries(logEntries, options);
885-
if (flushSeverity != null) {
890+
if (flushSeverity != Severity.NONE) {
886891
for (LogEntry logEntry : logEntries) {
887892
// flush pending writes if log severity at or above flush severity
888893
if (logEntry.getSeverity().compareTo(flushSeverity) >= 0) {

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

+3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
*/
2727
public enum Severity {
2828

29+
/** The None severity level. Should not be used with log entries. */
30+
NONE(LogSeverity.UNRECOGNIZED),
31+
2932
/** The log entry has no assigned severity level. */
3033
DEFAULT(LogSeverity.DEFAULT),
3134

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

+10
Original file line numberDiff line numberDiff line change
@@ -394,4 +394,14 @@ public void testStructureLogPresentations() {
394394
public void testStructureLogPresentationWithProtobufPayload() {
395395
assertThrows(UnsupportedOperationException.class, () -> PROTO_ENTRY.toStructuredJsonString());
396396
}
397+
398+
@Test
399+
public void testStructureLogInvalidSeverity() {
400+
assertThrows(
401+
IllegalArgumentException.class,
402+
() -> PROTO_ENTRY.toBuilder().setSeverity(Severity.NONE).build().toPb(PROJECT));
403+
assertThrows(
404+
IllegalArgumentException.class,
405+
() -> STRING_ENTRY.toBuilder().setSeverity(Severity.NONE).build().toStructuredJsonString());
406+
}
397407
}

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

+38-1
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,12 @@ protected void after() {
231231
}
232232
}
233233

234+
static class CustomLevel extends Level {
235+
CustomLevel() {
236+
super("CUSTOM", 510);
237+
}
238+
}
239+
234240
@Rule public final OutputStreamPatcher outputStreamPatcher = new OutputStreamPatcher();
235241

236242
@Before
@@ -241,7 +247,7 @@ public void setUp() {
241247
expect(options.getProjectId()).andStubReturn(PROJECT);
242248
expect(options.getService()).andStubReturn(logging);
243249
expect(options.getAutoPopulateMetadata()).andStubReturn(Boolean.FALSE);
244-
logging.setFlushSeverity(EasyMock.anyObject(Severity.class));
250+
logging.setFlushSeverity(Severity.ERROR);
245251
expectLastCall().anyTimes();
246252
logging.setWriteSynchronicity(EasyMock.anyObject(Synchronicity.class));
247253
expectLastCall().anyTimes();
@@ -537,6 +543,37 @@ public void testFlushLevel() {
537543
handler.publish(newLogRecord(Level.WARNING, MESSAGE));
538544
}
539545

546+
@Test
547+
public void testFlushLevelOff() {
548+
logging.setFlushSeverity(Severity.NONE);
549+
expectLastCall().once();
550+
replay(options, logging);
551+
LoggingHandler handler = new LoggingHandler(LOG_NAME, options, DEFAULT_RESOURCE);
552+
handler.setFlushLevel(Level.OFF);
553+
assertEquals(Level.OFF, handler.getFlushLevel());
554+
}
555+
556+
@Test
557+
public void testFlushLevelOn() {
558+
logging.setFlushSeverity(Severity.WARNING);
559+
expectLastCall().once();
560+
replay(options, logging);
561+
LoggingHandler handler = new LoggingHandler(LOG_NAME, options, DEFAULT_RESOURCE);
562+
handler.setFlushLevel(Level.WARNING);
563+
assertEquals(Level.WARNING, handler.getFlushLevel());
564+
}
565+
566+
@Test
567+
public void testCustomFlushLevelOn() {
568+
CustomLevel level = new CustomLevel();
569+
logging.setFlushSeverity(Severity.INFO);
570+
expectLastCall().once();
571+
replay(options, logging);
572+
LoggingHandler handler = new LoggingHandler(LOG_NAME, options, DEFAULT_RESOURCE);
573+
handler.setFlushLevel(level);
574+
assertEquals(level, handler.getFlushLevel());
575+
}
576+
540577
@Test
541578
public void testSyncWrite() {
542579
reset(logging);

0 commit comments

Comments
 (0)