Skip to content

Commit 7fb82db

Browse files
committed
Move constants to Constants and document them
1 parent d3ab77a commit 7fb82db

File tree

8 files changed

+415
-178
lines changed

8 files changed

+415
-178
lines changed

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: 2 additions & 2 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.LoggerFactory;
2626

@@ -38,7 +38,7 @@ public void setRootLoggerLevel(Level level) {
3838
case INFO -> "info";
3939
default -> "error";
4040
};
41-
System.setProperty(MavenSimpleLogger.DEFAULT_LOG_LEVEL_KEY, value);
41+
System.setProperty(Constants.MAVEN_LOGGER_DEFAULT_LOG_LEVEL, value);
4242
}
4343

4444
@Override

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

Lines changed: 2 additions & 20 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;
@@ -183,30 +184,11 @@ static void init() {
183184
/** The short name of this simple log instance */
184185
private transient String shortLogName = null;
185186

186-
/**
187-
* All system properties used by Maven Logger start with this prefix.
188-
*/
189-
public static final String MAVEN_PREFIX = "maven.logger.";
190-
191187
/**
192188
* Legacy SLF4J prefix maintained for backwards compatibility
193189
*/
194190
public static final String LEGACY_PREFIX = "org.slf4j.simpleLogger.";
195191

196-
// Property keys with new maven prefix
197-
public static final String LOG_KEY_PREFIX = MAVEN_PREFIX + "log.";
198-
public static final String CACHE_OUTPUT_STREAM_STRING_KEY = MAVEN_PREFIX + "cacheOutputStream";
199-
public static final String WARN_LEVEL_STRING_KEY = MAVEN_PREFIX + "warnLevelString";
200-
public static final String LEVEL_IN_BRACKETS_KEY = MAVEN_PREFIX + "levelInBrackets";
201-
public static final String LOG_FILE_KEY = MAVEN_PREFIX + "logFile";
202-
public static final String SHOW_SHORT_LOG_NAME_KEY = MAVEN_PREFIX + "showShortLogName";
203-
public static final String SHOW_LOG_NAME_KEY = MAVEN_PREFIX + "showLogName";
204-
public static final String SHOW_THREAD_NAME_KEY = MAVEN_PREFIX + "showThreadName";
205-
public static final String SHOW_THREAD_ID_KEY = MAVEN_PREFIX + "showThreadId";
206-
public static final String DATE_TIME_FORMAT_KEY = MAVEN_PREFIX + "dateTimeFormat";
207-
public static final String SHOW_DATE_TIME_KEY = MAVEN_PREFIX + "showDateTime";
208-
public static final String DEFAULT_LOG_LEVEL_KEY = MAVEN_PREFIX + "defaultLogLevel";
209-
210192
/**
211193
* Protected access allows only {@link MavenLoggerFactory} and also derived classes to instantiate
212194
* MavenLoggerFactory instances.
@@ -228,7 +210,7 @@ String recursivelyComputeLevelString() {
228210
int indexOfLastDot = tempName.length();
229211
while ((levelString == null) && (indexOfLastDot > -1)) {
230212
tempName = tempName.substring(0, indexOfLastDot);
231-
levelString = CONFIG_PARAMS.getStringProperty(MavenBaseLogger.LOG_KEY_PREFIX + tempName, null);
213+
levelString = CONFIG_PARAMS.getStringProperty(Constants.MAVEN_LOGGER_LOG_PREFIX + tempName, null);
232214
indexOfLastDot = tempName.lastIndexOf(".");
233215
}
234216
return levelString;

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

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.time.format.DateTimeFormatter;
2626
import java.util.Properties;
2727

28+
import org.apache.maven.api.Constants;
2829
import org.apache.maven.slf4j.OutputChoice.OutputChoiceType;
2930
import org.slf4j.helpers.Reporter;
3031

@@ -96,28 +97,26 @@ void init() {
9697

9798
loadProperties();
9899

99-
String defaultLogLevelString = getStringProperty(MavenBaseLogger.DEFAULT_LOG_LEVEL_KEY, null);
100+
String defaultLogLevelString = getStringProperty(Constants.MAVEN_LOGGER_DEFAULT_LOG_LEVEL, null);
100101
if (defaultLogLevelString != null) {
101102
defaultLogLevel = stringToLevel(defaultLogLevelString);
102103
}
103104

104105
// local variable,
105106
String dateTimeFormatStr;
106107

107-
showLogName =
108-
getBooleanProperty(MavenBaseLogger.SHOW_LOG_NAME_KEY, SimpleLoggerConfiguration.SHOW_LOG_NAME_DEFAULT);
109-
showShortLogName = getBooleanProperty(MavenBaseLogger.SHOW_SHORT_LOG_NAME_KEY, SHOW_SHORT_LOG_NAME_DEFAULT);
110-
showDateTime = getBooleanProperty(MavenBaseLogger.SHOW_DATE_TIME_KEY, SHOW_DATE_TIME_DEFAULT);
111-
showThreadName = getBooleanProperty(MavenBaseLogger.SHOW_THREAD_NAME_KEY, SHOW_THREAD_NAME_DEFAULT);
112-
showThreadId = getBooleanProperty(MavenBaseLogger.SHOW_THREAD_ID_KEY, SHOW_THREAD_ID_DEFAULT);
113-
dateTimeFormatStr = getStringProperty(MavenBaseLogger.DATE_TIME_FORMAT_KEY, DATE_TIME_FORMAT_STR_DEFAULT);
114-
levelInBrackets = getBooleanProperty(MavenBaseLogger.LEVEL_IN_BRACKETS_KEY, LEVEL_IN_BRACKETS_DEFAULT);
115-
warnLevelString = getStringProperty(MavenBaseLogger.WARN_LEVEL_STRING_KEY, WARN_LEVELS_STRING_DEFAULT);
108+
showLogName = getBooleanProperty(Constants.MAVEN_LOGGER_SHOW_LOG_NAME, SHOW_LOG_NAME_DEFAULT);
109+
showShortLogName = getBooleanProperty(Constants.MAVEN_LOGGER_SHOW_SHORT_LOG_NAME, SHOW_SHORT_LOG_NAME_DEFAULT);
110+
showDateTime = getBooleanProperty(Constants.MAVEN_LOGGER_SHOW_DATE_TIME, SHOW_DATE_TIME_DEFAULT);
111+
showThreadName = getBooleanProperty(Constants.MAVEN_LOGGER_SHOW_THREAD_NAME, SHOW_THREAD_NAME_DEFAULT);
112+
showThreadId = getBooleanProperty(Constants.MAVEN_LOGGER_SHOW_THREAD_ID, SHOW_THREAD_ID_DEFAULT);
113+
dateTimeFormatStr = getStringProperty(Constants.MAVEN_LOGGER_DATE_TIME_FORMAT, DATE_TIME_FORMAT_STR_DEFAULT);
114+
levelInBrackets = getBooleanProperty(Constants.MAVEN_LOGGER_LEVEL_IN_BRACKETS, LEVEL_IN_BRACKETS_DEFAULT);
115+
warnLevelString = getStringProperty(Constants.MAVEN_LOGGER_WARN_LEVEL, WARN_LEVELS_STRING_DEFAULT);
116116

117-
logFile = getStringProperty(MavenBaseLogger.LOG_FILE_KEY, logFile);
117+
logFile = getStringProperty(Constants.MAVEN_LOGGER_LOG_FILE, logFile);
118118

119-
cacheOutputStream =
120-
getBooleanProperty(MavenBaseLogger.CACHE_OUTPUT_STREAM_STRING_KEY, CACHE_OUTPUT_STREAM_DEFAULT);
119+
cacheOutputStream = getBooleanProperty(Constants.MAVEN_LOGGER_CACHE_OUTPUT_STREAM, CACHE_OUTPUT_STREAM_DEFAULT);
121120
outputChoice = computeOutputChoice(logFile, cacheOutputStream);
122121

123122
if (dateTimeFormatStr != null) {
@@ -155,7 +154,7 @@ private void loadProperties() {
155154
}
156155
// Only load legacy properties if there's no maven equivalent
157156
for (String propName : legacyProps.stringPropertyNames()) {
158-
String mavenKey = propName.replace(MavenBaseLogger.LEGACY_PREFIX, MavenBaseLogger.MAVEN_PREFIX);
157+
String mavenKey = propName.replace(MavenBaseLogger.LEGACY_PREFIX, Constants.MAVEN_LOGGER_PREFIX);
159158
if (!properties.containsKey(mavenKey)) {
160159
properties.setProperty(mavenKey, legacyProps.getProperty(propName));
161160
}
@@ -181,9 +180,9 @@ String getStringProperty(String name) {
181180
try {
182181
// Try maven property first
183182
prop = System.getProperty(name);
184-
if (prop == null && name.startsWith(MavenBaseLogger.MAVEN_PREFIX)) {
183+
if (prop == null && name.startsWith(Constants.MAVEN_LOGGER_PREFIX)) {
185184
// Try legacy property
186-
String legacyName = name.replace(MavenBaseLogger.MAVEN_PREFIX, MavenBaseLogger.LEGACY_PREFIX);
185+
String legacyName = name.replace(Constants.MAVEN_LOGGER_PREFIX, MavenBaseLogger.LEGACY_PREFIX);
187186
prop = System.getProperty(legacyName);
188187
if (prop != null) {
189188
Reporter.warn("Using deprecated property " + legacyName + ". Please migrate to " + name);
@@ -195,9 +194,9 @@ String getStringProperty(String name) {
195194

196195
if (prop == null) {
197196
prop = properties.getProperty(name);
198-
if (prop == null && name.startsWith(MavenBaseLogger.MAVEN_PREFIX)) {
197+
if (prop == null && name.startsWith(Constants.MAVEN_LOGGER_PREFIX)) {
199198
// Try legacy property from properties file
200-
String legacyName = name.replace(MavenBaseLogger.MAVEN_PREFIX, MavenBaseLogger.LEGACY_PREFIX);
199+
String legacyName = name.replace(Constants.MAVEN_LOGGER_PREFIX, MavenBaseLogger.LEGACY_PREFIX);
201200
prop = properties.getProperty(legacyName);
202201
if (prop != null) {
203202
Reporter.warn("Using deprecated property " + legacyName + ". Please migrate to " + name);

impl/maven-logging/src/test/java/org/apache/maven/slf4j/MavenBaseLoggerTimestampTest.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.io.ByteArrayOutputStream;
2222
import java.io.PrintStream;
2323

24+
import org.apache.maven.api.Constants;
2425
import org.junit.jupiter.api.AfterEach;
2526
import org.junit.jupiter.api.BeforeEach;
2627
import org.junit.jupiter.api.Test;
@@ -39,8 +40,8 @@ class MavenBaseLoggerTimestampTest {
3940
@BeforeEach
4041
void setUp() {
4142
// Reset configuration before each test
42-
System.clearProperty(MavenBaseLogger.SHOW_DATE_TIME_KEY);
43-
System.clearProperty(MavenBaseLogger.DATE_TIME_FORMAT_KEY);
43+
System.clearProperty(Constants.MAVEN_LOGGER_SHOW_DATE_TIME);
44+
System.clearProperty(Constants.MAVEN_LOGGER_DATE_TIME_FORMAT);
4445

4546
// Reset static initialization flag
4647
MavenBaseLogger.initialized = false;
@@ -54,15 +55,15 @@ void setUp() {
5455
@AfterEach
5556
void tearDown() {
5657
System.setErr(originalErr);
57-
System.clearProperty(MavenBaseLogger.SHOW_DATE_TIME_KEY);
58-
System.clearProperty(MavenBaseLogger.DATE_TIME_FORMAT_KEY);
58+
System.clearProperty(Constants.MAVEN_LOGGER_SHOW_DATE_TIME);
59+
System.clearProperty(Constants.MAVEN_LOGGER_DATE_TIME_FORMAT);
5960
MavenBaseLogger.initialized = false;
6061
}
6162

6263
@Test
6364
void whenShowDateTimeIsFalse_shouldNotIncludeTimestamp() {
6465
// Given
65-
System.setProperty(MavenBaseLogger.SHOW_DATE_TIME_KEY, "false");
66+
System.setProperty(Constants.MAVEN_LOGGER_SHOW_DATE_TIME, "false");
6667
initializeLogger();
6768

6869
// When
@@ -77,7 +78,7 @@ void whenShowDateTimeIsFalse_shouldNotIncludeTimestamp() {
7778
@Test
7879
void whenShowDateTimeIsTrue_withoutFormat_shouldShowElapsedTime() { // Changed test name and expectation
7980
// Given
80-
System.setProperty(MavenBaseLogger.SHOW_DATE_TIME_KEY, "true");
81+
System.setProperty(Constants.MAVEN_LOGGER_SHOW_DATE_TIME, "true");
8182
initializeLogger();
8283

8384
// When
@@ -95,8 +96,8 @@ void whenShowDateTimeIsTrue_withoutFormat_shouldShowElapsedTime() { // Changed t
9596
@ValueSource(strings = {"yyyy-MM-dd HH:mm:ss", "dd/MM/yyyy HH:mm:ss.SSS", "HH:mm:ss"})
9697
void whenCustomDateFormat_shouldFormatCorrectly(String dateFormat) {
9798
// Given
98-
System.setProperty(MavenBaseLogger.SHOW_DATE_TIME_KEY, "true");
99-
System.setProperty(MavenBaseLogger.DATE_TIME_FORMAT_KEY, dateFormat);
99+
System.setProperty(Constants.MAVEN_LOGGER_SHOW_DATE_TIME, "true");
100+
System.setProperty(Constants.MAVEN_LOGGER_DATE_TIME_FORMAT, dateFormat);
100101
initializeLogger();
101102

102103
// When
@@ -124,8 +125,8 @@ void whenCustomDateFormat_shouldFormatCorrectly(String dateFormat) {
124125
@Test
125126
void whenInvalidDateFormat_shouldUseElapsedMillis() {
126127
// Given
127-
System.setProperty(MavenBaseLogger.SHOW_DATE_TIME_KEY, "true");
128-
System.setProperty(MavenBaseLogger.DATE_TIME_FORMAT_KEY, "invalid-format");
128+
System.setProperty(Constants.MAVEN_LOGGER_SHOW_DATE_TIME, "true");
129+
System.setProperty(Constants.MAVEN_LOGGER_DATE_TIME_FORMAT, "invalid-format");
129130
initializeLogger();
130131

131132
// When

0 commit comments

Comments
 (0)