-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #292 from seart-group/feature/logfiles
- Loading branch information
Showing
2 changed files
with
55 additions
and
0 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
src/main/java/ch/usi/si/seart/actuate/logging/LogFileWebEndpointExtension.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters