Skip to content

Commit c6139d6

Browse files
committed
document "base severity level"
also improve tests
1 parent 2fd0e20 commit c6139d6

File tree

2 files changed

+29
-20
lines changed

2 files changed

+29
-20
lines changed

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@
6868
* {@code java.log}).
6969
* <li>{@code com.google.cloud.logging.LoggingHandler.level} specifies the default level for the
7070
* handler (defaults to {@code Level.INFO}).
71+
* <p> This configuration also sets the "base severity level".
72+
* Logs with the same severity with the base could be more efficiently sent to Stackdriver.
73+
* The base severity defaults to the level of of handler or {@code Level.FINEST}
74+
* if the handler is set to {@code Level.ALL}.
75+
* There is currently no way to modify the base level, see
76+
* <a href="https://github.com/GoogleCloudPlatform/google-cloud-java/issues/1740">tracking issue</a>.
7177
* <li>{@code com.google.cloud.logging.LoggingHandler.filter} specifies the name of a {@link Filter}
7278
* class to use (defaults to no filter).
7379
* <li>{@code com.google.cloud.logging.LoggingHandler.formatter} specifies the name of a
@@ -110,7 +116,7 @@ public class LoggingHandler extends Handler {
110116
private volatile Logging logging;
111117
private Level flushLevel;
112118
private long flushSize;
113-
private final Level nativeLevel;
119+
private final Level baseLevel;
114120
private Synchronicity synchronicity;
115121
private final List<Enhancer> enhancers;
116122

@@ -178,7 +184,7 @@ public LoggingHandler(String log, LoggingOptions options, MonitoredResource moni
178184

179185
Level level = helper.getLevelProperty(className + ".level", Level.INFO);
180186
setLevel(level);
181-
nativeLevel = level;
187+
baseLevel = level.equals(Level.ALL) ? Level.FINEST : level;
182188

183189
this.synchronicity =
184190
helper.getSynchronicityProperty(className + ".synchronicity", Synchronicity.ASYNC);
@@ -196,9 +202,9 @@ public LoggingHandler(String log, LoggingOptions options, MonitoredResource moni
196202
WriteOption.labels(
197203
ImmutableMap.of(
198204
LEVEL_NAME_KEY,
199-
nativeLevel.getName(),
205+
baseLevel.getName(),
200206
LEVEL_VALUE_KEY,
201-
nativeLevel.intValue() + ""))
207+
String.valueOf(baseLevel.intValue())))
202208
};
203209
} catch (Exception ex) {
204210
reportError(null, ex, ErrorManager.OPEN_FAILURE);
@@ -396,7 +402,7 @@ private LogEntry entryFor(LogRecord record) {
396402
.setTimestamp(record.getMillis())
397403
.setSeverity(severityFor(level));
398404

399-
if (!nativeLevel.equals(level)) {
405+
if (!baseLevel.equals(level)) {
400406
builder
401407
.addLabel("levelName", level.getName())
402408
.addLabel("levelValue", String.valueOf(level.intValue()));

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

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -134,14 +134,6 @@ public class LoggingHandlerTest {
134134
.addLabel("levelValue", String.valueOf(LoggingLevel.EMERGENCY.intValue()))
135135
.setTimestamp(123456789L)
136136
.build();
137-
private static final ImmutableMap<String, String> NATIVE_SEVERITY_MAP =
138-
ImmutableMap.of("levelName", Level.INFO.getName(), "levelValue", Level.INFO.intValue() + "");
139-
private static final WriteOption[] DEFAULT_OPTION =
140-
new WriteOption[] {
141-
WriteOption.logName(LOG_NAME),
142-
WriteOption.resource(DEFAULT_RESOURCE),
143-
WriteOption.labels(NATIVE_SEVERITY_MAP)
144-
};
145137
private static final String CONFIG_NAMESPACE = "com.google.cloud.logging.LoggingHandler";
146138
private static final ImmutableMap<String, String> CONFIG_MAP =
147139
ImmutableMap.<String, String>builder()
@@ -155,8 +147,15 @@ public class LoggingHandlerTest {
155147
.put("resourceType", "testResourceType")
156148
.put("synchronicity", "SYNC")
157149
.build();
150+
private static final ImmutableMap<String, String> NATIVE_SEVERITY_MAP =
151+
ImmutableMap.of(
152+
"levelName", Level.INFO.getName(), "levelValue", String.valueOf(Level.INFO.intValue()));
158153
private static final WriteOption[] DEFAULT_OPTIONS =
159-
new WriteOption[] {WriteOption.logName(LOG_NAME), WriteOption.resource(DEFAULT_RESOURCE)};
154+
new WriteOption[] {
155+
WriteOption.logName(LOG_NAME),
156+
WriteOption.resource(DEFAULT_RESOURCE),
157+
WriteOption.labels(NATIVE_SEVERITY_MAP)
158+
};
160159

161160
private static byte[] renderConfig(Map<String, String> config) {
162161
StringBuilder str = new StringBuilder();
@@ -220,6 +219,8 @@ private static LogRecord newLogRecord(Level level, String message) {
220219

221220
@Test
222221
public void testPublishLevels() {
222+
EasyMock.expect(options.getProjectId()).andReturn(PROJECT).anyTimes();
223+
EasyMock.expect(options.getService()).andReturn(logging);
223224
logging.writeAsync(ImmutableList.of(FINEST_ENTRY), DEFAULT_OPTIONS);
224225
EasyMock.expectLastCall().andReturn(ApiFutures.immediateFuture(null));
225226
logging.writeAsync(ImmutableList.of(FINER_ENTRY), DEFAULT_OPTIONS);
@@ -411,9 +412,7 @@ public void testSyncWrite() {
411412
.addLabel("levelValue", String.valueOf(Level.FINEST.intValue()))
412413
.setTimestamp(123456789L)
413414
.build();
414-
logging.write(
415-
ImmutableList.of(entry),
416-
DEFAULT_OPTIONS);
415+
logging.write(ImmutableList.of(entry), DEFAULT_OPTIONS);
417416
EasyMock.replay(options, logging);
418417
LoggingHandler handler = new LoggingHandler(LOG_NAME, options);
419418
handler.setLevel(Level.ALL);
@@ -452,8 +451,6 @@ public void testPropertiesFile() throws IOException, InterruptedException {
452451
LogEntry entry =
453452
LogEntry.newBuilder(Payload.StringPayload.of(MESSAGE))
454453
.setSeverity(Severity.DEBUG)
455-
.addLabel("levelName", "FINEST")
456-
.addLabel("levelValue", String.valueOf(Level.FINEST.intValue()))
457454
.addLabel("enhanced", "true")
458455
.setTimestamp(123456789L)
459456
.build();
@@ -462,7 +459,13 @@ public void testPropertiesFile() throws IOException, InterruptedException {
462459
WriteOption.logName("testLogName"),
463460
WriteOption.resource(
464461
MonitoredResource.of(
465-
"testResourceType", ImmutableMap.of("project_id", PROJECT, "enhanced", "true"))));
462+
"testResourceType", ImmutableMap.of("project_id", PROJECT, "enhanced", "true"))),
463+
WriteOption.labels(
464+
ImmutableMap.of(
465+
"levelName",
466+
Level.FINEST.getName(),
467+
"levelValue",
468+
String.valueOf(Level.FINEST.intValue()))));
466469
EasyMock.replay(options, logging);
467470
LogManager.getLogManager()
468471
.readConfiguration(new ByteArrayInputStream(renderConfig(CONFIG_MAP)));

0 commit comments

Comments
 (0)