gnodet commented on code in PR #13832: URL: https://github.com/apache/maven/pull/13832#discussion_r4548350611
########## 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: Already addressed — classLoaderPluginIds uses WeakHashMap with synchronized access. Javadoc at lines 72-80 documents the design. ########## impl/maven-core/src/main/java/org/apache/maven/logging/ProjectBuildLogAppender.java: ########## @@ -109,29 +114,102 @@ public static void setForkingMojoId(String forkingMojoId) { } } - public static void updateMdc() { - String id = getProjectId(); - if (id != null) { - MDC.put(KEY_PROJECT_ID, id); - } else { - MDC.remove(KEY_PROJECT_ID); - } + private static final AtomicLong SEQUENCE = new AtomicLong(); + + private static volatile Consumer<LogEvent> reportCapture; + + public static void setReportCapture(Consumer<LogEvent> capture) { + ProjectBuildLogAppender.reportCapture = capture; } private final BuildEventListener buildEventListener; + private final ConcurrentLinkedQueue<LogEvent> queue = new ConcurrentLinkedQueue<>(); + private final ReentrantLock drainLock = new ReentrantLock(); + private volatile boolean needsMetadata; public ProjectBuildLogAppender(BuildEventListener buildEventListener) { this.buildEventListener = buildEventListener; - MavenSimpleLogger.setLogSink(this::accept); + LoggerManager.get().setLogSink(new Sink()); + } + + public void setNeedsMetadata(boolean needsMetadata) { + this.needsMetadata = needsMetadata; + } + + private class Sink implements LogSink { + @Override + public void accept(LogEvent event) { + final LogEvent full; + String projectId = event.projectId() != null ? event.projectId() : getProjectId(); + String mojoId = event.mojoId() != null ? event.mojoId() : getMojoId(); + if (event instanceof DefaultLogEvent && projectId == event.projectId() && mojoId == event.mojoId()) { Review Comment: Already addressed — uses Objects.equals() for both projectId and mojoId comparisons. ########## impl/maven-core/src/main/java/org/apache/maven/logging/ProjectBuildLogAppender.java: ########## @@ -109,29 +114,102 @@ public static void setForkingMojoId(String forkingMojoId) { } } - public static void updateMdc() { - String id = getProjectId(); - if (id != null) { - MDC.put(KEY_PROJECT_ID, id); - } else { - MDC.remove(KEY_PROJECT_ID); - } + private static final AtomicLong SEQUENCE = new AtomicLong(); + + private static volatile Consumer<LogEvent> reportCapture; + + public static void setReportCapture(Consumer<LogEvent> capture) { + ProjectBuildLogAppender.reportCapture = capture; } private final BuildEventListener buildEventListener; + private final ConcurrentLinkedQueue<LogEvent> queue = new ConcurrentLinkedQueue<>(); + private final ReentrantLock drainLock = new ReentrantLock(); + private volatile boolean needsMetadata; public ProjectBuildLogAppender(BuildEventListener buildEventListener) { this.buildEventListener = buildEventListener; - MavenSimpleLogger.setLogSink(this::accept); + LoggerManager.get().setLogSink(new Sink()); + } + + public void setNeedsMetadata(boolean needsMetadata) { + this.needsMetadata = needsMetadata; + } + + private class Sink implements LogSink { + @Override + public void accept(LogEvent event) { + final LogEvent full; + String projectId = event.projectId() != null ? event.projectId() : getProjectId(); + String mojoId = event.mojoId() != null ? event.mojoId() : getMojoId(); + if (event instanceof DefaultLogEvent && projectId == event.projectId() && mojoId == event.mojoId()) { Review Comment: Already addressed — uses Objects.equals() for both projectId and mojoId comparisons. -- 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]
