Skip to content

Commit 918186c

Browse files
committed
New logging formats and policies
1 parent 7123f8a commit 918186c

File tree

4 files changed

+56
-14
lines changed

4 files changed

+56
-14
lines changed

src/main/java/xyz/srnyx/lazylibrary/LazyLibrary.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public LazyLibrary() {
9494
settings.builder.accept(builder);
9595
builder.build(jda);
9696

97-
// stop command
97+
// Console commands
9898
new Thread(() -> {
9999
final Scanner scanner = new Scanner(System.in);
100100
while (scanner.hasNextLine()) {
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package xyz.srnyx.lazylibrary.logging;
2+
3+
import ch.qos.logback.core.rolling.FixedWindowRollingPolicy;
4+
5+
import org.jetbrains.annotations.NotNull;
6+
7+
import java.time.OffsetDateTime;
8+
import java.time.ZoneId;
9+
import java.time.format.DateTimeFormatter;
10+
import java.util.regex.Pattern;
11+
12+
13+
// https://stackoverflow.com/a/72377192/18122334
14+
public class FixedWindowWithTimeRollingPolicy extends FixedWindowRollingPolicy {
15+
@NotNull private static final ZoneId ZONE_ID = ZoneId.of("America/New_York");
16+
@NotNull private static final Pattern DATE_PATTERN = Pattern.compile("%d\\{([^}]+)}");
17+
18+
@Override
19+
public void setFileNamePattern(String fnp) {
20+
// Replace %d{FORMAT} with DateTimeFormatter.ofPattern(FORMAT)
21+
final OffsetDateTime now = OffsetDateTime.now(ZONE_ID);
22+
super.setFileNamePattern(DATE_PATTERN.matcher(fnp).replaceAll(m -> DateTimeFormatter.ofPattern(m.group(1)).format(now)));
23+
}
24+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package xyz.srnyx.lazylibrary.logging;
2+
3+
import ch.qos.logback.core.rolling.TriggeringPolicyBase;
4+
5+
import java.io.File;
6+
7+
8+
// https://stackoverflow.com/a/32203808/18122334
9+
public class StartupBasedTriggeringPolicy<T> extends TriggeringPolicyBase<T> {
10+
private boolean doRolling = true;
11+
12+
@Override
13+
public boolean isTriggeringEvent(File activeFile, T event) {
14+
// Roll first time when event gets called
15+
if (doRolling) {
16+
doRolling = false;
17+
return true;
18+
}
19+
return false;
20+
}
21+
}

src/main/resources/logback.xml

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,25 @@
1-
<?xml version="1.0" encoding="UTF-8"?>
21
<configuration>
3-
<timestamp key="bySecond" datePattern="yyyyMMdd'T'HHmmss"/>
2+
<property name="TIMESTAMP" value="%d{hh:mm:ss.SSS a}"/>
3+
<property name="LOGS_PATH" value="logs/"/>
44

55
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
66
<encoder>
7-
<!-- Debug pattern -->
8-
<!--<pattern>%d{HH:mm:ss.SSS} %boldCyan(%-26.-26thread) %boldRed(%-36.-36class{36}) %boldRed(#%-24.-24method{24}) %boldRed(L%-5.-5line) %boldYellow(%-20.-20logger{0}) %highlight(%-6level) %msg%n%throwable</pattern>-->
9-
<!-- Normal pattern, no stack frames -->
10-
<pattern>%d{HH:mm:ss.SSS} %highlight(%-6level) %boldCyan(%-20.-20thread) %boldYellow(%-20.-20logger{0}) %msg%n%throwable</pattern>
7+
<pattern>${TIMESTAMP} %highlight(%-6level) %boldYellow(%-15.-15logger{0}) %boldCyan(%-30.-30class{30}) %msg%n%throwable</pattern>
118
</encoder>
129
</appender>
1310

1411
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
15-
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
16-
<!-- daily rollover -->
17-
<fileNamePattern>logs/logs-%d{yyyy-MM-dd}.log</fileNamePattern>
12+
<file>${LOG_PATH}bot.log</file>
1813

19-
<!-- keep 90 days' worth of history capped at 3GB total size -->
20-
<maxHistory>90</maxHistory>
21-
<totalSizeCap>3GB</totalSizeCap>
14+
<rollingPolicy class="xyz.srnyx.lazylibrary.logging.FixedWindowWithTimeRollingPolicy">
15+
<fileNamePattern>${LOG_PATH}bot_%d{yyyy-MM-dd_HH-mm}_%i.log</fileNamePattern>
16+
<maxIndex>10</maxIndex>
2217
</rollingPolicy>
2318

19+
<triggeringPolicy class="xyz.srnyx.lazylibrary.logging.StartupBasedTriggeringPolicy"/>
20+
2421
<encoder>
25-
<pattern>%d{HH:mm:ss.SSS} %-26.-26thread %-36.-36class{36} #%-24.-24method{24} L%-5.-5line %-20.-20logger{0} %-6level %msg%n%throwable</pattern>
22+
<pattern>${TIMESTAMP} [%-6level] [%-15.-15logger{0}] [%-30.-30class{30}] %msg%n%throwable</pattern>
2623
</encoder>
2724
</appender>
2825

0 commit comments

Comments
 (0)