From d3ab77a1a18bb82210033be11419d4c5fc506046 Mon Sep 17 00:00:00 2001 From: Guillaume Nodet Date: Wed, 15 Jan 2025 08:35:45 +0100 Subject: [PATCH] [MNG-8503] Configure logging using maven.logger.* properties rather than org.slf4j.simpleLogger.* --- ...ger.properties => maven.logger.properties} | 22 +++--- .../impl/MavenSimpleConfiguration.java | 3 +- .../apache/maven/slf4j/MavenBaseLogger.java | 78 +++++++++---------- .../slf4j/SimpleLoggerConfiguration.java | 54 ++++++++++++- 4 files changed, 100 insertions(+), 57 deletions(-) rename apache-maven/src/assembly/maven/conf/logging/{simplelogger.properties => maven.logger.properties} (63%) diff --git a/apache-maven/src/assembly/maven/conf/logging/simplelogger.properties b/apache-maven/src/assembly/maven/conf/logging/maven.logger.properties similarity index 63% rename from apache-maven/src/assembly/maven/conf/logging/simplelogger.properties rename to apache-maven/src/assembly/maven/conf/logging/maven.logger.properties index 8c4a5d1e4b0d..18c71c687cfe 100644 --- a/apache-maven/src/assembly/maven/conf/logging/simplelogger.properties +++ b/apache-maven/src/assembly/maven/conf/logging/maven.logger.properties @@ -15,16 +15,16 @@ # specific language governing permissions and limitations # under the License. -org.slf4j.simpleLogger.defaultLogLevel=info -org.slf4j.simpleLogger.showDateTime=false -org.slf4j.simpleLogger.showThreadName=false -org.slf4j.simpleLogger.showLogName=false -org.slf4j.simpleLogger.logFile=System.out -org.slf4j.simpleLogger.cacheOutputStream=true -org.slf4j.simpleLogger.levelInBrackets=true -org.slf4j.simpleLogger.log.Sisu=info -org.slf4j.simpleLogger.warnLevelString=WARNING +maven.logger.defaultLogLevel=info +maven.logger.showDateTime=false +maven.logger.showThreadName=false +maven.logger.showLogName=false +maven.logger.logFile=System.out +maven.logger.cacheOutputStream=true +maven.logger.levelInBrackets=true +maven.logger.log.Sisu=info +maven.logger.warnLevelString=WARNING # MNG-6181: mvn -X also prints all debug logging from HttpClient -org.slf4j.simpleLogger.log.org.apache.http=off -org.slf4j.simpleLogger.log.org.apache.http.wire=off +maven.logger.log.org.apache.http=off +maven.logger.log.org.apache.http.wire=off diff --git a/impl/maven-cli/src/main/java/org/apache/maven/cling/logging/impl/MavenSimpleConfiguration.java b/impl/maven-cli/src/main/java/org/apache/maven/cling/logging/impl/MavenSimpleConfiguration.java index e7fb25b39d04..e4a5bd7b9b62 100644 --- a/impl/maven-cli/src/main/java/org/apache/maven/cling/logging/impl/MavenSimpleConfiguration.java +++ b/impl/maven-cli/src/main/java/org/apache/maven/cling/logging/impl/MavenSimpleConfiguration.java @@ -20,6 +20,7 @@ import org.apache.maven.cling.logging.BaseSlf4jConfiguration; import org.apache.maven.slf4j.MavenLoggerFactory; +import org.apache.maven.slf4j.MavenSimpleLogger; import org.slf4j.ILoggerFactory; import org.slf4j.LoggerFactory; @@ -37,7 +38,7 @@ public void setRootLoggerLevel(Level level) { case INFO -> "info"; default -> "error"; }; - System.setProperty("org.slf4j.simpleLogger.defaultLogLevel", value); + System.setProperty(MavenSimpleLogger.DEFAULT_LOG_LEVEL_KEY, value); } @Override diff --git a/impl/maven-logging/src/main/java/org/apache/maven/slf4j/MavenBaseLogger.java b/impl/maven-logging/src/main/java/org/apache/maven/slf4j/MavenBaseLogger.java index cd8d1e19f034..aed4c6f2cc62 100644 --- a/impl/maven-logging/src/main/java/org/apache/maven/slf4j/MavenBaseLogger.java +++ b/impl/maven-logging/src/main/java/org/apache/maven/slf4j/MavenBaseLogger.java @@ -43,11 +43,11 @@ * * * @@ -184,34 +184,28 @@ static void init() { private transient String shortLogName = null; /** - * All system properties used by SimpleLogger start with this - * prefix + * All system properties used by Maven Logger start with this prefix. */ - public static final String SYSTEM_PREFIX = "org.slf4j.simpleLogger."; + public static final String MAVEN_PREFIX = "maven.logger."; - public static final String LOG_KEY_PREFIX = MavenBaseLogger.SYSTEM_PREFIX + "log."; - - public static final String CACHE_OUTPUT_STREAM_STRING_KEY = MavenBaseLogger.SYSTEM_PREFIX + "cacheOutputStream"; - - public static final String WARN_LEVEL_STRING_KEY = MavenBaseLogger.SYSTEM_PREFIX + "warnLevelString"; - - public static final String LEVEL_IN_BRACKETS_KEY = MavenBaseLogger.SYSTEM_PREFIX + "levelInBrackets"; - - public static final String LOG_FILE_KEY = MavenBaseLogger.SYSTEM_PREFIX + "logFile"; - - public static final String SHOW_SHORT_LOG_NAME_KEY = MavenBaseLogger.SYSTEM_PREFIX + "showShortLogName"; - - public static final String SHOW_LOG_NAME_KEY = MavenBaseLogger.SYSTEM_PREFIX + "showLogName"; - - public static final String SHOW_THREAD_NAME_KEY = MavenBaseLogger.SYSTEM_PREFIX + "showThreadName"; - - public static final String SHOW_THREAD_ID_KEY = MavenBaseLogger.SYSTEM_PREFIX + "showThreadId"; - - public static final String DATE_TIME_FORMAT_KEY = MavenBaseLogger.SYSTEM_PREFIX + "dateTimeFormat"; - - public static final String SHOW_DATE_TIME_KEY = MavenBaseLogger.SYSTEM_PREFIX + "showDateTime"; - - public static final String DEFAULT_LOG_LEVEL_KEY = MavenBaseLogger.SYSTEM_PREFIX + "defaultLogLevel"; + /** + * Legacy SLF4J prefix maintained for backwards compatibility + */ + public static final String LEGACY_PREFIX = "org.slf4j.simpleLogger."; + + // Property keys with new maven prefix + public static final String LOG_KEY_PREFIX = MAVEN_PREFIX + "log."; + public static final String CACHE_OUTPUT_STREAM_STRING_KEY = MAVEN_PREFIX + "cacheOutputStream"; + public static final String WARN_LEVEL_STRING_KEY = MAVEN_PREFIX + "warnLevelString"; + public static final String LEVEL_IN_BRACKETS_KEY = MAVEN_PREFIX + "levelInBrackets"; + public static final String LOG_FILE_KEY = MAVEN_PREFIX + "logFile"; + public static final String SHOW_SHORT_LOG_NAME_KEY = MAVEN_PREFIX + "showShortLogName"; + public static final String SHOW_LOG_NAME_KEY = MAVEN_PREFIX + "showLogName"; + public static final String SHOW_THREAD_NAME_KEY = MAVEN_PREFIX + "showThreadName"; + public static final String SHOW_THREAD_ID_KEY = MAVEN_PREFIX + "showThreadId"; + public static final String DATE_TIME_FORMAT_KEY = MAVEN_PREFIX + "dateTimeFormat"; + public static final String SHOW_DATE_TIME_KEY = MAVEN_PREFIX + "showDateTime"; + public static final String DEFAULT_LOG_LEVEL_KEY = MAVEN_PREFIX + "defaultLogLevel"; /** * Protected access allows only {@link MavenLoggerFactory} and also derived classes to instantiate @@ -235,7 +229,7 @@ String recursivelyComputeLevelString() { while ((levelString == null) && (indexOfLastDot > -1)) { tempName = tempName.substring(0, indexOfLastDot); levelString = CONFIG_PARAMS.getStringProperty(MavenBaseLogger.LOG_KEY_PREFIX + tempName, null); - indexOfLastDot = String.valueOf(tempName).lastIndexOf("."); + indexOfLastDot = tempName.lastIndexOf("."); } return levelString; } @@ -244,8 +238,8 @@ String recursivelyComputeLevelString() { * To avoid intermingling of log messages and associated stack traces, the two * operations are done in a synchronized block. * - * @param buf - * @param t + * @param buf The StringBuilder containing the log message to be written + * @param t The Throwable object whose stack trace should be written, may be null */ protected void write(StringBuilder buf, Throwable t) { PrintStream targetStream = CONFIG_PARAMS.outputChoice.getTargetPrintStream(); diff --git a/impl/maven-logging/src/main/java/org/apache/maven/slf4j/SimpleLoggerConfiguration.java b/impl/maven-logging/src/main/java/org/apache/maven/slf4j/SimpleLoggerConfiguration.java index 0ea1a33498ca..99a33ab5082d 100644 --- a/impl/maven-logging/src/main/java/org/apache/maven/slf4j/SimpleLoggerConfiguration.java +++ b/impl/maven-logging/src/main/java/org/apache/maven/slf4j/SimpleLoggerConfiguration.java @@ -44,7 +44,10 @@ */ public class SimpleLoggerConfiguration { - private static final String CONFIGURATION_FILE = "simplelogger.properties"; + private static final String CONFIGURATION_FILE = "maven.logger.properties"; + + @Deprecated(since = "4.0.0") + private static final String LEGACY_CONFIGURATION_FILE = "simplelogger.properties"; static final int DEFAULT_LOG_LEVEL_DEFAULT = MavenBaseLogger.LOG_LEVEL_INFO; int defaultLogLevel = DEFAULT_LOG_LEVEL_DEFAULT; @@ -127,12 +130,36 @@ void init() { } private void loadProperties() { - // Add props from the resource simplelogger.properties ClassLoader threadCL = Thread.currentThread().getContextClassLoader(); ClassLoader toUseCL = (threadCL != null ? threadCL : ClassLoader.getSystemClassLoader()); + + // Try loading maven properties first + boolean mavenPropsLoaded = false; try (InputStream in = toUseCL.getResourceAsStream(CONFIGURATION_FILE)) { if (in != null) { properties.load(in); + mavenPropsLoaded = true; + } + } catch (java.io.IOException e) { + // ignored + } + + // Try loading legacy properties + try (InputStream in = toUseCL.getResourceAsStream(LEGACY_CONFIGURATION_FILE)) { + if (in != null) { + Properties legacyProps = new Properties(); + legacyProps.load(in); + if (!mavenPropsLoaded) { + Reporter.warn("Using deprecated " + LEGACY_CONFIGURATION_FILE + ". Please migrate to " + + CONFIGURATION_FILE); + } + // Only load legacy properties if there's no maven equivalent + for (String propName : legacyProps.stringPropertyNames()) { + String mavenKey = propName.replace(MavenBaseLogger.LEGACY_PREFIX, MavenBaseLogger.MAVEN_PREFIX); + if (!properties.containsKey(mavenKey)) { + properties.setProperty(mavenKey, legacyProps.getProperty(propName)); + } + } } } catch (java.io.IOException e) { // ignored @@ -152,11 +179,32 @@ boolean getBooleanProperty(String name, boolean defaultValue) { String getStringProperty(String name) { String prop = null; try { + // Try maven property first prop = System.getProperty(name); + if (prop == null && name.startsWith(MavenBaseLogger.MAVEN_PREFIX)) { + // Try legacy property + String legacyName = name.replace(MavenBaseLogger.MAVEN_PREFIX, MavenBaseLogger.LEGACY_PREFIX); + prop = System.getProperty(legacyName); + if (prop != null) { + Reporter.warn("Using deprecated property " + legacyName + ". Please migrate to " + name); + } + } } catch (SecurityException e) { // Ignore } - return (prop == null) ? properties.getProperty(name) : prop; + + if (prop == null) { + prop = properties.getProperty(name); + if (prop == null && name.startsWith(MavenBaseLogger.MAVEN_PREFIX)) { + // Try legacy property from properties file + String legacyName = name.replace(MavenBaseLogger.MAVEN_PREFIX, MavenBaseLogger.LEGACY_PREFIX); + prop = properties.getProperty(legacyName); + if (prop != null) { + Reporter.warn("Using deprecated property " + legacyName + ". Please migrate to " + name); + } + } + } + return prop; } static int stringToLevel(String levelStr) {