diff --git a/lib/lib.gradle b/lib/lib.gradle index 41243bc..d0c3989 100644 --- a/lib/lib.gradle +++ b/lib/lib.gradle @@ -36,6 +36,7 @@ dependencies { compile("org.ocpsoft.prettytime:prettytime:3.2.7.Final") compile("commons-io:commons-io:2.4") compile("org.apache.commons:commons-compress:1.9") + compile("com.codepoetics:protonpack:1.4") testCompile("org.springframework.boot:spring-boot-starter-test:${springBootVersion}") } diff --git a/lib/src/main/java/eu/hinsch/spring/boot/actuator/logview/FileSystemFileProvider.java b/lib/src/main/java/eu/hinsch/spring/boot/actuator/logview/FileSystemFileProvider.java index 3173890..ba7f18c 100644 --- a/lib/src/main/java/eu/hinsch/spring/boot/actuator/logview/FileSystemFileProvider.java +++ b/lib/src/main/java/eu/hinsch/spring/boot/actuator/logview/FileSystemFileProvider.java @@ -1,5 +1,6 @@ package eu.hinsch.spring.boot.actuator.logview; +import com.codepoetics.protonpack.StreamUtils; import org.apache.commons.io.IOUtils; import org.apache.commons.io.input.ReversedLinesFileReader; @@ -11,9 +12,10 @@ import java.nio.file.Path; import java.nio.file.Paths; import java.util.ArrayList; -import java.util.Collections; import java.util.Date; +import java.util.LinkedList; import java.util.List; +import java.util.stream.Stream; /** * Created by lh on 28/02/15. @@ -74,14 +76,20 @@ private File getFile(Path folder, String filename) { @Override public void tailContent(Path folder, String filename, OutputStream stream, int lines) throws IOException { try (ReversedLinesFileReader reader = new ReversedLinesFileReader(getFile(folder, filename))) { - int i = 0; - String line; - List content = new ArrayList<>(); - while ((line = reader.readLine()) != null && i++ < lines) { - content.add(line); - } - Collections.reverse(content); + List content = StreamUtils.takeWhile( + Stream.generate(() -> readLine(reader)), + line -> line != null) + .limit(lines) + .collect(LinkedList::new, LinkedList::addFirst, LinkedList::addAll); IOUtils.writeLines(content, System.lineSeparator(), stream); } } + + private String readLine(ReversedLinesFileReader reader) { + try { + return reader.readLine(); + } catch (IOException e) { + throw new RuntimeException("cannot read line", e); + } + } } diff --git a/lib/src/test/java/eu/hinsch/spring/boot/actuator/logview/LogViewEndpointTest.java b/lib/src/test/java/eu/hinsch/spring/boot/actuator/logview/LogViewEndpointTest.java index bd76530..a16d3e4 100644 --- a/lib/src/test/java/eu/hinsch/spring/boot/actuator/logview/LogViewEndpointTest.java +++ b/lib/src/test/java/eu/hinsch/spring/boot/actuator/logview/LogViewEndpointTest.java @@ -362,6 +362,20 @@ public void shouldTailViewOnlyLastLine() throws Exception { assertThat(new String(outputStream.toByteArray()), containsString("line2")); } + @Test + public void shouldTailHandleMoreRequestedLinesThanExist() throws Exception { + // given + createFile("file.log", "line1" + System.lineSeparator() + "line2" + System.lineSeparator(), now); + ByteArrayServletOutputStream outputStream = mockResponseOutputStream(); + + // when + logViewEndpoint.view("file.log", null, 3, response); + + // then + assertThat(new String(outputStream.toByteArray()), containsString("line1")); + assertThat(new String(outputStream.toByteArray()), containsString("line2")); + } + @Test public void shouldSearchInFiles() throws Exception { // given