Don't use dependencies that would be downloaded in loggers

This commit is contained in:
Vankka 2023-06-18 23:36:15 +03:00
parent 11914b5d0e
commit 1586fcb927
No known key found for this signature in database
GPG Key ID: 6E50CB7A29B96AD0
3 changed files with 35 additions and 16 deletions

View File

@ -21,6 +21,9 @@ package com.discordsrv.common.logging;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import java.io.PrintWriter;
import java.io.StringWriter;
public interface Logger { public interface Logger {
default void info(String message) { default void info(String message) {
@ -69,4 +72,11 @@ public interface Logger {
void log(@Nullable String loggerName, @NotNull LogLevel logLevel, @Nullable String message, @Nullable Throwable throwable); void log(@Nullable String loggerName, @NotNull LogLevel logLevel, @Nullable String message, @Nullable Throwable throwable);
default String getStackTrace(Throwable throwable) {
final StringWriter stringWriter = new StringWriter();
final PrintWriter printWriter = new PrintWriter(stringWriter, true);
throwable.printStackTrace(printWriter);
return stringWriter.getBuffer().toString();
}
} }

View File

@ -23,8 +23,6 @@ import com.discordsrv.common.logging.LogLevel;
import com.discordsrv.common.logging.Logger; import com.discordsrv.common.logging.Logger;
import com.discordsrv.common.logging.backend.LogFilter; import com.discordsrv.common.logging.backend.LogFilter;
import com.discordsrv.common.logging.backend.LoggingBackend; import com.discordsrv.common.logging.backend.LoggingBackend;
import org.apache.commons.collections4.bidimap.DualHashBidiMap;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
@ -39,12 +37,18 @@ import java.util.logging.LogRecord;
public class JavaLoggerImpl implements Logger, LoggingBackend { public class JavaLoggerImpl implements Logger, LoggingBackend {
private static final DualHashBidiMap<Level, LogLevel> LEVELS = new DualHashBidiMap<>(); private static final Map<Level, LogLevel> LEVELS = new HashMap<>();
private static final Map<LogLevel, Level> LEVELS_REVERSE = new HashMap<>();
private static void put(Level level, LogLevel logLevel) {
LEVELS.put(level, logLevel);
LEVELS_REVERSE.put(logLevel, level);
}
static { static {
LEVELS.put(Level.INFO, LogLevel.INFO); put(Level.INFO, LogLevel.INFO);
LEVELS.put(Level.WARNING, LogLevel.WARNING); put(Level.WARNING, LogLevel.WARNING);
LEVELS.put(Level.SEVERE, LogLevel.ERROR); put(Level.SEVERE, LogLevel.ERROR);
} }
private final java.util.logging.Logger logger; private final java.util.logging.Logger logger;
@ -61,7 +65,7 @@ public class JavaLoggerImpl implements Logger, LoggingBackend {
@Override @Override
public void log(@Nullable String loggerName, @NotNull LogLevel level, @Nullable String message, @Nullable Throwable throwable) { public void log(@Nullable String loggerName, @NotNull LogLevel level, @Nullable String message, @Nullable Throwable throwable) {
Level logLevel = LEVELS.getKey(level); Level logLevel = LEVELS_REVERSE.get(level);
if (logLevel != null) { if (logLevel != null) {
List<String> contents = new ArrayList<>(2); List<String> contents = new ArrayList<>(2);
if (message != null) { if (message != null) {
@ -69,7 +73,7 @@ public class JavaLoggerImpl implements Logger, LoggingBackend {
} }
if (throwable != null) { if (throwable != null) {
// Exceptions aren't always logged correctly by the logger itself // Exceptions aren't always logged correctly by the logger itself
contents.add(ExceptionUtils.getStackTrace(throwable)); contents.add(getStackTrace(throwable));
} }
logger.log(logLevel, String.join("\n", contents)); logger.log(logLevel, String.join("\n", contents));
} }

View File

@ -23,7 +23,6 @@ import com.discordsrv.common.logging.LogLevel;
import com.discordsrv.common.logging.Logger; import com.discordsrv.common.logging.Logger;
import com.discordsrv.common.logging.backend.LogFilter; import com.discordsrv.common.logging.backend.LogFilter;
import com.discordsrv.common.logging.backend.LoggingBackend; import com.discordsrv.common.logging.backend.LoggingBackend;
import org.apache.commons.collections4.bidimap.DualHashBidiMap;
import org.apache.logging.log4j.Level; import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Marker; import org.apache.logging.log4j.Marker;
@ -41,14 +40,20 @@ import java.util.Map;
public class Log4JLoggerImpl implements Logger, LoggingBackend { public class Log4JLoggerImpl implements Logger, LoggingBackend {
private static final DualHashBidiMap<Level, LogLevel> LEVELS = new DualHashBidiMap<>(); private static final Map<Level, LogLevel> LEVELS = new HashMap<>();
private static final Map<LogLevel, Level> LEVELS_REVERSE = new HashMap<>();
private static void put(Level level, LogLevel logLevel) {
LEVELS.put(level, logLevel);
LEVELS_REVERSE.put(logLevel, level);
}
static { static {
LEVELS.put(Level.INFO, LogLevel.INFO); put(Level.INFO, LogLevel.INFO);
LEVELS.put(Level.WARN, LogLevel.WARNING); put(Level.WARN, LogLevel.WARNING);
LEVELS.put(Level.ERROR, LogLevel.ERROR); put(Level.ERROR, LogLevel.ERROR);
LEVELS.put(Level.DEBUG, LogLevel.DEBUG); put(Level.DEBUG, LogLevel.DEBUG);
LEVELS.put(Level.TRACE, LogLevel.TRACE); put(Level.TRACE, LogLevel.TRACE);
} }
private final org.apache.logging.log4j.Logger logger; private final org.apache.logging.log4j.Logger logger;
@ -65,7 +70,7 @@ public class Log4JLoggerImpl implements Logger, LoggingBackend {
@Override @Override
public void log(@Nullable String loggerName, @NotNull LogLevel level, @Nullable String message, @Nullable Throwable throwable) { public void log(@Nullable String loggerName, @NotNull LogLevel level, @Nullable String message, @Nullable Throwable throwable) {
Level logLevel = LEVELS.getKey(level); Level logLevel = LEVELS_REVERSE.get(level);
logger.log(logLevel, message, throwable); logger.log(logLevel, message, throwable);
} }