From 0010dd8ed35c605013aec8c95924c4855df7e1ed Mon Sep 17 00:00:00 2001 From: Greg Wilkins Date: Thu, 19 Jan 2017 04:18:05 +1100 Subject: [PATCH] Set timestamp from LogRecord (#1533) --- .../google/cloud/logging/LoggingHandler.java | 1 + .../logging/AsyncLoggingHandlerTest.java | 5 +- .../cloud/logging/LoggingHandlerTest.java | 87 ++++++++++++------- 3 files changed, 61 insertions(+), 32 deletions(-) diff --git a/google-cloud-logging/src/main/java/com/google/cloud/logging/LoggingHandler.java b/google-cloud-logging/src/main/java/com/google/cloud/logging/LoggingHandler.java index 3bdc301c624a..d497f6e1b70b 100644 --- a/google-cloud-logging/src/main/java/com/google/cloud/logging/LoggingHandler.java +++ b/google-cloud-logging/src/main/java/com/google/cloud/logging/LoggingHandler.java @@ -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(); diff --git a/google-cloud-logging/src/test/java/com/google/cloud/logging/AsyncLoggingHandlerTest.java b/google-cloud-logging/src/test/java/com/google/cloud/logging/AsyncLoggingHandlerTest.java index 495430c23d75..8ea02628309f 100644 --- a/google-cloud-logging/src/test/java/com/google/cloud/logging/AsyncLoggingHandlerTest.java +++ b/google-cloud-logging/src/test/java/com/google/cloud/logging/AsyncLoggingHandlerTest.java @@ -65,6 +65,7 @@ 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); @@ -72,6 +73,8 @@ public void testPublish() { 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); } } diff --git a/google-cloud-logging/src/test/java/com/google/cloud/logging/LoggingHandlerTest.java b/google-cloud-logging/src/test/java/com/google/cloud/logging/LoggingHandlerTest.java index e48b2051cd5d..a1c1697d18af 100644 --- a/google-cloud-logging/src/test/java/com/google/cloud/logging/LoggingHandlerTest.java +++ b/google-cloud-logging/src/test/java/com/google/cloud/logging/LoggingHandlerTest.java @@ -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; @@ -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() { @@ -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 @@ -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 @@ -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); } @@ -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); @@ -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 @@ -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 @@ -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 @@ -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(); }