fix: (maybe) ACTUALLY fix file logging (for the 69th time)
This commit is contained in:
parent
f561a9c810
commit
597a694727
1 changed files with 25 additions and 35 deletions
|
@ -2,10 +2,7 @@ package me.chayapak1.chomens_bot.util;
|
||||||
|
|
||||||
import me.chayapak1.chomens_bot.Main;
|
import me.chayapak1.chomens_bot.Main;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.*;
|
||||||
import java.io.BufferedWriter;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStream;
|
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
|
@ -18,12 +15,13 @@ import java.util.concurrent.ScheduledExecutorService;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.zip.GZIPOutputStream;
|
import java.util.zip.GZIPOutputStream;
|
||||||
|
|
||||||
// totallynotskidded™ from HBot
|
// original source code from hhhzzzsss, specifically HBot.
|
||||||
|
// source: https://github.com/hhhzzzsss/HBot-Release/blob/main/src/main/java/com/github/hhhzzzsss/hbot/Logger.java
|
||||||
public class FileLoggerUtilities {
|
public class FileLoggerUtilities {
|
||||||
public static final Path logDirectory = Path.of("logs");
|
public static final Path logDirectory = Path.of("logs");
|
||||||
public static final Path logPath = Paths.get(logDirectory.toString(), "log.txt");
|
public static final Path logPath = Paths.get(logDirectory.toString(), "log.txt");
|
||||||
|
|
||||||
public static BufferedWriter logWriter;
|
public static OutputStreamWriter logWriter;
|
||||||
|
|
||||||
public static LocalDate currentLogDate;
|
public static LocalDate currentLogDate;
|
||||||
public static final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("'['dd/MM/yyyy HH:mm:ss']' ");
|
public static final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("'['dd/MM/yyyy HH:mm:ss']' ");
|
||||||
|
@ -79,56 +77,49 @@ public class FileLoggerUtilities {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void makeNewLogFile() throws IOException {
|
public static synchronized void makeNewLogFile() throws IOException {
|
||||||
currentLogDate = LocalDate.now();
|
currentLogDate = LocalDate.now();
|
||||||
|
logWriter = new OutputStreamWriter(Files.newOutputStream(logPath, StandardOpenOption.CREATE, StandardOpenOption.APPEND), StandardCharsets.UTF_8);
|
||||||
if (!Files.exists(logPath)) Files.createFile(logPath);
|
|
||||||
|
|
||||||
logWriter = Files.newBufferedWriter(logPath, StandardCharsets.UTF_16, StandardOpenOption.TRUNCATE_EXISTING);
|
|
||||||
logWriter.write(currentLogDate.toString() + '\n');
|
logWriter.write(currentLogDate.toString() + '\n');
|
||||||
logWriter.flush();
|
logWriter.flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void openLogFile() throws IOException {
|
public static synchronized void openLogFile() throws IOException {
|
||||||
currentLogDate = LocalDate.parse(getLogDate(logPath));
|
currentLogDate = LocalDate.parse(getLogDate(logPath));
|
||||||
logWriter = Files.newBufferedWriter(logPath, StandardCharsets.UTF_16, StandardOpenOption.APPEND);
|
logWriter = new OutputStreamWriter(Files.newOutputStream(logPath, StandardOpenOption.CREATE, StandardOpenOption.APPEND), StandardCharsets.UTF_8);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void compressLogFile() throws IOException {
|
public static synchronized void compressLogFile() throws IOException {
|
||||||
if (Files.size(logPath) > 100 * 1024 * 1024) { // Will not save because log file is too big
|
if (Files.size(logPath) > 100 * 1024 * 1024) { // Will not save because log file is too big
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
final Path path = Paths.get(logDirectory.toString(), getLogDate(logPath) + ".txt.gz");
|
final Path path = Paths.get(logDirectory.toString(), getLogDate(logPath) + ".txt.gz");
|
||||||
|
|
||||||
Files.createFile(path);
|
try (
|
||||||
|
final InputStream in = Files.newInputStream(logPath, StandardOpenOption.READ);
|
||||||
InputStream in = Files.newInputStream(logPath);
|
final GZIPOutputStream out = new GZIPOutputStream(Files.newOutputStream(path, StandardOpenOption.CREATE))
|
||||||
GZIPOutputStream out = new GZIPOutputStream(Files.newOutputStream(path));
|
) {
|
||||||
|
|
||||||
byte[] buffer = new byte[1024];
|
byte[] buffer = new byte[1024];
|
||||||
int size;
|
int size;
|
||||||
while ((size = in.read(buffer)) > 0) {
|
while ((size = in.read(buffer)) > 0) {
|
||||||
out.write(buffer, 0, size);
|
out.write(buffer, 0, size);
|
||||||
}
|
}
|
||||||
in.close();
|
}
|
||||||
out.finish();
|
|
||||||
out.close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String getLogDate(Path path) throws IOException {
|
public static synchronized String getLogDate (Path filePath) throws IOException {
|
||||||
BufferedReader reader = Files.newBufferedReader(path);
|
try (final BufferedReader reader = Files.newBufferedReader(filePath, StandardCharsets.UTF_8)) {
|
||||||
String date = reader.readLine();
|
return reader.readLine();
|
||||||
reader.close();
|
}
|
||||||
return date;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean logIsCurrent(Path path) throws IOException {
|
public static synchronized boolean logIsCurrent(Path path) throws IOException {
|
||||||
LocalDate date = LocalDate.now();
|
LocalDate date = LocalDate.now();
|
||||||
return getLogDate(path).equals(date.toString());
|
return getLogDate(path).equals(date.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void log(String str) {
|
public static synchronized void log(String str) {
|
||||||
if (freezeTime > System.currentTimeMillis()) {
|
if (freezeTime > System.currentTimeMillis()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -162,8 +153,7 @@ public class FileLoggerUtilities {
|
||||||
logWriter.write("\n");
|
logWriter.write("\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (str.length() > 32767) logWriter.write("Message too big, not logging this message"); // should these stuff be hardcoded?
|
logWriter.write(getTimePrefix() + str.replaceAll("\\[(\\d+?)x](?=$|[\r\n])", "[/$1x]")); // the replaceAll will prevent conflicts with the duplicate counter
|
||||||
else logWriter.write(getTimePrefix() + str.replaceAll("\\[(\\d+?)x](?=$|[\r\n])", "[/$1x]")); // the replaceAll will prevent conflicts with the duplicate counter
|
|
||||||
logWriter.flush();
|
logWriter.flush();
|
||||||
|
|
||||||
duplicateCounter = 1;
|
duplicateCounter = 1;
|
||||||
|
|
Loading…
Reference in a new issue