AutoPacker-OSS / autopacker

MIT License
6 stars 1 forks source link

Add custom log endpoint on the different backend services #90

Closed ANicholasson closed 3 years ago

ANicholasson commented 3 years ago

These endpoints need to be a spring boot actuator.

An example of the implementation of a logging endpoint actuator:

@Component
@Endpoint(id = "info-log")
public class LogEndpoint {

    private final Path infoLogPath = FileSystems.getDefault().getPath("./src/main/resources/logs", "spring.log");
    private final Charset charset = Charset.forName("UTF-8");
    private final ArrayList<String> logList;

    public LogEndpoint() {
        this.logList = new ArrayList<>();
    }

    @ReadOperation
    public List<String> getInfoLog() {
        try (BufferedReader reader = Files.newBufferedReader(infoLogPath, charset)) {
            String line;
            while (((line = reader.readLine()) != null)){
                this.logList.add(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        List<String> logListToSend;
        if (this.logList.size() > 100) {
            logListToSend = this.logList.subList(this.logList.size() - 100, this.logList.size());
        } else {
            logListToSend = this.logList;
        }
        return logListToSend;
    }

}