Skip to content

Commit

Permalink
added support for printing out logs throught the stats API endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
albogdano committed Jun 18, 2024
1 parent d31308f commit 321cd6a
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 5 deletions.
17 changes: 17 additions & 0 deletions src/main/java/com/erudika/scoold/api/ApiController.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
Expand All @@ -76,6 +79,7 @@
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
Expand Down Expand Up @@ -996,6 +1000,19 @@ public Map<String, Object> stats(HttpServletRequest req) {
stats.put("para_version", Optional.ofNullable(paraVer).orElse("unknown"));
stats.put("scoold_version", Optional.ofNullable(Version.getVersion()).
orElse(Optional.ofNullable(System.getenv("SCOOLD_VERSION")).orElse("unknown")));

if ("true".equals(req.getParameter("includeLogs"))) {
try {
String logFile = System.getProperty("para.logs_dir", System.getProperty("user.dir"))
+ "/" + System.getProperty("para.logs_name", "scoold") + ".log";
Path path = Paths.get(logFile);
try (Stream<String> lines = Files.lines(path)) {
stats.put("log", lines.collect(Collectors.joining("\n")));
}
} catch (Exception e) {
logger.error("Failed to read log file. {}", e.getMessage());
}
}
return stats;
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/erudika/scoold/utils/ScooldUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -2101,7 +2101,7 @@ public Map<String, Long> getApiKeysExpirations() {
}
}
} catch (Exception ex) {
logger.error("Failed to parse API key " + k + " - key doesn't seem to be in JWT format.", ex.getMessage());
logger.error("Failed to parse API key " + k + " - key doesn't seem to be in JWT format. {}", ex.getMessage());
}
return 0L;
}));
Expand Down
8 changes: 4 additions & 4 deletions src/main/resources/logback.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
</appender>

<appender name="ROLLING" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${para.logs_dir:-.}/${para.logs_name:-para}.log</file>
<file>${para.logs_dir:-.}/${para.logs_name:-scoold}.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- rollover monthly -->
<fileNamePattern>para-%d{yyyy-MM}.log.zip</fileNamePattern>
<fileNamePattern>${para.logs_name:-scoold}-%d{yyyy-MM}.log.zip</fileNamePattern>
<maxHistory>12</maxHistory>
<totalSizeCap>3GB</totalSizeCap>
</rollingPolicy>
Expand All @@ -29,9 +29,9 @@
</appender>

<appender name="METRICSAPPENDER" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${para.logs_dir:-.}/${para.logs_name:-para}-metrics.log</file>
<file>${para.logs_dir:-.}/${para.logs_name:-scoold}-metrics.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${para.logs_name:-para}-metrics-%d{yyyy-MM-dd}.log.zip</fileNamePattern>
<fileNamePattern>${para.logs_name:-scoold}-metrics-%d{yyyy-MM-dd}.log.zip</fileNamePattern>
<maxHistory>7</maxHistory>
<totalSizeCap>1GB</totalSizeCap>
</rollingPolicy>
Expand Down
7 changes: 7 additions & 0 deletions src/main/resources/templates/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1443,6 +1443,13 @@ paths:
- stats
security:
- scoold_auth: []
parameters:
- name: includeLogs
in: query
required: false
description: If set to `true` will read and return the Scoold log file as well.
schema:
type: boolean
responses:
'200':
description: stats
Expand Down

0 comments on commit 321cd6a

Please sign in to comment.