Skip to content

Commit

Permalink
[MNG-8503] Configure logging using maven.logger.* properties rather t…
Browse files Browse the repository at this point in the history
…han org.slf4j.simpleLogger.*
  • Loading branch information
gnodet committed Jan 15, 2025
1 parent aeec37f commit d3ab77a
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 57 deletions.
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
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,61 +43,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 +184,28 @@ static void init() {
private transient String shortLogName = null;

/**
* All system properties used by <code>SimpleLogger</code> 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
Expand All @@ -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;
}
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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) {
Expand Down

0 comments on commit d3ab77a

Please sign in to comment.