From a9722fe171ca95d7408bda6dabd38ded89d9b3fb Mon Sep 17 00:00:00 2001 From: Matt Date: Fri, 25 Oct 2024 14:52:53 -0400 Subject: [PATCH] Update logging filter to allow calls from anyone using our Logging class to register loggers This will allow plugins with other package names like 'org.example' to register and have their stuff still show up in the log outputs. --- .../recaf/analytics/logging/Logging.java | 32 ++++++++++++++++--- .../analytics/logging/RecafLoggingFilter.java | 3 +- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/recaf-core/src/main/java/software/coley/recaf/analytics/logging/Logging.java b/recaf-core/src/main/java/software/coley/recaf/analytics/logging/Logging.java index a234b68d6..df600eaaf 100644 --- a/recaf-core/src/main/java/software/coley/recaf/analytics/logging/Logging.java +++ b/recaf-core/src/main/java/software/coley/recaf/analytics/logging/Logging.java @@ -10,8 +10,12 @@ import java.nio.file.Path; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import java.util.Map; +import java.util.NavigableSet; +import java.util.Set; +import java.util.TreeSet; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.CopyOnWriteArrayList; @@ -24,16 +28,28 @@ */ public class Logging { private static final Map loggers = new ConcurrentHashMap<>(); + private static final NavigableSet loggerKeys = Collections.synchronizedNavigableSet(new TreeSet<>()); private static final List> logConsumers = new CopyOnWriteArrayList<>(); private static Level interceptLevel = Level.INFO; + /** + * @return Set of the current logger keys. + */ + @Nonnull + public static NavigableSet loggerKeys() { + // We track the keys in this separate set so that we can retrieve them + // in sorted order without needing to wrap in 'new TreeSet' every time. + return Collections.unmodifiableNavigableSet(loggerKeys); + } + /** * @param name * Logger name. * * @return Logger associated with name. */ - public static DebuggingLogger get(String name) { + @Nonnull + public static DebuggingLogger get(@Nonnull String name) { return loggers.computeIfAbsent(name, k -> intercept(k, getLogger(k))); } @@ -43,7 +59,8 @@ public static DebuggingLogger get(String name) { * * @return Logger associated with class. */ - public static DebuggingLogger get(Class cls) { + @Nonnull + public static DebuggingLogger get(@Nonnull Class cls) { return loggers.computeIfAbsent(cls.getName(), k -> intercept(k, getLogger(k))); } @@ -69,7 +86,7 @@ public static void removeLogConsumer(@Nonnull LogConsumer consumer) { * @param level * New target level. */ - public static void setInterceptLevel(Level level) { + public static void setInterceptLevel(@Nonnull Level level) { interceptLevel = level; } @@ -80,7 +97,7 @@ public static void setInterceptLevel(Level level) { * Path to file to append to. */ @SuppressWarnings({"unchecked", "rawtypes"}) - public static void addFileAppender(Path path) { + public static void addFileAppender(@Nonnull Path path) { // We do it this way so the file path can be set at runtime. LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory(); FileAppender fileAppender = new FileAppender<>(); @@ -90,14 +107,17 @@ public static void addFileAppender(Path path) { fileAppender.setPrudent(true); fileAppender.setAppend(true); fileAppender.setImmediateFlush(true); + // Pattern PatternLayoutEncoder encoder = new PatternLayoutEncoder(); encoder.setContext(loggerContext); encoder.setPattern("%d{HH:mm:ss.SSS} [%logger{0}/%thread] %-5level: %msg%n"); encoder.start(); fileAppender.setEncoder(encoder); + // Start file appender fileAppender.start(); + // Create logger ch.qos.logback.classic.Logger logbackLogger = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(ch.qos.logback.classic.Logger.ROOT_LOGGER_NAME); @@ -105,7 +125,9 @@ public static void addFileAppender(Path path) { logbackLogger.setAdditive(false); } - private static DebuggingLogger intercept(String name, Logger logger) { + @Nonnull + private static DebuggingLogger intercept(@Nonnull String name, @Nonnull Logger logger) { + loggerKeys.add(name); return new InterceptingLogger(logger) { @Override public void intercept(@Nonnull Level level, String message) { diff --git a/recaf-core/src/main/java/software/coley/recaf/analytics/logging/RecafLoggingFilter.java b/recaf-core/src/main/java/software/coley/recaf/analytics/logging/RecafLoggingFilter.java index ab9782790..1b3af9db4 100644 --- a/recaf-core/src/main/java/software/coley/recaf/analytics/logging/RecafLoggingFilter.java +++ b/recaf-core/src/main/java/software/coley/recaf/analytics/logging/RecafLoggingFilter.java @@ -13,7 +13,8 @@ public class RecafLoggingFilter extends Filter { @Override public FilterReply decide(@Nonnull ILoggingEvent event) { - if (event.getLoggerName().startsWith("software.coley.")) + String loggerName = event.getLoggerName(); + if (loggerName.startsWith("software.coley.") || Logging.loggerKeys().contains(loggerName)) return FilterReply.ACCEPT; return FilterReply.DENY; }