Skip to content

Commit

Permalink
Set timestamp from LogRecord (#1533)
Browse files Browse the repository at this point in the history
  • Loading branch information
gregw authored and garrettjonesgoogle committed Jan 18, 2017
1 parent f495c23 commit 0010dd8
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ private LogEntry entryFor(LogRecord record) {
LogEntry.Builder builder = LogEntry.newBuilder(Payload.StringPayload.of(payload))
.addLabel("levelName", level.getName())
.addLabel("levelValue", String.valueOf(level.intValue()))
.setTimestamp(record.getMillis())
.setSeverity(severityFor(level));
enhanceLogEntry(builder, record);
return builder.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,16 @@ public void testPublish() {
.setSeverity(Severity.DEBUG)
.addLabel("levelName", "FINEST")
.addLabel("levelValue", String.valueOf(Level.FINEST.intValue()))
.setTimestamp(123456789L)
.build();
EasyMock.expect(logging.writeAsync(ImmutableList.of(entry), WriteOption.logName(LOG_NAME),
WriteOption.resource(DEFAULT_RESOURCE))).andReturn(FUTURE);
EasyMock.replay(options, logging);
Handler handler = new AsyncLoggingHandler(LOG_NAME, options);
handler.setLevel(Level.ALL);
handler.setFormatter(new TestFormatter());
handler.publish(new LogRecord(Level.FINEST, MESSAGE));
LogRecord record = new LogRecord(Level.FINEST, MESSAGE);
record.setMillis(123456789L);
handler.publish(record);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,66 +44,79 @@ public class LoggingHandlerTest {
.setSeverity(Severity.DEBUG)
.addLabel("levelName", "FINEST")
.addLabel("levelValue", String.valueOf(Level.FINEST.intValue()))
.setTimestamp(123456789L)
.build();
private static final LogEntry FINER_ENTRY = LogEntry.newBuilder(StringPayload.of(MESSAGE))
.setSeverity(Severity.DEBUG)
.addLabel("levelName", "FINER")
.addLabel("levelValue", String.valueOf(Level.FINER.intValue()))
.setTimestamp(123456789L)
.build();
private static final LogEntry FINE_ENTRY = LogEntry.newBuilder(StringPayload.of(MESSAGE))
.setSeverity(Severity.DEBUG)
.addLabel("levelName", "FINE")
.addLabel("levelValue", String.valueOf(Level.FINE.intValue()))
.setTimestamp(123456789L)
.build();
private static final LogEntry CONFIG_ENTRY = LogEntry.newBuilder(StringPayload.of(MESSAGE))
.setSeverity(Severity.INFO)
.addLabel("levelName", "CONFIG")
.addLabel("levelValue", String.valueOf(Level.CONFIG.intValue()))
.setTimestamp(123456789L)
.build();
private static final LogEntry INFO_ENTRY = LogEntry.newBuilder(StringPayload.of(MESSAGE))
.setSeverity(Severity.INFO)
.addLabel("levelName", "INFO")
.addLabel("levelValue", String.valueOf(Level.INFO.intValue()))
.setTimestamp(123456789L)
.build();
private static final LogEntry WARNING_ENTRY = LogEntry.newBuilder(StringPayload.of(MESSAGE))
.setSeverity(Severity.WARNING)
.addLabel("levelName", "WARNING")
.addLabel("levelValue", String.valueOf(Level.WARNING.intValue()))
.setTimestamp(123456789L)
.build();
private static final LogEntry SEVERE_ENTRY = LogEntry.newBuilder(StringPayload.of(MESSAGE))
.setSeverity(Severity.ERROR)
.addLabel("levelName", "SEVERE")
.addLabel("levelValue", String.valueOf(Level.SEVERE.intValue()))
.setTimestamp(123456789L)
.build();
private static final LogEntry DEBUG_ENTRY = LogEntry.newBuilder(StringPayload.of(MESSAGE))
.setSeverity(Severity.DEBUG)
.addLabel("levelName", "DEBUG")
.addLabel("levelValue", String.valueOf(LoggingLevel.DEBUG.intValue()))
.setTimestamp(123456789L)
.build();
private static final LogEntry NOTICE_ENTRY = LogEntry.newBuilder(StringPayload.of(MESSAGE))
.setSeverity(Severity.NOTICE)
.addLabel("levelName", "NOTICE")
.addLabel("levelValue", String.valueOf(LoggingLevel.NOTICE.intValue()))
.setTimestamp(123456789L)
.build();
private static final LogEntry ERROR_ENTRY = LogEntry.newBuilder(StringPayload.of(MESSAGE))
.setSeverity(Severity.ERROR)
.addLabel("levelName", "ERROR")
.addLabel("levelValue", String.valueOf(LoggingLevel.ERROR.intValue()))
.setTimestamp(123456789L)
.build();
private static final LogEntry CRITICAL_ENTRY = LogEntry.newBuilder(StringPayload.of(MESSAGE))
.setSeverity(Severity.CRITICAL)
.addLabel("levelName", "CRITICAL")
.addLabel("levelValue", String.valueOf(LoggingLevel.CRITICAL.intValue()))
.setTimestamp(123456789L)
.build();
private static final LogEntry ALERT_ENTRY = LogEntry.newBuilder(StringPayload.of(MESSAGE))
.setSeverity(Severity.ALERT)
.addLabel("levelName", "ALERT")
.addLabel("levelValue", String.valueOf(LoggingLevel.ALERT.intValue()))
.setTimestamp(123456789L)
.build();
private static final LogEntry EMERGENCY_ENTRY = LogEntry.newBuilder(StringPayload.of(MESSAGE))
.setSeverity(Severity.EMERGENCY)
.addLabel("levelName", "EMERGENCY")
.addLabel("levelValue", String.valueOf(LoggingLevel.EMERGENCY.intValue()))
.setTimestamp(123456789L)
.build();

private Logging logging;
Expand All @@ -127,6 +140,13 @@ public void setUp() {
public void afterClass() {
EasyMock.verify(logging, options);
}


private static LogRecord newLogRecord(Level level, String message) {
LogRecord record = new LogRecord(level, message);
record.setMillis(123456789L);
return record;
}

@Test
public void testPublishLevels() {
Expand Down Expand Up @@ -176,20 +196,20 @@ public void testPublishLevels() {
handler.setLevel(Level.ALL);
handler.setFormatter(new TestFormatter());
// default levels
handler.publish(new LogRecord(Level.FINEST, MESSAGE));
handler.publish(new LogRecord(Level.FINER, MESSAGE));
handler.publish(new LogRecord(Level.FINE, MESSAGE));
handler.publish(new LogRecord(Level.CONFIG, MESSAGE));
handler.publish(new LogRecord(Level.INFO, MESSAGE));
handler.publish(new LogRecord(Level.WARNING, MESSAGE));
handler.publish(new LogRecord(Level.SEVERE, MESSAGE));
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));
handler.publish(newLogRecord(Level.SEVERE, MESSAGE));
// Logging levels
handler.publish(new LogRecord(LoggingLevel.DEBUG, MESSAGE));
handler.publish(new LogRecord(LoggingLevel.NOTICE, MESSAGE));
handler.publish(new LogRecord(LoggingLevel.ERROR, MESSAGE));
handler.publish(new LogRecord(LoggingLevel.CRITICAL, MESSAGE));
handler.publish(new LogRecord(LoggingLevel.ALERT, MESSAGE));
handler.publish(new LogRecord(LoggingLevel.EMERGENCY, MESSAGE));
handler.publish(newLogRecord(LoggingLevel.DEBUG, MESSAGE));
handler.publish(newLogRecord(LoggingLevel.NOTICE, MESSAGE));
handler.publish(newLogRecord(LoggingLevel.ERROR, MESSAGE));
handler.publish(newLogRecord(LoggingLevel.CRITICAL, MESSAGE));
handler.publish(newLogRecord(LoggingLevel.ALERT, MESSAGE));
handler.publish(newLogRecord(LoggingLevel.EMERGENCY, MESSAGE));
}

@Test
Expand All @@ -204,7 +224,7 @@ public void testPublishCustomResource() {
Handler handler = new LoggingHandler(LOG_NAME, options, resource);
handler.setLevel(Level.ALL);
handler.setFormatter(new TestFormatter());
handler.publish(new LogRecord(Level.FINEST, MESSAGE));
handler.publish(newLogRecord(Level.FINEST, MESSAGE));
}

@Test
Expand All @@ -224,7 +244,7 @@ public void testReportFlushError() {
handler.setLevel(Level.ALL);
handler.setErrorManager(errorManager);
handler.setFormatter(new TestFormatter());
handler.publish(new LogRecord(Level.FINEST, MESSAGE));
handler.publish(newLogRecord(Level.FINEST, MESSAGE));
EasyMock.verify(errorManager);
}

Expand All @@ -237,7 +257,7 @@ public void testReportFormatError() {
ErrorManager errorManager = EasyMock.createStrictMock(ErrorManager.class);
errorManager.error(null, ex, ErrorManager.FORMAT_FAILURE);
EasyMock.expectLastCall().once();
LogRecord record = new LogRecord(Level.FINEST, MESSAGE);
LogRecord record = newLogRecord(Level.FINEST, MESSAGE);
EasyMock.expect(formatter.format(record)).andThrow(ex);
EasyMock.replay(errorManager, formatter);
Handler handler = new LoggingHandler(LOG_NAME, options);
Expand All @@ -260,12 +280,12 @@ public void testFlushSize() {
handler.setLevel(Level.ALL);
handler.setFlushSize(6);
handler.setFormatter(new TestFormatter());
handler.publish(new LogRecord(Level.FINEST, MESSAGE));
handler.publish(new LogRecord(Level.FINER, MESSAGE));
handler.publish(new LogRecord(Level.FINE, MESSAGE));
handler.publish(new LogRecord(Level.CONFIG, MESSAGE));
handler.publish(new LogRecord(Level.INFO, MESSAGE));
handler.publish(new LogRecord(Level.WARNING, MESSAGE));
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
Expand All @@ -282,12 +302,12 @@ public void testFlushLevel() {
handler.setFlushSize(100);
handler.setFlushLevel(Level.WARNING);
handler.setFormatter(new TestFormatter());
handler.publish(new LogRecord(Level.FINEST, MESSAGE));
handler.publish(new LogRecord(Level.FINER, MESSAGE));
handler.publish(new LogRecord(Level.FINE, MESSAGE));
handler.publish(new LogRecord(Level.CONFIG, MESSAGE));
handler.publish(new LogRecord(Level.INFO, MESSAGE));
handler.publish(new LogRecord(Level.WARNING, MESSAGE));
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
Expand All @@ -298,13 +318,18 @@ public void testAddHandler() {
WriteOption.resource(DEFAULT_RESOURCE));
EasyMock.expectLastCall().andReturn(Futures.immediateFuture(null));
EasyMock.replay(options, logging);
LoggingHandler handler = new LoggingHandler(LOG_NAME, options);
LoggingHandler handler = new LoggingHandler(LOG_NAME, options) {
@Override
public void close() {
// Make close NOOP to avoid mock close exception
}
};
handler.setLevel(Level.ALL);
handler.setFormatter(new TestFormatter());
Logger logger = Logger.getLogger(getClass().getName());
logger.setLevel(Level.ALL);
LoggingHandler.addHandler(logger, handler);
logger.finest(MESSAGE);
logger.log(newLogRecord(Level.FINEST, MESSAGE));
}

@Test
Expand All @@ -320,7 +345,7 @@ public void testClose() throws Exception {
Handler handler = new LoggingHandler(LOG_NAME, options);
handler.setLevel(Level.ALL);
handler.setFormatter(new TestFormatter());
handler.publish(new LogRecord(Level.FINEST, MESSAGE));
handler.publish(newLogRecord(Level.FINEST, MESSAGE));
handler.close();
handler.close();
}
Expand Down

0 comments on commit 0010dd8

Please sign in to comment.