Skip to content

Commit

Permalink
Merge pull request #292 from seart-group/feature/logfiles
Browse files Browse the repository at this point in the history
  • Loading branch information
dabico authored Jan 24, 2024
2 parents ea1f2a0 + 9cd1302 commit 5b51c46
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package ch.usi.si.seart.actuate.logging;

import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.experimental.FieldDefaults;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.boot.actuate.endpoint.annotation.Selector;
import org.springframework.boot.actuate.endpoint.web.annotation.EndpointWebExtension;
import org.springframework.boot.actuate.logging.LogFileWebEndpoint;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.http.ResponseEntity;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

@AllArgsConstructor
@FieldDefaults(level = AccessLevel.PRIVATE, makeFinal = true)
@EndpointWebExtension(endpoint = LogFileWebEndpoint.class)
public class LogFileWebEndpointExtension {

private static final String LOG_ROOT = "logs";

LogFileWebEndpoint delegate;

@ReadOperation(produces = "text/plain; charset=UTF-8")
public ResponseEntity<Resource> logFile(@Selector(match = Selector.Match.ALL_REMAINING) String... segments) {
if (segments.length == 0) return ResponseEntity.ok(delegate.logFile());
Path path = Paths.get(LOG_ROOT, segments);
if (Files.notExists(path)) return ResponseEntity.notFound().build();
File file = Files.isRegularFile(path) ? path.toFile() : getDirectoryLogFile(path);
if (!file.exists()) return ResponseEntity.notFound().build();
Resource resource = new FileSystemResource(file);
return ResponseEntity.ok(resource);
}

private File getDirectoryLogFile(Path path) {
String directory = path.getFileName().toString();
String file = directory + ".log";
return Paths.get(path.toString(), file).toFile();
}
}
11 changes: 11 additions & 0 deletions src/main/java/ch/usi/si/seart/config/ActuatorConfig.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package ch.usi.si.seart.config;

import ch.usi.si.seart.actuate.info.SpringInfoContributor;
import ch.usi.si.seart.actuate.logging.LogFileWebEndpointExtension;
import ch.usi.si.seart.github.GitHubAPIHealthIndicator;
import org.springframework.boot.actuate.autoconfigure.endpoint.condition.ConditionalOnAvailableEndpoint;
import org.springframework.boot.actuate.autoconfigure.info.ConditionalOnEnabledInfoContributor;
import org.springframework.boot.actuate.autoconfigure.info.InfoContributorAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.info.InfoContributorFallback;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.boot.actuate.info.InfoContributor;
import org.springframework.boot.actuate.logging.LogFileWebEndpoint;
import org.springframework.boot.actuate.trace.http.HttpTraceRepository;
import org.springframework.boot.actuate.trace.http.InMemoryHttpTraceRepository;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
Expand All @@ -32,4 +36,11 @@ public HealthIndicator gitHubApiHealthIndicator() {
public InfoContributor applicationInfoContributor() {
return new SpringInfoContributor();
}

@Bean
@ConditionalOnMissingBean
@ConditionalOnAvailableEndpoint
public LogFileWebEndpointExtension logFileWebEndpointExtension(LogFileWebEndpoint delegate) {
return new LogFileWebEndpointExtension(delegate);
}
}

0 comments on commit 5b51c46

Please sign in to comment.