Add exceptions to the devlok-logger

This commit is contained in:
Weapie 2024-07-08 14:40:51 +02:00
parent 9dc62ef6fd
commit aa50c7a2bc
6 changed files with 85 additions and 21 deletions

View File

@ -12,7 +12,7 @@ allprojects {
apply(plugin = "com.github.johnrengelman.shadow")
group = "de.weapie"
version = "2024.07.08-01"
version = "2024.07.08-02"
repositories {
mavenCentral()

View File

@ -7,59 +7,59 @@ import java.io.InputStream;
public class StringsInputStream<T extends Iterable<String>> extends InputStream {
private ByteArrayInputStream bais = null;
private ByteArrayInputStream byteArrayInputStream = null;
public StringsInputStream(final T strings) throws IOException {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
for (String line : strings) {
outputStream.write(line.getBytes());
}
bais = new ByteArrayInputStream(outputStream.toByteArray());
byteArrayInputStream = new ByteArrayInputStream(outputStream.toByteArray());
}
@Override
public int read() throws IOException {
return bais.read();
return byteArrayInputStream.read();
}
@Override
public int read(byte[] b) throws IOException {
return bais.read(b);
return byteArrayInputStream.read(b);
}
@Override
public int read(byte[] b, int off, int len) throws IOException {
return bais.read(b, off, len);
return byteArrayInputStream.read(b, off, len);
}
@Override
public long skip(long n) throws IOException {
return bais.skip(n);
return byteArrayInputStream.skip(n);
}
@Override
public int available() throws IOException {
return bais.available();
return byteArrayInputStream.available();
}
@Override
public void close() throws IOException {
bais.close();
byteArrayInputStream.close();
}
@Override
public synchronized void mark(int readLimit) {
bais.mark(readLimit);
byteArrayInputStream.mark(readLimit);
}
@Override
public synchronized void reset() throws IOException {
bais.reset();
byteArrayInputStream.reset();
}
@Override
public boolean markSupported() {
return bais.markSupported();
return byteArrayInputStream.markSupported();
}
}

View File

@ -2,6 +2,9 @@ package de.weapie.devlok.logger;
import de.weapie.devlok.files.haste.HasteAPI;
import de.weapie.devlok.files.utils.StringsInputStream;
import de.weapie.devlok.logger.entry.BasicEntry;
import de.weapie.devlok.logger.entry.ErrorEntry;
import de.weapie.devlok.logger.entry.LogEntry;
import de.weapie.devlok.utils.types.MessageUtils;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
@ -17,7 +20,7 @@ public class DevlokLogger {
@NonNull private String prefix;
@NonNull private Logger logger;
private List<LogEntry> entries = new ArrayList<>();
private List<BasicEntry> entries = new ArrayList<>();
public void postHaste() {
this.postHaste(null);
@ -35,7 +38,9 @@ public class DevlokLogger {
add("-----------------------------------\n");
add("\n");
}};
for (LogEntry entry : entries) {
for (BasicEntry entry : entries) {
if(!entry.uploadable) continue;
lines.add(entry.prepareLogMessage(this.prefix) + "\n");
}
@ -44,6 +49,12 @@ public class DevlokLogger {
this.info("Haste URL: " + url);
}
public void error(Exception exception) {
ErrorEntry logEntry = new ErrorEntry("ERROR", exception.getMessage(), exception);
entries.add(logEntry);
this.logger.severe(logEntry.prepareLogMessage(this.prefix));
}
public void info(String message) {
LogEntry logEntry = new LogEntry("INFO", message);
entries.add(logEntry);
@ -86,6 +97,12 @@ public class DevlokLogger {
this.logger.finest(logEntry.prepareLogMessage(this.prefix));
}
public void error(Exception exception, boolean uploadable) {
ErrorEntry logEntry = new ErrorEntry("ERROR", exception.getMessage(), exception, uploadable);
entries.add(logEntry);
this.logger.severe(logEntry.prepareLogMessage(this.prefix));
}
public void info(String message, boolean uploadable) {
LogEntry logEntry = new LogEntry("INFO", message, uploadable);
entries.add(logEntry);

View File

@ -0,0 +1,12 @@
package de.weapie.devlok.logger.entry;
public abstract class BasicEntry {
public long time = 0;
public String type = "";
public String message = "";
public boolean uploadable = false;
public abstract String prepareLogMessage(String prefix);
}

View File

@ -0,0 +1,39 @@
package de.weapie.devlok.logger.entry;
import de.weapie.devlok.utils.types.MessageUtils;
import java.io.PrintWriter;
import java.io.StringWriter;
public class ErrorEntry extends BasicEntry {
private Exception exception;
public ErrorEntry(String type, String message, Exception exception) {
this.time = System.currentTimeMillis();
this.type = type;
this.message = message;
this.uploadable = true;
this.exception = exception;
}
public ErrorEntry(String type, String message, Exception exception, boolean uploadable) {
this.time = System.currentTimeMillis();
this.type = type;
this.message = message;
this.uploadable = uploadable;
this.exception = exception;
}
@Override
public String prepareLogMessage(String prefix) {
String msg = "[" + MessageUtils.getDateAndTimeWithSecondsFromMillis(this.time) + "/" + prefix + " #" + this.type + "] " + message + "\n";
StringWriter sw = new StringWriter();
exception.printStackTrace(new PrintWriter(sw));
msg += sw.toString();
return msg;
}
}

View File

@ -1,4 +1,4 @@
package de.weapie.devlok.logger;
package de.weapie.devlok.logger.entry;
import de.weapie.devlok.utils.types.MessageUtils;
import lombok.AllArgsConstructor;
@ -6,12 +6,7 @@ import lombok.Getter;
@Getter
@AllArgsConstructor
public class LogEntry {
private long time;
private String type;
private String message;
private boolean uploadable;
public class LogEntry extends BasicEntry {
public LogEntry(String type, String message) {
this.time = System.currentTimeMillis();
@ -27,6 +22,7 @@ public class LogEntry {
this.uploadable = uploadable;
}
@Override
public String prepareLogMessage(String prefix) {
return "[" + MessageUtils.getDateAndTimeWithSecondsFromMillis(this.time) + "/" + prefix + " #" + this.type + "] " + message;
}