Skip to content

Commit

Permalink
Update logging filter to allow calls from anyone using our Logging cl…
Browse files Browse the repository at this point in the history
…ass 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.
  • Loading branch information
Col-E committed Oct 25, 2024
1 parent 236cbf2 commit a9722fe
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -24,16 +28,28 @@
*/
public class Logging {
private static final Map<String, DebuggingLogger> loggers = new ConcurrentHashMap<>();
private static final NavigableSet<String> loggerKeys = Collections.synchronizedNavigableSet(new TreeSet<>());
private static final List<LogConsumer<String>> logConsumers = new CopyOnWriteArrayList<>();
private static Level interceptLevel = Level.INFO;

/**
* @return Set of the current logger keys.
*/
@Nonnull
public static NavigableSet<String> 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)));
}

Expand All @@ -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)));
}

Expand All @@ -69,7 +86,7 @@ public static void removeLogConsumer(@Nonnull LogConsumer<String> consumer) {
* @param level
* New target level.
*/
public static void setInterceptLevel(Level level) {
public static void setInterceptLevel(@Nonnull Level level) {
interceptLevel = level;
}

Expand All @@ -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<>();
Expand All @@ -90,22 +107,27 @@ 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);
logbackLogger.addAppender(fileAppender);
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
public class RecafLoggingFilter extends Filter<ILoggingEvent> {
@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;
}
Expand Down

0 comments on commit a9722fe

Please sign in to comment.