Skip to content

Commit

Permalink
Merge branch 'master' of github.com:lukashinsch/spring-boot-actuator-…
Browse files Browse the repository at this point in the history
…logview
  • Loading branch information
lukashinsch committed Mar 28, 2015
2 parents 81f828b + d2be290 commit d693556
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 12 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# spring-boot-actuator-logview changelog

## 0.2.5
- allow downloading only the last 50 lines of each file (closes [#13](https://github.com/lukashinsch/spring-boot-actuator-logview/pull/13))

## 0.2.4
- show file content when calling endpoint url without trailing slash (fixes [#11](https://github.com/lukashinsch/spring-boot-actuator-logview/issues/11))
- allow to specify logging path via "endpoints.logview.path" property (fixes [#3](https://github.com/lukashinsch/spring-boot-actuator-logview/issues/3)
Expand Down
2 changes: 1 addition & 1 deletion lib/lib.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ apply plugin: 'com.github.kt3k.coveralls'

ext {
springBootVersion = '1.2.1.RELEASE'
libVersion = '0.2.4'
libVersion = '0.2.5'
}

jar {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,14 @@
* Created by lh on 28/02/15.
*/
public interface FileProvider {

boolean canHandle(Path folder);

List<FileEntry> getFileEntries(Path folder) throws IOException;

void streamContent(Path folder, String filename, OutputStream stream) throws IOException;

default void tailContent(Path folder, String filename, OutputStream stream, int lines) throws IOException {
throw new UnsupportedOperationException("by default no tailing possible");
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package eu.hinsch.spring.boot.actuator.logview;

import org.apache.commons.io.IOUtils;
import org.apache.commons.io.input.ReversedLinesFileReader;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
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.List;

Expand All @@ -31,7 +34,7 @@ public List<FileEntry> getFileEntries(Path loggingPath) throws IOException {
}

private FileEntry createFileEntry(Path path) {
FileEntry fileEntry = new FileEntry();
final FileEntry fileEntry = new FileEntry();
fileEntry.setFilename(path.getFileName().toString());
try {
fileEntry.setModified(Files.getLastModifiedTime(path));
Expand Down Expand Up @@ -61,6 +64,24 @@ else if (isArchive(path)) {

@Override
public void streamContent(Path folder, String filename, OutputStream stream) throws IOException {
IOUtils.copy(new FileInputStream(Paths.get(folder.toString(), filename).toFile()), stream);
IOUtils.copy(new FileInputStream(getFile(folder, filename)), stream);
}

private File getFile(Path folder, String filename) {
return Paths.get(folder.toString(), filename).toFile();
}

@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<String> content = new ArrayList<>();
while ((line = reader.readLine()) != null && i++ < lines) {
content.add(line);
}
Collections.reverse(content);
IOUtils.writeLines(content, System.lineSeparator(), stream);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,19 @@ private List<FileEntry> sortFiles(List<FileEntry> files, SortBy sortBy, boolean
}

@RequestMapping("/view")
public void view(@RequestParam String filename, @RequestParam(required = false) String base, HttpServletResponse response) throws IOException {
public void view(@RequestParam String filename,
@RequestParam(required = false) String base,
@RequestParam(required = false) Integer tailLines,
HttpServletResponse response) throws IOException {
securityCheck(filename);
Path path = loggingPath(base);
getFileProvider(path).streamContent(path, filename, response.getOutputStream());
FileProvider fileProvider = getFileProvider(path);
if (tailLines != null) {
fileProvider.tailContent(path, filename, response.getOutputStream(), tailLines);
}
else {
fileProvider.streamContent(path, filename, response.getOutputStream());
}
}

@RequestMapping("/search")
Expand Down
3 changes: 2 additions & 1 deletion lib/src/main/resources/templates/logview.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@
</#if>
&nbsp;
<#if file.fileType == 'FILE'>
<a href="view?filename=${file.filename}&base=${base}">${file.filename}</a>
<a href="view?filename=${file.filename}&base=${base}">${file.filename}</a>&nbsp;
<a href="view?filename=${file.filename}&base=${base}&tailLines=50" title="Download last 50 lines"><i class="fa fa-angle-double-down"></i></a>
</#if>
<#if file.fileType == 'ARCHIVE'>
<a href="?base=${base}/${file.filename}">${file.filename}</a>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ public void shouldViewZipFileContent() throws Exception {
ByteArrayServletOutputStream outputStream = mockResponseOutputStream();

// when
logViewEndpoint.view("A.log", "file.zip", response);
logViewEndpoint.view("A.log", "file.zip", null, response);

// then
assertThat(new String(outputStream.toByteArray()), is("content"));
Expand All @@ -236,6 +236,17 @@ private void createZipArchive(String archiveFileName, String contentFileName, St
}
}

@Test(expected = UnsupportedOperationException.class)
public void shouldThrowExceptionWhenCallingTailForZip() throws Exception {
// given
createZipArchive("file.zip", "A.log", "content");

// when
logViewEndpoint.view("A.log", "file.zip", 1, response);

// then -> exception
}

@Test
public void shouldListTarGzContent() throws Exception {
// given
Expand All @@ -258,12 +269,23 @@ public void shouldViewTarGzFileContent() throws Exception {
ByteArrayServletOutputStream outputStream = mockResponseOutputStream();

// when
logViewEndpoint.view("A.log", "file.tar.gz", response);
logViewEndpoint.view("A.log", "file.tar.gz", null, response);

// then
assertThat(new String(outputStream.toByteArray()), is("content"));
}

@Test(expected = UnsupportedOperationException.class)
public void shouldThrowExceptionWhenCallingTailForTarGz() throws Exception {
// given
createTarGzArchive("file.tar.gz", "A.log", "content");

// when
logViewEndpoint.view("A.log", "file.tar.gz", 1, response);

// then -> exception
}

private void createTarGzArchive(String archiveFileName, String contentFileName, String content) throws Exception {

try(TarArchiveOutputStream tos = new TarArchiveOutputStream(new GZIPOutputStream(
Expand Down Expand Up @@ -310,7 +332,7 @@ public void shouldNotAllowToListFileOutsideRoot() throws Exception {
expectedException.expectMessage(containsString("this String argument must not contain the substring [..]"));

// when
logViewEndpoint.view("../somefile", null, null);
logViewEndpoint.view("../somefile", null, null, null);
}

@Test
Expand All @@ -320,12 +342,26 @@ public void shouldViewFile() throws Exception {
ByteArrayServletOutputStream outputStream = mockResponseOutputStream();

// when
logViewEndpoint.view("file.log", null, response);
logViewEndpoint.view("file.log", null, null, response);

// then
assertThat(new String(outputStream.toByteArray()), is("abc"));
}

@Test
public void shouldTailViewOnlyLastLine() throws Exception {
// given
createFile("file.log", "line1" + System.lineSeparator() + "line2" + System.lineSeparator(), now);
ByteArrayServletOutputStream outputStream = mockResponseOutputStream();

// when
logViewEndpoint.view("file.log", null, 1, response);

// then
assertThat(new String(outputStream.toByteArray()), not(containsString("line1")));
assertThat(new String(outputStream.toByteArray()), containsString("line2"));
}

@Test
public void shouldSearchInFiles() throws Exception {
// given
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
management.context-path=/manage
#logging.path=/tmp/log
endpoints.logview.path=/tmp/log
logging.path=/tmp/log
#endpoints.logview.path=/tmp/log

0 comments on commit d693556

Please sign in to comment.