gnodet-bot commented on code in PR #13832: URL: https://github.com/apache/maven/pull/13832#discussion_r4535453809
########## impl/maven-logging-slf4j/src/main/java/org/apache/maven/logging/slf4j/MavenLoggerFactory.java: ########## @@ -0,0 +1,107 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.maven.logging.slf4j; + +import java.util.Objects; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; + +import org.apache.maven.api.logging.LoggerManager; +import org.apache.maven.logging.impl.DefaultLoggerManager; +import org.slf4j.ILoggerFactory; +import org.slf4j.Logger; + +/** + * LogFactory for Maven which creates SLF4J loggers that delegate level checks + * and severity recording to the shared {@link DefaultLoggerManager}. + */ +public class MavenLoggerFactory implements ILoggerFactory { + static volatile MavenLoggerFactory instance; + + private static final StackWalker STACK_WALKER = StackWalker.getInstance(StackWalker.Option.RETAIN_CLASS_REFERENCE); + + final DefaultLoggerManager logManager; + final ConcurrentMap<String, Logger> loggerMap = new ConcurrentHashMap<>(); + + public MavenLoggerFactory() { + instance = this; + LoggerManager mgr = LoggerManager.get(); + if (mgr instanceof DefaultLoggerManager dlm) { + logManager = dlm; + } else { + logManager = new DefaultLoggerManager(); + LoggerManager.set(logManager); + } + logManager.addReconfigurationListener(this::updateExistingLoggers); + } + + @Override + public Logger getLogger(String name) { + // Fast path: if a logger is already cached by name alone (non-plugin case), + // return it without performing an expensive stack walk. + Logger cached = loggerMap.get(name); + if (cached != null) { + return cached; + } + + String pluginId = resolvePluginId(); + String cacheKey = pluginId != null ? name + "@" + pluginId : name; + return loggerMap.computeIfAbsent(cacheKey, k -> new MavenSimpleLogger(name, pluginId)); + } + + /** Sentinel value for classloaders that are not associated with any plugin. */ + private static final String NO_PLUGIN = "\0"; + + private final ConcurrentMap<ClassLoader, String> classLoaderPluginIds = new ConcurrentHashMap<>(); Review Comment: ⚠️ **`classLoaderPluginIds` retains strong ClassLoader references — plugin classloaders leak.** `ConcurrentMap<ClassLoader, String>` uses strong keys. Plugin classloaders are GC-eligible after mojo execution completes (the plugin realm is discarded by `PlexusContainerCapsuleFactory`). But as long as this map holds a strong reference to the classloader, neither the classloader nor any of the classes it loaded can be collected. In a build with many distinct plugins, this accumulates one leaked classloader entry per unique plugin (and its entire class graph) for the lifetime of the Maven JVM. Use `ClassValue<String>` — it is keyed by `Class`, is GC-friendly (the JVM can reclaim the value when the class is unloaded), and is lock-free: ```suggestion private final ClassValue<String> classLoaderPluginIdByClass = new ClassValue<>() { @Override protected String computeValue(Class<?> type) { String resolved = LoggerManager.get().getPluginId(type.getClassLoader()); return resolved != null ? resolved : NO_PLUGIN; } }; ``` Then in `resolveClassLoaderPluginId(ClassLoader cl)`: ```java private String resolveClassLoaderPluginId(Class<?> cls) { if (cls == null || cls.getClassLoader() == null) { return null; } String id = classLoaderPluginIdByClass.get(cls); return id == NO_PLUGIN ? null : id; } ``` And in `resolvePluginId()`, pass `f.getDeclaringClass()` instead of `f.getDeclaringClass().getClassLoader()`. `ClassValue` ties the cache lifetime to the class — when the plugin realm is unloaded, its entries are automatically reclaimed. ########## impl/maven-logging-slf4j/src/main/java/org/apache/maven/logging/slf4j/MavenLoggerFactory.java: ########## @@ -0,0 +1,107 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.maven.logging.slf4j; + +import java.util.Objects; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; + +import org.apache.maven.api.logging.LoggerManager; +import org.apache.maven.logging.impl.DefaultLoggerManager; +import org.slf4j.ILoggerFactory; +import org.slf4j.Logger; + +/** + * LogFactory for Maven which creates SLF4J loggers that delegate level checks + * and severity recording to the shared {@link DefaultLoggerManager}. + */ +public class MavenLoggerFactory implements ILoggerFactory { + static volatile MavenLoggerFactory instance; + + private static final StackWalker STACK_WALKER = StackWalker.getInstance(StackWalker.Option.RETAIN_CLASS_REFERENCE); + + final DefaultLoggerManager logManager; + final ConcurrentMap<String, Logger> loggerMap = new ConcurrentHashMap<>(); + + public MavenLoggerFactory() { + instance = this; + LoggerManager mgr = LoggerManager.get(); + if (mgr instanceof DefaultLoggerManager dlm) { + logManager = dlm; + } else { + logManager = new DefaultLoggerManager(); + LoggerManager.set(logManager); + } + logManager.addReconfigurationListener(this::updateExistingLoggers); + } + + @Override + public Logger getLogger(String name) { + // Fast path: if a logger is already cached by name alone (non-plugin case), + // return it without performing an expensive stack walk. + Logger cached = loggerMap.get(name); + if (cached != null) { + return cached; + } + Review Comment: ⚠️ **Fast-path cache bypass breaks plugin log attribution.** `loggerMap.get(name)` is keyed by bare `name` for non-plugin loggers, but by `name + "@" + pluginId` for plugin loggers (line 73). The fast path at line 55 hits on the bare `name` key and returns early, skipping `resolvePluginId()` entirely. This means: if `LoggerFactory.getLogger("com.example.Foo")` is called once from a non-plugin context (key `"com.example.Foo"` stored), subsequent calls from a plugin classloader with the same logger name also hit the fast path and get the wrong (global, non-plugin-attributed) logger. Plugin log output then appears without plugin context. The comment says "non-plugin case" but the check cannot distinguish whether the cached entry is non-plugin or whether this invocation is from a non-plugin context. The classloader cache (`classLoaderPluginIds`) already eliminates the expensive `getPluginId()` call — the stack walk itself is the remaining cost. But a logger name lookup short-circuits even the stack walk, breaking correctness. **Fix:** remove the fast path. The classloader cache makes the stack walk cheap enough: ```suggestion @Override public Logger getLogger(String name) { String pluginId = resolvePluginId(); String cacheKey = pluginId != null ? name + "@" + pluginId : name; return loggerMap.computeIfAbsent(cacheKey, k -> new MavenSimpleLogger(name, pluginId)); } ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
