LogLevelExtractor { public static Result extract(String logLine) { Matcher m = LOG_PATTERN.matcher(logLine); if (!m.find()) { return null; } String logLevel = m.group(1); String logTimestamp = m.group(2) + SPACE + m.group(3) + SPACE + m.group(4); LocalDateTime timestamp = LocalDateTime.parse(logTimestamp, LOG_TIMESTAMP_FORMATTER); return new Result(logLevel, timestamp); }  static Result extract(String logLine);  }
@Test public void extractWorksCorrectlyForLineFromLogFile() { String logLine = "[info 2017/02/07 11:16:36.694 PST locator1 <locator request thread[1]> tid=0x27] Mapped \"{[/v1/async-event-queues],methods=[GET]}\" onto public java.lang.String"; LogLevelExtractor.Result result = LogLevelExtractor.extract(logLine); assertThat(result).isNotNull(); assertThat(result.getLogLevel()).isEqualTo(Level.INFO); assertThat(result.getLogTimestamp().toString()).isEqualTo("2017-02-07T11:16:36.694"); }
@Test public void extractWorksForFine() { String logLine = "[fine 2017/02/07 11:16:36.694 PST locator1 <locator request thread[1]> tid=0x27] Mapped \"{[/v1/async-event-queues],methods=[GET]}\" onto public java.lang.String"; LogLevelExtractor.Result result = LogLevelExtractor.extract(logLine); assertThat(result).isNotNull(); assertThat(result.getLogLevel()).isEqualTo(Level.DEBUG); assertThat(result.getLogTimestamp().toString()).isEqualTo("2017-02-07T11:16:36.694"); }
@Test public void extractWorksForFiner() { String logLine = "[finer 2017/02/07 11:16:36.694 PST locator1 <locator request thread[1]> tid=0x27] Mapped \"{[/v1/async-event-queues],methods=[GET]}\" onto public java.lang.String"; LogLevelExtractor.Result result = LogLevelExtractor.extract(logLine); assertThat(result).isNotNull(); assertThat(result.getLogLevel()).isEqualTo(Level.TRACE); assertThat(result.getLogTimestamp().toString()).isEqualTo("2017-02-07T11:16:36.694"); }
@Test public void extractWorksForFinest() { String logLine = "[finest 2017/02/07 11:16:36.694 PST locator1 <locator request thread[1]> tid=0x27] Mapped \"{[/v1/async-event-queues],methods=[GET]}\" onto public java.lang.String"; LogLevelExtractor.Result result = LogLevelExtractor.extract(logLine); assertThat(result).isNotNull(); assertThat(result.getLogLevel()).isEqualTo(Level.TRACE); assertThat(result.getLogTimestamp().toString()).isEqualTo("2017-02-07T11:16:36.694"); }
@Test public void extractReturnsNullIfNoTimestamp() { String logLine = "[info (this line is not a valid log statement since it has no timestamp)"; LogLevelExtractor.Result result = LogLevelExtractor.extract(logLine); assertThat(result).isNull(); }
@Test public void extractReturnsNullIfLineDoesNotMatchPattern() { String logLine = "some line containing a date like 2017/02/07 11:16:36.694 PST "; LogLevelExtractor.Result result = LogLevelExtractor.extract(logLine); assertThat(result).isNull(); }