Skip to content

Commit

Permalink
Move constants to Constants and document them
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet committed Jan 22, 2025
1 parent d3ab77a commit 7fb82db
Show file tree
Hide file tree
Showing 8 changed files with 415 additions and 178 deletions.
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.LoggerFactory;

Expand All @@ -38,7 +38,7 @@ public void setRootLoggerLevel(Level level) {
case INFO -> "info";
default -> "error";
};
System.setProperty(MavenSimpleLogger.DEFAULT_LOG_LEVEL_KEY, value);
System.setProperty(Constants.MAVEN_LOGGER_DEFAULT_LOG_LEVEL, value);
}

@Override
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 Down Expand Up @@ -183,30 +184,11 @@ static void init() {
/** The short name of this simple log instance */
private transient String shortLogName = null;

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

/**
* 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
* MavenLoggerFactory instances.
Expand All @@ -228,7 +210,7 @@ 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);
levelString = CONFIG_PARAMS.getStringProperty(Constants.MAVEN_LOGGER_LOG_PREFIX + tempName, null);
indexOfLastDot = tempName.lastIndexOf(".");
}
return levelString;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.time.format.DateTimeFormatter;
import java.util.Properties;

import org.apache.maven.api.Constants;
import org.apache.maven.slf4j.OutputChoice.OutputChoiceType;
import org.slf4j.helpers.Reporter;

Expand Down Expand Up @@ -96,28 +97,26 @@ void init() {

loadProperties();

String defaultLogLevelString = getStringProperty(MavenBaseLogger.DEFAULT_LOG_LEVEL_KEY, null);
String defaultLogLevelString = getStringProperty(Constants.MAVEN_LOGGER_DEFAULT_LOG_LEVEL, null);
if (defaultLogLevelString != null) {
defaultLogLevel = stringToLevel(defaultLogLevelString);
}

// local variable,
String dateTimeFormatStr;

showLogName =
getBooleanProperty(MavenBaseLogger.SHOW_LOG_NAME_KEY, SimpleLoggerConfiguration.SHOW_LOG_NAME_DEFAULT);
showShortLogName = getBooleanProperty(MavenBaseLogger.SHOW_SHORT_LOG_NAME_KEY, SHOW_SHORT_LOG_NAME_DEFAULT);
showDateTime = getBooleanProperty(MavenBaseLogger.SHOW_DATE_TIME_KEY, SHOW_DATE_TIME_DEFAULT);
showThreadName = getBooleanProperty(MavenBaseLogger.SHOW_THREAD_NAME_KEY, SHOW_THREAD_NAME_DEFAULT);
showThreadId = getBooleanProperty(MavenBaseLogger.SHOW_THREAD_ID_KEY, SHOW_THREAD_ID_DEFAULT);
dateTimeFormatStr = getStringProperty(MavenBaseLogger.DATE_TIME_FORMAT_KEY, DATE_TIME_FORMAT_STR_DEFAULT);
levelInBrackets = getBooleanProperty(MavenBaseLogger.LEVEL_IN_BRACKETS_KEY, LEVEL_IN_BRACKETS_DEFAULT);
warnLevelString = getStringProperty(MavenBaseLogger.WARN_LEVEL_STRING_KEY, WARN_LEVELS_STRING_DEFAULT);
showLogName = getBooleanProperty(Constants.MAVEN_LOGGER_SHOW_LOG_NAME, SHOW_LOG_NAME_DEFAULT);
showShortLogName = getBooleanProperty(Constants.MAVEN_LOGGER_SHOW_SHORT_LOG_NAME, SHOW_SHORT_LOG_NAME_DEFAULT);
showDateTime = getBooleanProperty(Constants.MAVEN_LOGGER_SHOW_DATE_TIME, SHOW_DATE_TIME_DEFAULT);
showThreadName = getBooleanProperty(Constants.MAVEN_LOGGER_SHOW_THREAD_NAME, SHOW_THREAD_NAME_DEFAULT);
showThreadId = getBooleanProperty(Constants.MAVEN_LOGGER_SHOW_THREAD_ID, SHOW_THREAD_ID_DEFAULT);
dateTimeFormatStr = getStringProperty(Constants.MAVEN_LOGGER_DATE_TIME_FORMAT, DATE_TIME_FORMAT_STR_DEFAULT);
levelInBrackets = getBooleanProperty(Constants.MAVEN_LOGGER_LEVEL_IN_BRACKETS, LEVEL_IN_BRACKETS_DEFAULT);
warnLevelString = getStringProperty(Constants.MAVEN_LOGGER_WARN_LEVEL, WARN_LEVELS_STRING_DEFAULT);

logFile = getStringProperty(MavenBaseLogger.LOG_FILE_KEY, logFile);
logFile = getStringProperty(Constants.MAVEN_LOGGER_LOG_FILE, logFile);

cacheOutputStream =
getBooleanProperty(MavenBaseLogger.CACHE_OUTPUT_STREAM_STRING_KEY, CACHE_OUTPUT_STREAM_DEFAULT);
cacheOutputStream = getBooleanProperty(Constants.MAVEN_LOGGER_CACHE_OUTPUT_STREAM, CACHE_OUTPUT_STREAM_DEFAULT);
outputChoice = computeOutputChoice(logFile, cacheOutputStream);

if (dateTimeFormatStr != null) {
Expand Down Expand Up @@ -155,7 +154,7 @@ private void loadProperties() {
}
// 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);
String mavenKey = propName.replace(MavenBaseLogger.LEGACY_PREFIX, Constants.MAVEN_LOGGER_PREFIX);
if (!properties.containsKey(mavenKey)) {
properties.setProperty(mavenKey, legacyProps.getProperty(propName));
}
Expand All @@ -181,9 +180,9 @@ String getStringProperty(String name) {
try {
// Try maven property first
prop = System.getProperty(name);
if (prop == null && name.startsWith(MavenBaseLogger.MAVEN_PREFIX)) {
if (prop == null && name.startsWith(Constants.MAVEN_LOGGER_PREFIX)) {
// Try legacy property
String legacyName = name.replace(MavenBaseLogger.MAVEN_PREFIX, MavenBaseLogger.LEGACY_PREFIX);
String legacyName = name.replace(Constants.MAVEN_LOGGER_PREFIX, MavenBaseLogger.LEGACY_PREFIX);
prop = System.getProperty(legacyName);
if (prop != null) {
Reporter.warn("Using deprecated property " + legacyName + ". Please migrate to " + name);
Expand All @@ -195,9 +194,9 @@ String getStringProperty(String name) {

if (prop == null) {
prop = properties.getProperty(name);
if (prop == null && name.startsWith(MavenBaseLogger.MAVEN_PREFIX)) {
if (prop == null && name.startsWith(Constants.MAVEN_LOGGER_PREFIX)) {
// Try legacy property from properties file
String legacyName = name.replace(MavenBaseLogger.MAVEN_PREFIX, MavenBaseLogger.LEGACY_PREFIX);
String legacyName = name.replace(Constants.MAVEN_LOGGER_PREFIX, MavenBaseLogger.LEGACY_PREFIX);
prop = properties.getProperty(legacyName);
if (prop != null) {
Reporter.warn("Using deprecated property " + legacyName + ". Please migrate to " + name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

import org.apache.maven.api.Constants;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -39,8 +40,8 @@ class MavenBaseLoggerTimestampTest {
@BeforeEach
void setUp() {
// Reset configuration before each test
System.clearProperty(MavenBaseLogger.SHOW_DATE_TIME_KEY);
System.clearProperty(MavenBaseLogger.DATE_TIME_FORMAT_KEY);
System.clearProperty(Constants.MAVEN_LOGGER_SHOW_DATE_TIME);
System.clearProperty(Constants.MAVEN_LOGGER_DATE_TIME_FORMAT);

// Reset static initialization flag
MavenBaseLogger.initialized = false;
Expand All @@ -54,15 +55,15 @@ void setUp() {
@AfterEach
void tearDown() {
System.setErr(originalErr);
System.clearProperty(MavenBaseLogger.SHOW_DATE_TIME_KEY);
System.clearProperty(MavenBaseLogger.DATE_TIME_FORMAT_KEY);
System.clearProperty(Constants.MAVEN_LOGGER_SHOW_DATE_TIME);
System.clearProperty(Constants.MAVEN_LOGGER_DATE_TIME_FORMAT);
MavenBaseLogger.initialized = false;
}

@Test
void whenShowDateTimeIsFalse_shouldNotIncludeTimestamp() {
// Given
System.setProperty(MavenBaseLogger.SHOW_DATE_TIME_KEY, "false");
System.setProperty(Constants.MAVEN_LOGGER_SHOW_DATE_TIME, "false");
initializeLogger();

// When
Expand All @@ -77,7 +78,7 @@ void whenShowDateTimeIsFalse_shouldNotIncludeTimestamp() {
@Test
void whenShowDateTimeIsTrue_withoutFormat_shouldShowElapsedTime() { // Changed test name and expectation
// Given
System.setProperty(MavenBaseLogger.SHOW_DATE_TIME_KEY, "true");
System.setProperty(Constants.MAVEN_LOGGER_SHOW_DATE_TIME, "true");
initializeLogger();

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

// When
Expand Down Expand Up @@ -124,8 +125,8 @@ void whenCustomDateFormat_shouldFormatCorrectly(String dateFormat) {
@Test
void whenInvalidDateFormat_shouldUseElapsedMillis() {
// Given
System.setProperty(MavenBaseLogger.SHOW_DATE_TIME_KEY, "true");
System.setProperty(MavenBaseLogger.DATE_TIME_FORMAT_KEY, "invalid-format");
System.setProperty(Constants.MAVEN_LOGGER_SHOW_DATE_TIME, "true");
System.setProperty(Constants.MAVEN_LOGGER_DATE_TIME_FORMAT, "invalid-format");
initializeLogger();

// When
Expand Down
Loading

0 comments on commit 7fb82db

Please sign in to comment.