Skip to content

Commit

Permalink
feat: only show the last 10 mb of large log files and prepend it with…
Browse files Browse the repository at this point in the history
… a message if content was trunated
  • Loading branch information
mburri committed Jun 28, 2024
1 parent c6e9527 commit cce68c2
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -800,38 +800,29 @@ public String[] getLogFileNames(final long deploymentId) {
}
}

// TODO test
public String getDeploymentLog(String logName) throws IOException {
public String readContentOfDeploymentLog(String logFilename, long maxSize) throws IOException {
String logsPath = ConfigurationService.getProperty(ConfigKey.LOGS_PATH);

String name = logsPath + File.separator + logName;
String name = logsPath + File.separator + logFilename;
File file = new File(name);

if (file.length() > 1_000_000) throw new IOException(String.format("%s is larger than 10 MB and cannot be shown.", file.getName()));
long fileSize = file.length();
long startPosition = fileSize > maxSize ? fileSize - maxSize : 0;

StringBuilder content = new StringBuilder();
Scanner scanner;
try {
try (RandomAccessFile raf = new RandomAccessFile(file, "r")) {
raf.seek(startPosition);

scanner = new Scanner(new FileInputStream(file));
try {
while (scanner.hasNextLine()) {
content.append(scanner.nextLine()).append('\n');
}
} finally {
scanner.close();
StringBuilder content = new StringBuilder();
if (startPosition > 0 ) {
content.append("[INFO] Log Truncation Notice: The beginning of this log has been truncated because the log file exceeded the maximum allowable size. Some earlier entries are missing.\n\n");
content.append("\n...\n");
}
String line;
while ((line = raf.readLine()) != null) {
content.append(line).append("\n");
}

return content.toString();
} catch (FileNotFoundException e) {
String message = "The file "
+ logsPath
+ File.separator
+ logName
+ " was found, but couldn't be read!";
log.log(Level.WARNING, message);
throw new AMWRuntimeException(message, e);
}

}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
@Stateless
public class LogContentService implements DeploymentLogContentUseCase {

public final static int MAX_FILE_SIZE = 10_000_000;

@Inject
private DeploymentBoundary deploymentBoundary;

Expand All @@ -23,6 +25,6 @@ public DeploymentLog getContent(DeploymentLogContentCommand command) throws Vali

return new DeploymentLog(command.getId(),
command.getFilename(),
deploymentBoundary.getDeploymentLog(command.getFilename()));
deploymentBoundary.readContentOfDeploymentLog(command.getFilename(), MAX_FILE_SIZE));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import java.io.IOException;

import static ch.puzzle.itc.mobiliar.business.deploy.control.LogContentService.MAX_FILE_SIZE;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.fail;
Expand Down Expand Up @@ -43,7 +44,7 @@ public void should_throw_exception_when_log_file_not_found() throws ValidationEx
public void should_get_content_of_log_file() throws ValidationException, IOException {
// given
doReturn(new String[]{"file-name.log"}).when(deploymentBoundary).getLogFileNames(12345);
doReturn("log-file-content").when(deploymentBoundary).getDeploymentLog("file-name.log");
doReturn("log-file-content").when(deploymentBoundary).readContentOfDeploymentLog("file-name.log", MAX_FILE_SIZE);

// when
DeploymentLog content = service.getContent(new DeploymentLogContentCommand(12345, "file-name.log"));
Expand Down

0 comments on commit cce68c2

Please sign in to comment.