Skip to content

Commit

Permalink
Switch to slf4j simple logger (fixes #835) (#868)
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet committed Jul 10, 2023
1 parent daf68fd commit f8adf1b
Show file tree
Hide file tree
Showing 36 changed files with 1,143 additions and 471 deletions.
8 changes: 4 additions & 4 deletions client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@
<artifactId>mvnd-common</artifactId>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-embedder</artifactId>
<groupId>org.apache.maven.daemon</groupId>
<artifactId>mvnd-logging</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<groupId>org.apache.maven</groupId>
<artifactId>maven-embedder</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,6 @@

public class DefaultClient {
public static void main(String[] argv) throws Exception {
final String logbackConfFallback = System.getProperty("logback.configurationFile.fallback");
if (null != logbackConfFallback && !"".equals(logbackConfFallback)) {
System.setProperty("logback.configurationFile", logbackConfFallback);
System.clearProperty("logback.configurationFile.fallback");
}

final Logger LOGGER = LoggerFactory.getLogger(DefaultClient.class);
LOGGER.warn("Found old JDK, fallback to the embedded maven!");
LOGGER.warn("Use JDK 11+ to run maven-mvnd client!");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.bridge.SLF4JBridgeHandler;
import org.slf4j.impl.MvndLoggerFactory;
import org.slf4j.impl.StaticLoggerBinder;

import static org.mvndaemon.mvnd.client.DaemonParameters.LOG_EXTENSION;

Expand All @@ -70,8 +72,6 @@ public class DefaultClient implements Client {
private final DaemonParameters parameters;

public static void main(String[] argv) throws Exception {
System.clearProperty("logback.configurationFile.fallback");

final List<String> args = new ArrayList<>(Arrays.asList(argv));

// Log file
Expand Down Expand Up @@ -115,6 +115,10 @@ public static void main(String[] argv) throws Exception {
// System properties
setSystemPropertiesFromCommandLine(args);

if (StaticLoggerBinder.getSingleton().getLoggerFactory() instanceof MvndLoggerFactory) {
((MvndLoggerFactory) StaticLoggerBinder.getSingleton().getLoggerFactory()).reconfigure();
}

DaemonParameters parameters = new DaemonParameters();
if (parameters.serial()) {
System.setProperty(Environment.MVND_THREADS.getProperty(), Integer.toString(1));
Expand Down Expand Up @@ -191,9 +195,13 @@ public static void setSystemPropertiesFromCommandLine(List<String> args) {
/* This needs to be done very early, otherwise various DaemonParameters do not work properly */
final int eqPos = val.indexOf('=');
if (eqPos >= 0) {
System.setProperty(val.substring(0, eqPos), val.substring(eqPos + 1));
String k = val.substring(0, eqPos);
String v = val.substring(eqPos + 1);
System.setProperty(k, v);
LOGGER.trace("Setting system property {} to {}", k, v);
} else {
System.setProperty(val, "");
LOGGER.trace("Setting system property {}", val);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import org.mvndaemon.mvnd.common.logging.ClientOutput;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.impl.SimpleLogger;

import static java.lang.Thread.sleep;
import static org.mvndaemon.mvnd.common.DaemonState.Canceled;
Expand Down Expand Up @@ -433,10 +434,11 @@ private Process startDaemonProcess(String daemonId, ClientOutput output) {
args.add("-Dmaven.conf=" + mvndHome.resolve("mvn").resolve("conf"));
args.add("-Dclassworlds.conf=" + mvndHome.resolve("bin").resolve("mvnd-daemon.conf"));

args.add("-D" + SimpleLogger.LOG_FILE_KEY + "="
+ parameters.daemonStorage().resolve("daemon-" + daemonId + ".log"));

Environment.MVND_JAVA_HOME.addSystemProperty(
args, parameters.javaHome().toString());
Environment.LOGBACK_CONFIGURATION_FILE.addSystemProperty(
args, parameters.logbackConfigurationPath().toString());
Environment.MVND_ID.addSystemProperty(args, daemonId);
Environment.MVND_DAEMON_STORAGE.addSystemProperty(
args, parameters.daemonStorage().toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,11 +184,21 @@ private static String javaHomeFromPath() {
}

public Path userDir() {
return value(Environment.USER_DIR).orSystemProperty().orFail().asPath().toAbsolutePath();
return value(Environment.USER_DIR)
.orSystemProperty()
.orFail()
.cache(provider)
.asPath()
.toAbsolutePath();
}

public Path userHome() {
return value(Environment.USER_HOME).orSystemProperty().orFail().asPath().toAbsolutePath();
return value(Environment.USER_HOME)
.orSystemProperty()
.orFail()
.cache(provider)
.asPath()
.toAbsolutePath();
}

public Path suppliedPropertiesPath() {
Expand Down Expand Up @@ -256,15 +266,6 @@ public Path multiModuleProjectDirectory(Path projectDir) {
.normalize();
}

public Path logbackConfigurationPath() {
return property(Environment.MVND_LOGBACK)
.orDefault(() -> mvndHome()
.resolve("mvn/conf/logging/logback-daemon.xml")
.toString())
.orFail()
.asPath();
}

public String minHeapSize() {
return property(Environment.MVND_MIN_HEAP_SIZE).asString();
}
Expand Down
11 changes: 0 additions & 11 deletions common/src/main/java/org/mvndaemon/mvnd/common/Environment.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,6 @@ public enum Environment {
/** Use one thread, no log buffering and the default project builder to behave like a standard maven */
SERIAL("mvnd.serial", null, Boolean.FALSE, OptionType.VOID, Flags.OPTIONAL, "mvnd:-1", "mvnd:--serial"),

//
// Log properties
//

/**
* The location of the Logback configuration file the daemon should use to configure its logging.
*/
MVND_LOGBACK("mvnd.logback", null, null, OptionType.PATH, Flags.NONE),
/** The system property expected by logback to set the configuration file */
LOGBACK_CONFIGURATION_FILE("logback.configurationFile", null, null, OptionType.PATH, Flags.INTERNAL),

//
// System properties
//
Expand Down
35 changes: 18 additions & 17 deletions daemon-m39/src/main/java/org/apache/maven/cli/DaemonMavenCli.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@
import org.apache.maven.cli.event.ExecutionEventLogger;
import org.apache.maven.cli.internal.BootstrapCoreExtensionManager;
import org.apache.maven.cli.internal.extension.model.CoreExtension;
import org.apache.maven.cli.logging.Slf4jConfiguration;
import org.apache.maven.cli.logging.Slf4jConfigurationFactory;
import org.apache.maven.cli.transfer.QuietMavenTransferListener;
import org.apache.maven.cli.transfer.Slf4jMavenTransferListener;
import org.apache.maven.eventspy.internal.EventSpyDispatcher;
Expand Down Expand Up @@ -113,6 +115,8 @@
import org.slf4j.ILoggerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.impl.MvndSimpleLogger;
import org.slf4j.spi.LocationAwareLogger;
import org.sonatype.plexus.components.sec.dispatcher.DefaultSecDispatcher;
import org.sonatype.plexus.components.sec.dispatcher.SecDispatcher;

Expand Down Expand Up @@ -365,15 +369,17 @@ void logging(CliRequest cliRequest) {
cliRequest.quiet = !cliRequest.debug && cliRequest.commandLine.hasOption(CLIManager.QUIET);
cliRequest.showErrors = cliRequest.debug || cliRequest.commandLine.hasOption(CLIManager.ERRORS);

ch.qos.logback.classic.Level level;
Slf4jConfiguration slf4jConfiguration = Slf4jConfigurationFactory.getConfiguration(slf4jLoggerFactory);

if (cliRequest.debug) {
level = ch.qos.logback.classic.Level.DEBUG;
cliRequest.request.setLoggingLevel(MavenExecutionRequest.LOGGING_LEVEL_DEBUG);
slf4jConfiguration.setRootLoggerLevel(Slf4jConfiguration.Level.DEBUG);
} else if (cliRequest.quiet) {
level = ch.qos.logback.classic.Level.WARN;
} else {
level = ch.qos.logback.classic.Level.INFO;
cliRequest.request.setLoggingLevel(MavenExecutionRequest.LOGGING_LEVEL_ERROR);
slf4jConfiguration.setRootLoggerLevel(Slf4jConfiguration.Level.ERROR);
}
((ch.qos.logback.classic.Logger) slf4jLoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME)).setLevel(level);
// else fall back to default log level specified in conf
// see https://issues.apache.org/jira/browse/MNG-2570

// LOG COLOR
String styleColor = cliRequest.getUserProperties().getProperty(STYLE_COLOR_PROPERTY, "auto");
Expand All @@ -389,11 +395,6 @@ void logging(CliRequest cliRequest) {
MessageUtils.setColorEnabled(false);
}

// Workaround for https://github.com/apache/maven-mvnd/issues/39
final ch.qos.logback.classic.Logger mvndLogger =
(ch.qos.logback.classic.Logger) slf4jLoggerFactory.getLogger("org.mvndaemon.mvnd");
mvndLogger.setLevel(ch.qos.logback.classic.Level.toLevel(System.getProperty("mvnd.log.level"), null));

// LOG STREAMS
if (cliRequest.commandLine.hasOption(CLIManager.LOG_FILE)) {
File logFile = new File(cliRequest.commandLine.getOptionValue(CLIManager.LOG_FILE));
Expand All @@ -413,15 +414,15 @@ void logging(CliRequest cliRequest) {
.asOptional()
.map(Boolean::parseBoolean)
.orElse(Boolean.FALSE)) {
ch.qos.logback.classic.Logger stdout =
(ch.qos.logback.classic.Logger) slf4jLoggerFactory.getLogger("stdout");
ch.qos.logback.classic.Logger stderr =
(ch.qos.logback.classic.Logger) slf4jLoggerFactory.getLogger("stderr");
stdout.setLevel(ch.qos.logback.classic.Level.INFO);
stderr.setLevel(ch.qos.logback.classic.Level.INFO);
MvndSimpleLogger stdout = (MvndSimpleLogger) slf4jLoggerFactory.getLogger("stdout");
MvndSimpleLogger stderr = (MvndSimpleLogger) slf4jLoggerFactory.getLogger("stderr");
stdout.setLogLevel(LocationAwareLogger.INFO_INT);
stderr.setLogLevel(LocationAwareLogger.INFO_INT);
System.setOut(new LoggingOutputStream(s -> stdout.info("[stdout] " + s)).printStream());
System.setErr(new LoggingOutputStream(s -> stderr.warn("[stderr] " + s)).printStream());
}

slf4jConfiguration.activate();
}

private void version(CliRequest cliRequest) throws ExitException {
Expand Down
35 changes: 18 additions & 17 deletions daemon-m40/src/main/java/org/apache/maven/cli/DaemonMavenCli.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@
import org.apache.maven.cli.internal.extension.model.CoreExtension;
import org.apache.maven.cli.jansi.JansiMessageBuilderFactory;
import org.apache.maven.cli.jansi.MessageUtils;
import org.apache.maven.cli.logging.Slf4jConfiguration;
import org.apache.maven.cli.logging.Slf4jConfigurationFactory;
import org.apache.maven.cli.transfer.QuietMavenTransferListener;
import org.apache.maven.cli.transfer.Slf4jMavenTransferListener;
import org.apache.maven.eventspy.internal.EventSpyDispatcher;
Expand Down Expand Up @@ -103,6 +105,8 @@
import org.slf4j.ILoggerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.impl.MvndSimpleLogger;
import org.slf4j.spi.LocationAwareLogger;
import org.sonatype.plexus.components.sec.dispatcher.DefaultSecDispatcher;
import org.sonatype.plexus.components.sec.dispatcher.SecDispatcher;

Expand Down Expand Up @@ -411,15 +415,17 @@ void logging(CliRequest cliRequest) {
cliRequest.quiet = !cliRequest.verbose && commandLine.hasOption(CLIManager.QUIET);
cliRequest.showErrors = cliRequest.verbose || commandLine.hasOption(CLIManager.ERRORS);

ch.qos.logback.classic.Level level;
Slf4jConfiguration slf4jConfiguration = Slf4jConfigurationFactory.getConfiguration(slf4jLoggerFactory);

if (cliRequest.verbose) {
level = ch.qos.logback.classic.Level.DEBUG;
cliRequest.request.setLoggingLevel(MavenExecutionRequest.LOGGING_LEVEL_DEBUG);
slf4jConfiguration.setRootLoggerLevel(Slf4jConfiguration.Level.DEBUG);
} else if (cliRequest.quiet) {
level = ch.qos.logback.classic.Level.WARN;
} else {
level = ch.qos.logback.classic.Level.INFO;
cliRequest.request.setLoggingLevel(MavenExecutionRequest.LOGGING_LEVEL_ERROR);
slf4jConfiguration.setRootLoggerLevel(Slf4jConfiguration.Level.ERROR);
}
((ch.qos.logback.classic.Logger) slf4jLoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME)).setLevel(level);
// else fall back to default log level specified in conf
// see https://issues.apache.org/jira/browse/MNG-2570

// LOG COLOR
String styleColor = cliRequest.getUserProperties().getProperty(STYLE_COLOR_PROPERTY, "auto");
Expand All @@ -438,11 +444,6 @@ void logging(CliRequest cliRequest) {
}
}

// Workaround for https://github.com/apache/maven-mvnd/issues/39
final ch.qos.logback.classic.Logger mvndLogger =
(ch.qos.logback.classic.Logger) slf4jLoggerFactory.getLogger("org.mvndaemon.mvnd");
mvndLogger.setLevel(ch.qos.logback.classic.Level.toLevel(System.getProperty("mvnd.log.level"), null));

// LOG STREAMS
if (commandLine.hasOption(CLIManager.LOG_FILE)) {
File logFile = new File(commandLine.getOptionValue(CLIManager.LOG_FILE));
Expand All @@ -462,15 +463,15 @@ void logging(CliRequest cliRequest) {
.asOptional()
.map(Boolean::parseBoolean)
.orElse(Boolean.FALSE)) {
ch.qos.logback.classic.Logger stdout =
(ch.qos.logback.classic.Logger) slf4jLoggerFactory.getLogger("stdout");
ch.qos.logback.classic.Logger stderr =
(ch.qos.logback.classic.Logger) slf4jLoggerFactory.getLogger("stderr");
stdout.setLevel(ch.qos.logback.classic.Level.INFO);
stderr.setLevel(ch.qos.logback.classic.Level.INFO);
MvndSimpleLogger stdout = (MvndSimpleLogger) slf4jLoggerFactory.getLogger("stdout");
MvndSimpleLogger stderr = (MvndSimpleLogger) slf4jLoggerFactory.getLogger("stderr");
stdout.setLogLevel(LocationAwareLogger.INFO_INT);
stderr.setLogLevel(LocationAwareLogger.INFO_INT);
System.setOut(new LoggingOutputStream(s -> stdout.info("[stdout] " + s)).printStream());
System.setErr(new LoggingOutputStream(s -> stderr.warn("[stderr] " + s)).printStream());
}

slf4jConfiguration.activate();
}

private void version(CliRequest cliRequest) throws ExitException {
Expand Down
4 changes: 0 additions & 4 deletions daemon/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,6 @@
</dependency>

<!-- Logging -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId>
Expand Down

This file was deleted.

8 changes: 1 addition & 7 deletions dist-m39/src/main/provisio/maven-distro.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,14 @@
<artifactSet to="/mvn">
<artifact id="org.apache.maven:apache-maven:tar.gz:bin:${maven3.version}">
<unpack useRoot="false"
excludes="conf/logging/*,lib/maven-slf4j-provider*" />
excludes="lib/maven-slf4j-*" />
</artifact>
</artifactSet>

<artifactSet to="/mvn/lib">
<artifact id="org.apache.maven.daemon:mvnd-logging:${project.version}">
<exclusion id="*:*"/>
</artifact>
<artifact id="ch.qos.logback:logback-classic">
<exclusion id="*:*"/>
</artifact>
<artifact id="ch.qos.logback:logback-core">
<exclusion id="*:*"/>
</artifact>
<artifact id="org.slf4j:jul-to-slf4j">
<exclusion id="*:*"/>
</artifact>
Expand Down
8 changes: 1 addition & 7 deletions dist-m40/src/main/provisio/maven-distro.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,14 @@
<artifactSet to="/mvn">
<artifact id="org.apache.maven:apache-maven:tar.gz:bin:${maven4.version}">
<unpack useRoot="false"
excludes="conf/logging/*,lib/maven-slf4j-provider*,lib/plexus-utils-3.*" />
excludes="lib/maven-slf4j-*,lib/plexus-utils-3.*" />
</artifact>
</artifactSet>

<artifactSet to="/mvn/lib">
<artifact id="org.apache.maven.daemon:mvnd-logging:${project.version}">
<exclusion id="*:*"/>
</artifact>
<artifact id="ch.qos.logback:logback-classic">
<exclusion id="*:*"/>
</artifact>
<artifact id="ch.qos.logback:logback-core">
<exclusion id="*:*"/>
</artifact>
<artifact id="org.slf4j:jul-to-slf4j">
<exclusion id="*:*"/>
</artifact>
Expand Down
2 changes: 1 addition & 1 deletion dist/src/main/distro/bin/mvnd-bash-completion.bash
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ _mvnd()

local mvnd_opts="-1"
local mvnd_long_opts="--color|--completion|--purge|--raw-streams|--serial|--status|--stop"
local mvnd_properties="-Djava.home|-Djdk.java.options|-Dmaven.multiModuleProjectDirectory|-Dmaven.repo.local|-Dmaven.settings|-Dmvnd.buildTime|-Dmvnd.builder|-Dmvnd.cancelConnectTimeout|-Dmvnd.connectTimeout|-Dmvnd.daemonStorage|-Dmvnd.debug|-Dmvnd.debug.address|-Dmvnd.duplicateDaemonGracePeriod|-Dmvnd.enableAssertions|-Dmvnd.expirationCheckDelay|-Dmvnd.home|-Dmvnd.idleTimeout|-Dmvnd.jvmArgs|-Dmvnd.keepAlive|-Dmvnd.logPurgePeriod|-Dmvnd.logback|-Dmvnd.maxHeapSize|-Dmvnd.maxLostKeepAlive|-Dmvnd.minHeapSize|-Dmvnd.minThreads|-Dmvnd.noBuffering|-Dmvnd.noDaemon|-Dmvnd.noModelCache|-Dmvnd.pluginRealmEvictPattern|-Dmvnd.propertiesPath|-Dmvnd.rawStreams|-Dmvnd.registry|-Dmvnd.rollingWindowSize|-Dmvnd.serial|-Dmvnd.socketConnectTimeout|-Dmvnd.socketFamily|-Dmvnd.threadStackSize|-Dmvnd.threads|-Dstyle.color|-Duser.dir|-Duser.home"
local mvnd_properties="-Djava.home|-Djdk.java.options|-Dmaven.multiModuleProjectDirectory|-Dmaven.repo.local|-Dmaven.settings|-Dmvnd.buildTime|-Dmvnd.builder|-Dmvnd.cancelConnectTimeout|-Dmvnd.connectTimeout|-Dmvnd.daemonStorage|-Dmvnd.debug|-Dmvnd.debug.address|-Dmvnd.duplicateDaemonGracePeriod|-Dmvnd.enableAssertions|-Dmvnd.expirationCheckDelay|-Dmvnd.home|-Dmvnd.idleTimeout|-Dmvnd.jvmArgs|-Dmvnd.keepAlive|-Dmvnd.logPurgePeriod|-Dmvnd.maxHeapSize|-Dmvnd.maxLostKeepAlive|-Dmvnd.minHeapSize|-Dmvnd.minThreads|-Dmvnd.noBuffering|-Dmvnd.noDaemon|-Dmvnd.noModelCache|-Dmvnd.pluginRealmEvictPattern|-Dmvnd.propertiesPath|-Dmvnd.rawStreams|-Dmvnd.registry|-Dmvnd.rollingWindowSize|-Dmvnd.serial|-Dmvnd.socketConnectTimeout|-Dmvnd.socketFamily|-Dmvnd.threadStackSize|-Dmvnd.threads|-Dstyle.color|-Duser.dir|-Duser.home"
local opts="-am|-amd|-B|-C|-c|-cpu|-D|-e|-emp|-ep|-f|-fae|-ff|-fn|-gs|-h|-l|-N|-npr|-npu|-nsu|-o|-P|-pl|-q|-rf|-s|-T|-t|-U|-up|-V|-v|-X|${mvnd_opts}"
local long_opts="--also-make|--also-make-dependents|--batch-mode|--strict-checksums|--lax-checksums|--check-plugin-updates|--define|--errors|--encrypt-master-password|--encrypt-password|--file|--fail-at-end|--fail-fast|--fail-never|--global-settings|--help|--log-file|--non-recursive|--no-plugin-registry|--no-plugin-updates|--no-snapshot-updates|--offline|--activate-profiles|--projects|--quiet|--resume-from|--settings|--threads|--toolchains|--update-snapshots|--update-plugins|--show-version|--version|--debug|${mvnd_long_opts}"

Expand Down
Loading

0 comments on commit f8adf1b

Please sign in to comment.