Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MNG-8503] Configure logging using maven.logger.* properties rather than org.slf4j.simpleLogger.* #2048

Merged
merged 4 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
112 changes: 112 additions & 0 deletions api/maven-api-core/src/main/java/org/apache/maven/api/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -453,5 +453,117 @@ public final class Constants {
@Config(type = "java.lang.Integer", defaultValue = "100")
public static final String MAVEN_BUILDER_MAX_PROBLEMS = "maven.builder.maxProblems";

/**
* All system properties used by Maven Logger start with this prefix.
*
* @since 4.0.0
*/
public static final String MAVEN_LOGGER_PREFIX = "maven.logger.";

/**
* Default log level for all instances of SimpleLogger. Must be one of ("trace", "debug", "info",
* "warn", "error" or "off"). If not specified, defaults to "info".
*
* @since 4.0.0
*/
@Config
public static final String MAVEN_LOGGER_DEFAULT_LOG_LEVEL = MAVEN_LOGGER_PREFIX + "defaultLogLevel";

/**
* Set to true if you want the current date and time to be included in output messages. Default is false.
*
* @since 4.0.0
*/
@Config(type = "java.lang.Boolean", defaultValue = "false")
public static final String MAVEN_LOGGER_SHOW_DATE_TIME = MAVEN_LOGGER_PREFIX + "showDateTime";

/**
* The date and time format to be used in the output messages. The pattern describing the date and
* time format is defined by SimpleDateFormat. If the format is not specified or is invalid, the
* number of milliseconds since start up will be output.
*
* @since 4.0.0
*/
@Config
public static final String MAVEN_LOGGER_DATE_TIME_FORMAT = MAVEN_LOGGER_PREFIX + "dateTimeFormat";

/**
* If you would like to output the current thread id, then set to true. Defaults to false.
*
* @since 4.0.0
*/
@Config(type = "java.lang.Boolean", defaultValue = "false")
public static final String MAVEN_LOGGER_SHOW_THREAD_ID = MAVEN_LOGGER_PREFIX + "showThreadId";

/**
* Set to true if you want to output the current thread name. Defaults to true.
*
* @since 4.0.0
*/
@Config(type = "java.lang.Boolean", defaultValue = "true")
public static final String MAVEN_LOGGER_SHOW_THREAD_NAME = MAVEN_LOGGER_PREFIX + "showThreadName";

/**
* Set to true if you want the Logger instance name to be included in output messages. Defaults to true.
*
* @since 4.0.0
*/
@Config(type = "java.lang.Boolean", defaultValue = "true")
public static final String MAVEN_LOGGER_SHOW_LOG_NAME = MAVEN_LOGGER_PREFIX + "showLogName";

/**
* Set to true if you want the last component of the name to be included in output messages. Defaults to false.
*
* @since 4.0.0
*/
@Config(type = "java.lang.Boolean", defaultValue = "false")
public static final String MAVEN_LOGGER_SHOW_SHORT_LOG_NAME = MAVEN_LOGGER_PREFIX + "showShortLogName";

/**
* The output target which can be the path to a file, or the special values "System.out" and "System.err".
* Default is "System.err".
*
* @since 4.0.0
*/
@Config
public static final String MAVEN_LOGGER_LOG_FILE = MAVEN_LOGGER_PREFIX + "logFile";

/**
* Should the level string be output in brackets? Defaults to false.
*
* @since 4.0.0
*/
@Config(type = "java.lang.Boolean", defaultValue = "false")
public static final String MAVEN_LOGGER_LEVEL_IN_BRACKETS = MAVEN_LOGGER_PREFIX + "levelInBrackets";

/**
* The string value output for the warn level. Defaults to WARN.
*
* @since 4.0.0
*/
@Config(defaultValue = "WARN")
public static final String MAVEN_LOGGER_WARN_LEVEL = MAVEN_LOGGER_PREFIX + "warnLevelString";

/**
* If the output target is set to "System.out" or "System.err" (see preceding entry), by default, logs will
* be output to the latest value referenced by System.out/err variables. By setting this parameter to true,
* the output stream will be cached, i.e. assigned once at initialization time and re-used independently of
* the current value referenced by System.out/err.
*
* @since 4.0.0
*/
@Config(type = "java.lang.Boolean", defaultValue = "false")
public static final String MAVEN_LOGGER_CACHE_OUTPUT_STREAM = MAVEN_LOGGER_PREFIX + "cacheOutputStream";

/**
* maven.logger.log.a.b.c - Logging detail level for a SimpleLogger instance named "a.b.c". Right-side value
* must be one of "trace", "debug", "info", "warn", "error" or "off". When a logger named "a.b.c" is initialized,
* its level is assigned from this property. If unspecified, the level of nearest parent logger will be used,
* and if none is set, then the value specified by {@code maven.logger.defaultLogLevel} will be used.
*
* @since 4.0.0
*/
public static final String MAVEN_LOGGER_LOG_PREFIX = MAVEN_LOGGER_PREFIX + "log.";

private Constants() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
*/
package org.apache.maven.cling.logging.impl;

import org.apache.maven.api.Constants;
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.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -42,10 +42,10 @@ public void setRootLoggerLevel(Level level) {
default -> "error";
};

String current = System.setProperty(MavenSimpleLogger.DEFAULT_LOG_LEVEL_KEY, value);
String current = System.setProperty(Constants.MAVEN_LOGGER_DEFAULT_LOG_LEVEL, value);
if (current != null && !value.equalsIgnoreCase(current)) {
LOGGER.info(
"System property '" + MavenSimpleLogger.DEFAULT_LOG_LEVEL_KEY + "' is already set to '" + current
"System property '" + Constants.MAVEN_LOGGER_DEFAULT_LOG_LEVEL + "' is already set to '" + current
+ "' - ignoring system property and get log level from -X/-e/-q options, log level will be set to"
+ value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.ArrayList;
import java.util.List;

import org.apache.maven.api.Constants;
import org.apache.maven.api.MonotonicClock;
import org.slf4j.Logger;
import org.slf4j.Marker;
Expand All @@ -43,61 +44,61 @@
*
*
* <ul>
* <li><code>org.slf4j.simpleLogger.logFile</code> - The output target which can
* <li><code>maven.logger.logFile</code> - The output target which can
* be the <em>path</em> to a file, or the special values "System.out" and
* "System.err". Default is "System.err".</li>
*
* <li><code>org.slf4j.simpleLogger.cacheOutputStream</code> - If the output
* <li><code>maven.logger.cacheOutputStream</code> - If the output
* target is set to "System.out" or "System.err" (see preceding entry), by
* default, logs will be output to the latest value referenced by
* <code>System.out/err</code> variables. By setting this parameter to true, the
* output stream will be cached, i.e. assigned once at initialization time and
* re-used independently of the current value referenced by
* <code>System.out/err</code>.</li>
*
* <li><code>org.slf4j.simpleLogger.defaultLogLevel</code> - Default log level
* <li><code>maven.logger.defaultLogLevel</code> - Default log level
* for all instances of SimpleLogger. Must be one of ("trace", "debug", "info",
* "warn", "error" or "off"). If not specified, defaults to "info".</li>
*
* <li><code>org.slf4j.simpleLogger.log.<em>a.b.c</em></code> - Logging detail
* <li><code>maven.logger.log.<em>a.b.c</em></code> - Logging detail
* level for a SimpleLogger instance named "a.b.c". Right-side value must be one
* of "trace", "debug", "info", "warn", "error" or "off". When a SimpleLogger
* named "a.b.c" is initialized, its level is assigned from this property. If
* unspecified, the level of nearest parent logger will be used, and if none is
* set, then the value specified by
* <code>org.slf4j.simpleLogger.defaultLogLevel</code> will be used.</li>
* <code>maven.logger.defaultLogLevel</code> will be used.</li>
*
* <li><code>org.slf4j.simpleLogger.showDateTime</code> - Set to
* <li><code>maven.logger.showDateTime</code> - Set to
* <code>true</code> if you want the current date and time to be included in
* output messages. Default is <code>false</code></li>
*
* <li><code>org.slf4j.simpleLogger.dateTimeFormat</code> - The date and time
* <li><code>maven.logger.dateTimeFormat</code> - The date and time
* format to be used in the output messages. The pattern describing the date and
* time format is defined by <a href=
* "http://docs.oracle.com/javase/1.5.0/docs/api/java/text/SimpleDateFormat.html">
* <code>SimpleDateFormat</code></a>. If the format is not specified or is
* invalid, the number of milliseconds since start up will be output.</li>
*
* <li><code>org.slf4j.simpleLogger.showThreadName</code> -Set to
* <li><code>maven.logger.showThreadName</code> -Set to
* <code>true</code> if you want to output the current thread name. Defaults to
* <code>true</code>.</li>
*
* <li>(since version 1.7.33 and 2.0.0-alpha6) <code>org.slf4j.simpleLogger.showThreadId</code> -
* <li>(since version 1.7.33 and 2.0.0-alpha6) <code>maven.logger.showThreadId</code> -
* If you would like to output the current thread id, then set to
* <code>true</code>. Defaults to <code>false</code>.</li>
*
* <li><code>org.slf4j.simpleLogger.showLogName</code> - Set to
* <li><code>maven.logger.showLogName</code> - Set to
* <code>true</code> if you want the Logger instance name to be included in
* output messages. Defaults to <code>true</code>.</li>
*
* <li><code>org.slf4j.simpleLogger.showShortLogName</code> - Set to
* <li><code>maven.logger.showShortLogName</code> - Set to
* <code>true</code> if you want the last component of the name to be included
* in output messages. Defaults to <code>false</code>.</li>
*
* <li><code>org.slf4j.simpleLogger.levelInBrackets</code> - Should the level
* <li><code>maven.logger.levelInBrackets</code> - Should the level
* string be output in brackets? Defaults to <code>false</code>.</li>
*
* <li><code>org.slf4j.simpleLogger.warnLevelString</code> - The string value
* <li><code>maven.logger.warnLevelString</code> - The string value
* output for the warn level. Defaults to <code>WARN</code>.</li>
*
* </ul>
Expand Down Expand Up @@ -184,34 +185,9 @@ static void init() {
private transient String shortLogName = null;

/**
* All system properties used by <code>SimpleLogger</code> start with this
* prefix
* Legacy SLF4J prefix maintained for backwards compatibility
*/
public static final String SYSTEM_PREFIX = "org.slf4j.simpleLogger.";

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";
public static final String LEGACY_PREFIX = "org.slf4j.simpleLogger.";

/**
* Protected access allows only {@link MavenLoggerFactory} and also derived classes to instantiate
Expand All @@ -234,8 +210,8 @@ String recursivelyComputeLevelString() {
int indexOfLastDot = tempName.length();
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(".");
levelString = CONFIG_PARAMS.getStringProperty(Constants.MAVEN_LOGGER_LOG_PREFIX + tempName, null);
indexOfLastDot = tempName.lastIndexOf(".");
}
return levelString;
}
Expand All @@ -244,8 +220,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();
Expand Down
Loading
Loading