Skip to content

Commit 0010dd8

Browse files
gregwgarrettjonesgoogle
authored andcommitted
Set timestamp from LogRecord (#1533)
1 parent f495c23 commit 0010dd8

File tree

3 files changed

+61
-32
lines changed

3 files changed

+61
-32
lines changed

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

+1
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,7 @@ private LogEntry entryFor(LogRecord record) {
309309
LogEntry.Builder builder = LogEntry.newBuilder(Payload.StringPayload.of(payload))
310310
.addLabel("levelName", level.getName())
311311
.addLabel("levelValue", String.valueOf(level.intValue()))
312+
.setTimestamp(record.getMillis())
312313
.setSeverity(severityFor(level));
313314
enhanceLogEntry(builder, record);
314315
return builder.build();

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,16 @@ public void testPublish() {
6565
.setSeverity(Severity.DEBUG)
6666
.addLabel("levelName", "FINEST")
6767
.addLabel("levelValue", String.valueOf(Level.FINEST.intValue()))
68+
.setTimestamp(123456789L)
6869
.build();
6970
EasyMock.expect(logging.writeAsync(ImmutableList.of(entry), WriteOption.logName(LOG_NAME),
7071
WriteOption.resource(DEFAULT_RESOURCE))).andReturn(FUTURE);
7172
EasyMock.replay(options, logging);
7273
Handler handler = new AsyncLoggingHandler(LOG_NAME, options);
7374
handler.setLevel(Level.ALL);
7475
handler.setFormatter(new TestFormatter());
75-
handler.publish(new LogRecord(Level.FINEST, MESSAGE));
76+
LogRecord record = new LogRecord(Level.FINEST, MESSAGE);
77+
record.setMillis(123456789L);
78+
handler.publish(record);
7679
}
7780
}

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

+56-31
Original file line numberDiff line numberDiff line change
@@ -44,66 +44,79 @@ public class LoggingHandlerTest {
4444
.setSeverity(Severity.DEBUG)
4545
.addLabel("levelName", "FINEST")
4646
.addLabel("levelValue", String.valueOf(Level.FINEST.intValue()))
47+
.setTimestamp(123456789L)
4748
.build();
4849
private static final LogEntry FINER_ENTRY = LogEntry.newBuilder(StringPayload.of(MESSAGE))
4950
.setSeverity(Severity.DEBUG)
5051
.addLabel("levelName", "FINER")
5152
.addLabel("levelValue", String.valueOf(Level.FINER.intValue()))
53+
.setTimestamp(123456789L)
5254
.build();
5355
private static final LogEntry FINE_ENTRY = LogEntry.newBuilder(StringPayload.of(MESSAGE))
5456
.setSeverity(Severity.DEBUG)
5557
.addLabel("levelName", "FINE")
5658
.addLabel("levelValue", String.valueOf(Level.FINE.intValue()))
59+
.setTimestamp(123456789L)
5760
.build();
5861
private static final LogEntry CONFIG_ENTRY = LogEntry.newBuilder(StringPayload.of(MESSAGE))
5962
.setSeverity(Severity.INFO)
6063
.addLabel("levelName", "CONFIG")
6164
.addLabel("levelValue", String.valueOf(Level.CONFIG.intValue()))
65+
.setTimestamp(123456789L)
6266
.build();
6367
private static final LogEntry INFO_ENTRY = LogEntry.newBuilder(StringPayload.of(MESSAGE))
6468
.setSeverity(Severity.INFO)
6569
.addLabel("levelName", "INFO")
6670
.addLabel("levelValue", String.valueOf(Level.INFO.intValue()))
71+
.setTimestamp(123456789L)
6772
.build();
6873
private static final LogEntry WARNING_ENTRY = LogEntry.newBuilder(StringPayload.of(MESSAGE))
6974
.setSeverity(Severity.WARNING)
7075
.addLabel("levelName", "WARNING")
7176
.addLabel("levelValue", String.valueOf(Level.WARNING.intValue()))
77+
.setTimestamp(123456789L)
7278
.build();
7379
private static final LogEntry SEVERE_ENTRY = LogEntry.newBuilder(StringPayload.of(MESSAGE))
7480
.setSeverity(Severity.ERROR)
7581
.addLabel("levelName", "SEVERE")
7682
.addLabel("levelValue", String.valueOf(Level.SEVERE.intValue()))
83+
.setTimestamp(123456789L)
7784
.build();
7885
private static final LogEntry DEBUG_ENTRY = LogEntry.newBuilder(StringPayload.of(MESSAGE))
7986
.setSeverity(Severity.DEBUG)
8087
.addLabel("levelName", "DEBUG")
8188
.addLabel("levelValue", String.valueOf(LoggingLevel.DEBUG.intValue()))
89+
.setTimestamp(123456789L)
8290
.build();
8391
private static final LogEntry NOTICE_ENTRY = LogEntry.newBuilder(StringPayload.of(MESSAGE))
8492
.setSeverity(Severity.NOTICE)
8593
.addLabel("levelName", "NOTICE")
8694
.addLabel("levelValue", String.valueOf(LoggingLevel.NOTICE.intValue()))
95+
.setTimestamp(123456789L)
8796
.build();
8897
private static final LogEntry ERROR_ENTRY = LogEntry.newBuilder(StringPayload.of(MESSAGE))
8998
.setSeverity(Severity.ERROR)
9099
.addLabel("levelName", "ERROR")
91100
.addLabel("levelValue", String.valueOf(LoggingLevel.ERROR.intValue()))
101+
.setTimestamp(123456789L)
92102
.build();
93103
private static final LogEntry CRITICAL_ENTRY = LogEntry.newBuilder(StringPayload.of(MESSAGE))
94104
.setSeverity(Severity.CRITICAL)
95105
.addLabel("levelName", "CRITICAL")
96106
.addLabel("levelValue", String.valueOf(LoggingLevel.CRITICAL.intValue()))
107+
.setTimestamp(123456789L)
97108
.build();
98109
private static final LogEntry ALERT_ENTRY = LogEntry.newBuilder(StringPayload.of(MESSAGE))
99110
.setSeverity(Severity.ALERT)
100111
.addLabel("levelName", "ALERT")
101112
.addLabel("levelValue", String.valueOf(LoggingLevel.ALERT.intValue()))
113+
.setTimestamp(123456789L)
102114
.build();
103115
private static final LogEntry EMERGENCY_ENTRY = LogEntry.newBuilder(StringPayload.of(MESSAGE))
104116
.setSeverity(Severity.EMERGENCY)
105117
.addLabel("levelName", "EMERGENCY")
106118
.addLabel("levelValue", String.valueOf(LoggingLevel.EMERGENCY.intValue()))
119+
.setTimestamp(123456789L)
107120
.build();
108121

109122
private Logging logging;
@@ -127,6 +140,13 @@ public void setUp() {
127140
public void afterClass() {
128141
EasyMock.verify(logging, options);
129142
}
143+
144+
145+
private static LogRecord newLogRecord(Level level, String message) {
146+
LogRecord record = new LogRecord(level, message);
147+
record.setMillis(123456789L);
148+
return record;
149+
}
130150

131151
@Test
132152
public void testPublishLevels() {
@@ -176,20 +196,20 @@ public void testPublishLevels() {
176196
handler.setLevel(Level.ALL);
177197
handler.setFormatter(new TestFormatter());
178198
// default levels
179-
handler.publish(new LogRecord(Level.FINEST, MESSAGE));
180-
handler.publish(new LogRecord(Level.FINER, MESSAGE));
181-
handler.publish(new LogRecord(Level.FINE, MESSAGE));
182-
handler.publish(new LogRecord(Level.CONFIG, MESSAGE));
183-
handler.publish(new LogRecord(Level.INFO, MESSAGE));
184-
handler.publish(new LogRecord(Level.WARNING, MESSAGE));
185-
handler.publish(new LogRecord(Level.SEVERE, MESSAGE));
199+
handler.publish(newLogRecord(Level.FINEST, MESSAGE));
200+
handler.publish(newLogRecord(Level.FINER, MESSAGE));
201+
handler.publish(newLogRecord(Level.FINE, MESSAGE));
202+
handler.publish(newLogRecord(Level.CONFIG, MESSAGE));
203+
handler.publish(newLogRecord(Level.INFO, MESSAGE));
204+
handler.publish(newLogRecord(Level.WARNING, MESSAGE));
205+
handler.publish(newLogRecord(Level.SEVERE, MESSAGE));
186206
// Logging levels
187-
handler.publish(new LogRecord(LoggingLevel.DEBUG, MESSAGE));
188-
handler.publish(new LogRecord(LoggingLevel.NOTICE, MESSAGE));
189-
handler.publish(new LogRecord(LoggingLevel.ERROR, MESSAGE));
190-
handler.publish(new LogRecord(LoggingLevel.CRITICAL, MESSAGE));
191-
handler.publish(new LogRecord(LoggingLevel.ALERT, MESSAGE));
192-
handler.publish(new LogRecord(LoggingLevel.EMERGENCY, MESSAGE));
207+
handler.publish(newLogRecord(LoggingLevel.DEBUG, MESSAGE));
208+
handler.publish(newLogRecord(LoggingLevel.NOTICE, MESSAGE));
209+
handler.publish(newLogRecord(LoggingLevel.ERROR, MESSAGE));
210+
handler.publish(newLogRecord(LoggingLevel.CRITICAL, MESSAGE));
211+
handler.publish(newLogRecord(LoggingLevel.ALERT, MESSAGE));
212+
handler.publish(newLogRecord(LoggingLevel.EMERGENCY, MESSAGE));
193213
}
194214

195215
@Test
@@ -204,7 +224,7 @@ public void testPublishCustomResource() {
204224
Handler handler = new LoggingHandler(LOG_NAME, options, resource);
205225
handler.setLevel(Level.ALL);
206226
handler.setFormatter(new TestFormatter());
207-
handler.publish(new LogRecord(Level.FINEST, MESSAGE));
227+
handler.publish(newLogRecord(Level.FINEST, MESSAGE));
208228
}
209229

210230
@Test
@@ -224,7 +244,7 @@ public void testReportFlushError() {
224244
handler.setLevel(Level.ALL);
225245
handler.setErrorManager(errorManager);
226246
handler.setFormatter(new TestFormatter());
227-
handler.publish(new LogRecord(Level.FINEST, MESSAGE));
247+
handler.publish(newLogRecord(Level.FINEST, MESSAGE));
228248
EasyMock.verify(errorManager);
229249
}
230250

@@ -237,7 +257,7 @@ public void testReportFormatError() {
237257
ErrorManager errorManager = EasyMock.createStrictMock(ErrorManager.class);
238258
errorManager.error(null, ex, ErrorManager.FORMAT_FAILURE);
239259
EasyMock.expectLastCall().once();
240-
LogRecord record = new LogRecord(Level.FINEST, MESSAGE);
260+
LogRecord record = newLogRecord(Level.FINEST, MESSAGE);
241261
EasyMock.expect(formatter.format(record)).andThrow(ex);
242262
EasyMock.replay(errorManager, formatter);
243263
Handler handler = new LoggingHandler(LOG_NAME, options);
@@ -260,12 +280,12 @@ public void testFlushSize() {
260280
handler.setLevel(Level.ALL);
261281
handler.setFlushSize(6);
262282
handler.setFormatter(new TestFormatter());
263-
handler.publish(new LogRecord(Level.FINEST, MESSAGE));
264-
handler.publish(new LogRecord(Level.FINER, MESSAGE));
265-
handler.publish(new LogRecord(Level.FINE, MESSAGE));
266-
handler.publish(new LogRecord(Level.CONFIG, MESSAGE));
267-
handler.publish(new LogRecord(Level.INFO, MESSAGE));
268-
handler.publish(new LogRecord(Level.WARNING, MESSAGE));
283+
handler.publish(newLogRecord(Level.FINEST, MESSAGE));
284+
handler.publish(newLogRecord(Level.FINER, MESSAGE));
285+
handler.publish(newLogRecord(Level.FINE, MESSAGE));
286+
handler.publish(newLogRecord(Level.CONFIG, MESSAGE));
287+
handler.publish(newLogRecord(Level.INFO, MESSAGE));
288+
handler.publish(newLogRecord(Level.WARNING, MESSAGE));
269289
}
270290

271291
@Test
@@ -282,12 +302,12 @@ public void testFlushLevel() {
282302
handler.setFlushSize(100);
283303
handler.setFlushLevel(Level.WARNING);
284304
handler.setFormatter(new TestFormatter());
285-
handler.publish(new LogRecord(Level.FINEST, MESSAGE));
286-
handler.publish(new LogRecord(Level.FINER, MESSAGE));
287-
handler.publish(new LogRecord(Level.FINE, MESSAGE));
288-
handler.publish(new LogRecord(Level.CONFIG, MESSAGE));
289-
handler.publish(new LogRecord(Level.INFO, MESSAGE));
290-
handler.publish(new LogRecord(Level.WARNING, MESSAGE));
305+
handler.publish(newLogRecord(Level.FINEST, MESSAGE));
306+
handler.publish(newLogRecord(Level.FINER, MESSAGE));
307+
handler.publish(newLogRecord(Level.FINE, MESSAGE));
308+
handler.publish(newLogRecord(Level.CONFIG, MESSAGE));
309+
handler.publish(newLogRecord(Level.INFO, MESSAGE));
310+
handler.publish(newLogRecord(Level.WARNING, MESSAGE));
291311
}
292312

293313
@Test
@@ -298,13 +318,18 @@ public void testAddHandler() {
298318
WriteOption.resource(DEFAULT_RESOURCE));
299319
EasyMock.expectLastCall().andReturn(Futures.immediateFuture(null));
300320
EasyMock.replay(options, logging);
301-
LoggingHandler handler = new LoggingHandler(LOG_NAME, options);
321+
LoggingHandler handler = new LoggingHandler(LOG_NAME, options) {
322+
@Override
323+
public void close() {
324+
// Make close NOOP to avoid mock close exception
325+
}
326+
};
302327
handler.setLevel(Level.ALL);
303328
handler.setFormatter(new TestFormatter());
304329
Logger logger = Logger.getLogger(getClass().getName());
305330
logger.setLevel(Level.ALL);
306331
LoggingHandler.addHandler(logger, handler);
307-
logger.finest(MESSAGE);
332+
logger.log(newLogRecord(Level.FINEST, MESSAGE));
308333
}
309334

310335
@Test
@@ -320,7 +345,7 @@ public void testClose() throws Exception {
320345
Handler handler = new LoggingHandler(LOG_NAME, options);
321346
handler.setLevel(Level.ALL);
322347
handler.setFormatter(new TestFormatter());
323-
handler.publish(new LogRecord(Level.FINEST, MESSAGE));
348+
handler.publish(newLogRecord(Level.FINEST, MESSAGE));
324349
handler.close();
325350
handler.close();
326351
}

0 commit comments

Comments
 (0)