Skip to content

Commit 21121d8

Browse files
authored
[MNG-8503] Configure logging using maven.logger.* properties rather than org.slf4j.simpleLogger.* (#2048)
1 parent 023b014 commit 21121d8

File tree

9 files changed

+491
-212
lines changed

9 files changed

+491
-212
lines changed

apache-maven/src/assembly/maven/conf/logging/simplelogger.properties renamed to apache-maven/src/assembly/maven/conf/logging/maven.logger.properties

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,16 @@
1515
# specific language governing permissions and limitations
1616
# under the License.
1717

18-
org.slf4j.simpleLogger.defaultLogLevel=info
19-
org.slf4j.simpleLogger.showDateTime=false
20-
org.slf4j.simpleLogger.showThreadName=false
21-
org.slf4j.simpleLogger.showLogName=false
22-
org.slf4j.simpleLogger.logFile=System.out
23-
org.slf4j.simpleLogger.cacheOutputStream=true
24-
org.slf4j.simpleLogger.levelInBrackets=true
25-
org.slf4j.simpleLogger.log.Sisu=info
26-
org.slf4j.simpleLogger.warnLevelString=WARNING
18+
maven.logger.defaultLogLevel=info
19+
maven.logger.showDateTime=false
20+
maven.logger.showThreadName=false
21+
maven.logger.showLogName=false
22+
maven.logger.logFile=System.out
23+
maven.logger.cacheOutputStream=true
24+
maven.logger.levelInBrackets=true
25+
maven.logger.log.Sisu=info
26+
maven.logger.warnLevelString=WARNING
2727

2828
# MNG-6181: mvn -X also prints all debug logging from HttpClient
29-
org.slf4j.simpleLogger.log.org.apache.http=off
30-
org.slf4j.simpleLogger.log.org.apache.http.wire=off
29+
maven.logger.log.org.apache.http=off
30+
maven.logger.log.org.apache.http.wire=off

api/maven-api-core/src/main/java/org/apache/maven/api/Constants.java

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,5 +453,117 @@ public final class Constants {
453453
@Config(type = "java.lang.Integer", defaultValue = "100")
454454
public static final String MAVEN_BUILDER_MAX_PROBLEMS = "maven.builder.maxProblems";
455455

456+
/**
457+
* All system properties used by Maven Logger start with this prefix.
458+
*
459+
* @since 4.0.0
460+
*/
461+
public static final String MAVEN_LOGGER_PREFIX = "maven.logger.";
462+
463+
/**
464+
* Default log level for all instances of SimpleLogger. Must be one of ("trace", "debug", "info",
465+
* "warn", "error" or "off"). If not specified, defaults to "info".
466+
*
467+
* @since 4.0.0
468+
*/
469+
@Config
470+
public static final String MAVEN_LOGGER_DEFAULT_LOG_LEVEL = MAVEN_LOGGER_PREFIX + "defaultLogLevel";
471+
472+
/**
473+
* Set to true if you want the current date and time to be included in output messages. Default is false.
474+
*
475+
* @since 4.0.0
476+
*/
477+
@Config(type = "java.lang.Boolean", defaultValue = "false")
478+
public static final String MAVEN_LOGGER_SHOW_DATE_TIME = MAVEN_LOGGER_PREFIX + "showDateTime";
479+
480+
/**
481+
* The date and time format to be used in the output messages. The pattern describing the date and
482+
* time format is defined by SimpleDateFormat. If the format is not specified or is invalid, the
483+
* number of milliseconds since start up will be output.
484+
*
485+
* @since 4.0.0
486+
*/
487+
@Config
488+
public static final String MAVEN_LOGGER_DATE_TIME_FORMAT = MAVEN_LOGGER_PREFIX + "dateTimeFormat";
489+
490+
/**
491+
* If you would like to output the current thread id, then set to true. Defaults to false.
492+
*
493+
* @since 4.0.0
494+
*/
495+
@Config(type = "java.lang.Boolean", defaultValue = "false")
496+
public static final String MAVEN_LOGGER_SHOW_THREAD_ID = MAVEN_LOGGER_PREFIX + "showThreadId";
497+
498+
/**
499+
* Set to true if you want to output the current thread name. Defaults to true.
500+
*
501+
* @since 4.0.0
502+
*/
503+
@Config(type = "java.lang.Boolean", defaultValue = "true")
504+
public static final String MAVEN_LOGGER_SHOW_THREAD_NAME = MAVEN_LOGGER_PREFIX + "showThreadName";
505+
506+
/**
507+
* Set to true if you want the Logger instance name to be included in output messages. Defaults to true.
508+
*
509+
* @since 4.0.0
510+
*/
511+
@Config(type = "java.lang.Boolean", defaultValue = "true")
512+
public static final String MAVEN_LOGGER_SHOW_LOG_NAME = MAVEN_LOGGER_PREFIX + "showLogName";
513+
514+
/**
515+
* Set to true if you want the last component of the name to be included in output messages. Defaults to false.
516+
*
517+
* @since 4.0.0
518+
*/
519+
@Config(type = "java.lang.Boolean", defaultValue = "false")
520+
public static final String MAVEN_LOGGER_SHOW_SHORT_LOG_NAME = MAVEN_LOGGER_PREFIX + "showShortLogName";
521+
522+
/**
523+
* The output target which can be the path to a file, or the special values "System.out" and "System.err".
524+
* Default is "System.err".
525+
*
526+
* @since 4.0.0
527+
*/
528+
@Config
529+
public static final String MAVEN_LOGGER_LOG_FILE = MAVEN_LOGGER_PREFIX + "logFile";
530+
531+
/**
532+
* Should the level string be output in brackets? Defaults to false.
533+
*
534+
* @since 4.0.0
535+
*/
536+
@Config(type = "java.lang.Boolean", defaultValue = "false")
537+
public static final String MAVEN_LOGGER_LEVEL_IN_BRACKETS = MAVEN_LOGGER_PREFIX + "levelInBrackets";
538+
539+
/**
540+
* The string value output for the warn level. Defaults to WARN.
541+
*
542+
* @since 4.0.0
543+
*/
544+
@Config(defaultValue = "WARN")
545+
public static final String MAVEN_LOGGER_WARN_LEVEL = MAVEN_LOGGER_PREFIX + "warnLevelString";
546+
547+
/**
548+
* If the output target is set to "System.out" or "System.err" (see preceding entry), by default, logs will
549+
* be output to the latest value referenced by System.out/err variables. By setting this parameter to true,
550+
* the output stream will be cached, i.e. assigned once at initialization time and re-used independently of
551+
* the current value referenced by System.out/err.
552+
*
553+
* @since 4.0.0
554+
*/
555+
@Config(type = "java.lang.Boolean", defaultValue = "false")
556+
public static final String MAVEN_LOGGER_CACHE_OUTPUT_STREAM = MAVEN_LOGGER_PREFIX + "cacheOutputStream";
557+
558+
/**
559+
* maven.logger.log.a.b.c - Logging detail level for a SimpleLogger instance named "a.b.c". Right-side value
560+
* must be one of "trace", "debug", "info", "warn", "error" or "off". When a logger named "a.b.c" is initialized,
561+
* its level is assigned from this property. If unspecified, the level of nearest parent logger will be used,
562+
* and if none is set, then the value specified by {@code maven.logger.defaultLogLevel} will be used.
563+
*
564+
* @since 4.0.0
565+
*/
566+
public static final String MAVEN_LOGGER_LOG_PREFIX = MAVEN_LOGGER_PREFIX + "log.";
567+
456568
private Constants() {}
457569
}

impl/maven-cli/src/main/java/org/apache/maven/cling/logging/impl/MavenSimpleConfiguration.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
*/
1919
package org.apache.maven.cling.logging.impl;
2020

21+
import org.apache.maven.api.Constants;
2122
import org.apache.maven.cling.logging.BaseSlf4jConfiguration;
2223
import org.apache.maven.slf4j.MavenLoggerFactory;
23-
import org.apache.maven.slf4j.MavenSimpleLogger;
2424
import org.slf4j.ILoggerFactory;
2525
import org.slf4j.Logger;
2626
import org.slf4j.LoggerFactory;
@@ -42,10 +42,10 @@ public void setRootLoggerLevel(Level level) {
4242
default -> "error";
4343
};
4444

45-
String current = System.setProperty(MavenSimpleLogger.DEFAULT_LOG_LEVEL_KEY, value);
45+
String current = System.setProperty(Constants.MAVEN_LOGGER_DEFAULT_LOG_LEVEL, value);
4646
if (current != null && !value.equalsIgnoreCase(current)) {
4747
LOGGER.info(
48-
"System property '" + MavenSimpleLogger.DEFAULT_LOG_LEVEL_KEY + "' is already set to '" + current
48+
"System property '" + Constants.MAVEN_LOGGER_DEFAULT_LOG_LEVEL + "' is already set to '" + current
4949
+ "' - ignoring system property and get log level from -X/-e/-q options, log level will be set to"
5050
+ value);
5151
}

impl/maven-logging/src/main/java/org/apache/maven/slf4j/MavenBaseLogger.java

Lines changed: 20 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.util.ArrayList;
2626
import java.util.List;
2727

28+
import org.apache.maven.api.Constants;
2829
import org.apache.maven.api.MonotonicClock;
2930
import org.slf4j.Logger;
3031
import org.slf4j.Marker;
@@ -43,61 +44,61 @@
4344
*
4445
*
4546
* <ul>
46-
* <li><code>org.slf4j.simpleLogger.logFile</code> - The output target which can
47+
* <li><code>maven.logger.logFile</code> - The output target which can
4748
* be the <em>path</em> to a file, or the special values "System.out" and
4849
* "System.err". Default is "System.err".</li>
4950
*
50-
* <li><code>org.slf4j.simpleLogger.cacheOutputStream</code> - If the output
51+
* <li><code>maven.logger.cacheOutputStream</code> - If the output
5152
* target is set to "System.out" or "System.err" (see preceding entry), by
5253
* default, logs will be output to the latest value referenced by
5354
* <code>System.out/err</code> variables. By setting this parameter to true, the
5455
* output stream will be cached, i.e. assigned once at initialization time and
5556
* re-used independently of the current value referenced by
5657
* <code>System.out/err</code>.</li>
5758
*
58-
* <li><code>org.slf4j.simpleLogger.defaultLogLevel</code> - Default log level
59+
* <li><code>maven.logger.defaultLogLevel</code> - Default log level
5960
* for all instances of SimpleLogger. Must be one of ("trace", "debug", "info",
6061
* "warn", "error" or "off"). If not specified, defaults to "info".</li>
6162
*
62-
* <li><code>org.slf4j.simpleLogger.log.<em>a.b.c</em></code> - Logging detail
63+
* <li><code>maven.logger.log.<em>a.b.c</em></code> - Logging detail
6364
* level for a SimpleLogger instance named "a.b.c". Right-side value must be one
6465
* of "trace", "debug", "info", "warn", "error" or "off". When a SimpleLogger
6566
* named "a.b.c" is initialized, its level is assigned from this property. If
6667
* unspecified, the level of nearest parent logger will be used, and if none is
6768
* set, then the value specified by
68-
* <code>org.slf4j.simpleLogger.defaultLogLevel</code> will be used.</li>
69+
* <code>maven.logger.defaultLogLevel</code> will be used.</li>
6970
*
70-
* <li><code>org.slf4j.simpleLogger.showDateTime</code> - Set to
71+
* <li><code>maven.logger.showDateTime</code> - Set to
7172
* <code>true</code> if you want the current date and time to be included in
7273
* output messages. Default is <code>false</code></li>
7374
*
74-
* <li><code>org.slf4j.simpleLogger.dateTimeFormat</code> - The date and time
75+
* <li><code>maven.logger.dateTimeFormat</code> - The date and time
7576
* format to be used in the output messages. The pattern describing the date and
7677
* time format is defined by <a href=
7778
* "http://docs.oracle.com/javase/1.5.0/docs/api/java/text/SimpleDateFormat.html">
7879
* <code>SimpleDateFormat</code></a>. If the format is not specified or is
7980
* invalid, the number of milliseconds since start up will be output.</li>
8081
*
81-
* <li><code>org.slf4j.simpleLogger.showThreadName</code> -Set to
82+
* <li><code>maven.logger.showThreadName</code> -Set to
8283
* <code>true</code> if you want to output the current thread name. Defaults to
8384
* <code>true</code>.</li>
8485
*
85-
* <li>(since version 1.7.33 and 2.0.0-alpha6) <code>org.slf4j.simpleLogger.showThreadId</code> -
86+
* <li>(since version 1.7.33 and 2.0.0-alpha6) <code>maven.logger.showThreadId</code> -
8687
* If you would like to output the current thread id, then set to
8788
* <code>true</code>. Defaults to <code>false</code>.</li>
8889
*
89-
* <li><code>org.slf4j.simpleLogger.showLogName</code> - Set to
90+
* <li><code>maven.logger.showLogName</code> - Set to
9091
* <code>true</code> if you want the Logger instance name to be included in
9192
* output messages. Defaults to <code>true</code>.</li>
9293
*
93-
* <li><code>org.slf4j.simpleLogger.showShortLogName</code> - Set to
94+
* <li><code>maven.logger.showShortLogName</code> - Set to
9495
* <code>true</code> if you want the last component of the name to be included
9596
* in output messages. Defaults to <code>false</code>.</li>
9697
*
97-
* <li><code>org.slf4j.simpleLogger.levelInBrackets</code> - Should the level
98+
* <li><code>maven.logger.levelInBrackets</code> - Should the level
9899
* string be output in brackets? Defaults to <code>false</code>.</li>
99100
*
100-
* <li><code>org.slf4j.simpleLogger.warnLevelString</code> - The string value
101+
* <li><code>maven.logger.warnLevelString</code> - The string value
101102
* output for the warn level. Defaults to <code>WARN</code>.</li>
102103
*
103104
* </ul>
@@ -184,34 +185,9 @@ static void init() {
184185
private transient String shortLogName = null;
185186

186187
/**
187-
* All system properties used by <code>SimpleLogger</code> start with this
188-
* prefix
188+
* Legacy SLF4J prefix maintained for backwards compatibility
189189
*/
190-
public static final String SYSTEM_PREFIX = "org.slf4j.simpleLogger.";
191-
192-
public static final String LOG_KEY_PREFIX = MavenBaseLogger.SYSTEM_PREFIX + "log.";
193-
194-
public static final String CACHE_OUTPUT_STREAM_STRING_KEY = MavenBaseLogger.SYSTEM_PREFIX + "cacheOutputStream";
195-
196-
public static final String WARN_LEVEL_STRING_KEY = MavenBaseLogger.SYSTEM_PREFIX + "warnLevelString";
197-
198-
public static final String LEVEL_IN_BRACKETS_KEY = MavenBaseLogger.SYSTEM_PREFIX + "levelInBrackets";
199-
200-
public static final String LOG_FILE_KEY = MavenBaseLogger.SYSTEM_PREFIX + "logFile";
201-
202-
public static final String SHOW_SHORT_LOG_NAME_KEY = MavenBaseLogger.SYSTEM_PREFIX + "showShortLogName";
203-
204-
public static final String SHOW_LOG_NAME_KEY = MavenBaseLogger.SYSTEM_PREFIX + "showLogName";
205-
206-
public static final String SHOW_THREAD_NAME_KEY = MavenBaseLogger.SYSTEM_PREFIX + "showThreadName";
207-
208-
public static final String SHOW_THREAD_ID_KEY = MavenBaseLogger.SYSTEM_PREFIX + "showThreadId";
209-
210-
public static final String DATE_TIME_FORMAT_KEY = MavenBaseLogger.SYSTEM_PREFIX + "dateTimeFormat";
211-
212-
public static final String SHOW_DATE_TIME_KEY = MavenBaseLogger.SYSTEM_PREFIX + "showDateTime";
213-
214-
public static final String DEFAULT_LOG_LEVEL_KEY = MavenBaseLogger.SYSTEM_PREFIX + "defaultLogLevel";
190+
public static final String LEGACY_PREFIX = "org.slf4j.simpleLogger.";
215191

216192
/**
217193
* Protected access allows only {@link MavenLoggerFactory} and also derived classes to instantiate
@@ -234,8 +210,8 @@ String recursivelyComputeLevelString() {
234210
int indexOfLastDot = tempName.length();
235211
while ((levelString == null) && (indexOfLastDot > -1)) {
236212
tempName = tempName.substring(0, indexOfLastDot);
237-
levelString = CONFIG_PARAMS.getStringProperty(MavenBaseLogger.LOG_KEY_PREFIX + tempName, null);
238-
indexOfLastDot = String.valueOf(tempName).lastIndexOf(".");
213+
levelString = CONFIG_PARAMS.getStringProperty(Constants.MAVEN_LOGGER_LOG_PREFIX + tempName, null);
214+
indexOfLastDot = tempName.lastIndexOf(".");
239215
}
240216
return levelString;
241217
}
@@ -244,8 +220,8 @@ String recursivelyComputeLevelString() {
244220
* To avoid intermingling of log messages and associated stack traces, the two
245221
* operations are done in a synchronized block.
246222
*
247-
* @param buf
248-
* @param t
223+
* @param buf The StringBuilder containing the log message to be written
224+
* @param t The Throwable object whose stack trace should be written, may be null
249225
*/
250226
protected void write(StringBuilder buf, Throwable t) {
251227
PrintStream targetStream = CONFIG_PARAMS.outputChoice.getTargetPrintStream();

0 commit comments

Comments
 (0)