Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve logging with latest Maven changes #1164

Merged
merged 1 commit into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -323,11 +323,6 @@ public ExecutionResult execute(ClientOutput output, List<String> argv) {
return DefaultResult.success(argv);
}

// Raw streams
if (Environment.MVND_RAW_STREAMS.removeCommandLineOption(args) != null) {
args.add("-D" + Environment.MVND_RAW_STREAMS.getProperty());
}

Optional<String> threads = Optional.ofNullable(Environment.MVND_THREADS.removeCommandLineOption(args));
Environment.MVND_THREADS.addCommandLineOption(args, threads.orElseGet(parameters::threads));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;

import org.apache.maven.slf4j.MavenSimpleLogger;
import org.mvndaemon.mvnd.common.DaemonCompatibilitySpec;
import org.mvndaemon.mvnd.common.DaemonCompatibilitySpec.Result;
import org.mvndaemon.mvnd.common.DaemonConnection;
Expand All @@ -55,7 +56,6 @@
import org.mvndaemon.mvnd.common.Os;
import org.mvndaemon.mvnd.common.SocketFamily;
import org.mvndaemon.mvnd.common.logging.ClientOutput;
import org.mvndaemon.mvnd.logging.slf4j.MvndBaseLogger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -434,7 +434,7 @@ 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" + MvndBaseLogger.LOG_FILE_KEY + "="
args.add("-D" + MavenSimpleLogger.LOG_FILE_KEY + "="
+ parameters.daemonStorage().resolve("daemon-" + daemonId + ".log"));

Environment.MVND_JAVA_HOME.addSystemProperty(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,10 +296,6 @@ public enum Environment {
* start with <code>'glob:**&#47;'</code> to support any location of the local repository.
*/
MVND_PLUGIN_REALM_EVICT_PATTERN("mvnd.pluginRealmEvictPattern", null, "", OptionType.STRING, Flags.OPTIONAL),
/**
* Whether or not decorate output and error streams
**/
MVND_RAW_STREAMS("mvnd.rawStreams", null, Boolean.FALSE, OptionType.VOID, Flags.OPTIONAL, "mvnd:--raw-streams"),
/**
* Overall timeout to connect to a daemon.
*/
Expand Down
4 changes: 2 additions & 2 deletions common/src/main/java/org/mvndaemon/mvnd/common/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -986,8 +986,8 @@ public String toString() {
+ repositoryUrl + '\'' + ", resourceName='"
+ resourceName + '\'' + ", contentLength="
+ contentLength + ", transferredBytes="
+ transferredBytes + ", exception='"
+ exception + '\'' + '}';
+ transferredBytes
+ (exception != null ? ", exception='" + exception + '\'' : "") + '}';
}

private String mnemonic() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.Optional;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Option;
Expand All @@ -31,7 +30,6 @@
import org.apache.maven.cling.invoker.mvn.CommonsCliMavenOptions;
import org.codehaus.plexus.interpolation.BasicInterpolator;
import org.codehaus.plexus.interpolation.InterpolationException;
import org.mvndaemon.mvnd.common.Environment;

import static org.apache.maven.cling.invoker.Utils.createInterpolator;

Expand All @@ -49,15 +47,6 @@ public org.apache.commons.cli.Options getOptions() {
return this.cliManager.getOptions();
}

@Override
public Optional<Boolean> rawStreams() {
if (commandLine.hasOption(CLIManager.RAW_STREAMS)
|| Environment.MVND_RAW_STREAMS.asOptional().isPresent()) {
return Optional.of(Boolean.TRUE);
}
return Optional.empty();
}

private static CommonsCliDaemonMavenOptions interpolate(
CommonsCliDaemonMavenOptions options, Collection<Map<String, String>> properties) {
try {
Expand Down
11 changes: 8 additions & 3 deletions daemon/src/main/java/org/apache/maven/cli/DaemonCli.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,25 @@
*/
package org.apache.maven.cli;

import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;

import org.mvndaemon.mvnd.logging.smart.BuildEventListener;
import org.apache.maven.logging.BuildEventListener;

/**
* Simple interface to bridge maven 3.9.x and 4.0.x CLI
*/
public interface DaemonCli extends AutoCloseable {
public interface DaemonCli {
int main(
List<String> args,
String workingDir,
String projectDir,
Map<String, String> env,
BuildEventListener buildEventListener)
BuildEventListener buildEventListener,
InputStream in,
OutputStream out,
OutputStream err)
throws Exception;
}
64 changes: 26 additions & 38 deletions daemon/src/main/java/org/apache/maven/cli/DaemonMavenCling.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@
*/
package org.apache.maven.cli;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.nio.charset.StandardCharsets;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Paths;
import java.util.List;
import java.util.Map;
Expand All @@ -29,56 +28,45 @@
import org.apache.maven.api.cli.ParserRequest;
import org.apache.maven.cling.invoker.ProtoLogger;
import org.apache.maven.cling.invoker.ProtoLookup;
import org.apache.maven.jline.MessageUtils;
import org.apache.maven.logging.BuildEventListener;
import org.codehaus.plexus.classworlds.ClassWorld;
import org.codehaus.plexus.classworlds.realm.ClassRealm;
import org.jline.terminal.Terminal;
import org.jline.terminal.impl.ExternalTerminal;
import org.mvndaemon.mvnd.cli.EnvHelper;
import org.mvndaemon.mvnd.logging.smart.BuildEventListener;

public class DaemonMavenCling implements DaemonCli {
private final DaemonMavenParser parser;
private final DaemonMavenInvoker invoker;

public DaemonMavenCling() {
this.parser = new DaemonMavenParser();
this.invoker = new DaemonMavenInvoker(ProtoLookup.builder()
.addMapping(
ClassWorld.class, ((ClassRealm) Thread.currentThread().getContextClassLoader()).getWorld())
.build());
}

@Override
public void close() throws Exception {
invoker.close();
}

@Override
public int main(
List<String> args,
String workingDir,
String projectDir,
Map<String, String> env,
BuildEventListener buildEventListener)
BuildEventListener buildEventListener,
InputStream in,
OutputStream out,
OutputStream err)
throws Exception {
Terminal terminal = new ExternalTerminal(
"Maven",
"ansi",
new ByteArrayInputStream(new byte[0]),
new ByteArrayOutputStream(),
StandardCharsets.UTF_8);
MessageUtils.systemInstall(terminal);
EnvHelper.environment(workingDir, env);
System.setProperty("maven.multiModuleProjectDirectory", projectDir);
return invoker.invoke(parser.parse(ParserRequest.builder(
"mvnd", "Maven Daemon", args, new ProtoLogger(), new DaemonMessageBuilderFactory())
.cwd(Paths.get(workingDir))
.lookup(ProtoLookup.builder()
.addMapping(Environment.class, () -> env)
.addMapping(BuildEventListener.class, buildEventListener)
.build())
.build()));

try (DaemonMavenInvoker invoker = new DaemonMavenInvoker(ProtoLookup.builder()
.addMapping(
ClassWorld.class, ((ClassRealm) Thread.currentThread().getContextClassLoader()).getWorld())
.addMapping(BuildEventListener.class, buildEventListener)
.build())) {
DaemonMavenParser parser = new DaemonMavenParser();
return invoker.invoke(parser.parse(ParserRequest.builder(
"mvnd", "Maven Daemon", args, new ProtoLogger(), new DaemonMessageBuilderFactory())
.cwd(Paths.get(workingDir))
.in(in)
.out(out)
.err(err)
.lookup(ProtoLookup.builder()
.addMapping(Environment.class, () -> env)
.addMapping(BuildEventListener.class, buildEventListener)
.build())
.build()));
}
}

/**
Expand Down
89 changes: 48 additions & 41 deletions daemon/src/main/java/org/apache/maven/cli/DaemonMavenInvoker.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,24 @@
*/
package org.apache.maven.cli;

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.nio.charset.StandardCharsets;

import org.apache.maven.api.cli.Options;
import org.apache.maven.api.cli.mvn.MavenInvokerRequest;
import org.apache.maven.api.cli.mvn.MavenOptions;
import org.apache.maven.cli.event.ExecutionEventLogger;
import org.apache.maven.api.services.MavenException;
import org.apache.maven.cling.invoker.ContainerCapsuleFactory;
import org.apache.maven.cling.invoker.ProtoLookup;
import org.apache.maven.cling.invoker.mvn.resident.DefaultResidentMavenInvoker;
import org.apache.maven.execution.ExecutionListener;
import org.apache.maven.jline.MessageUtils;
import org.eclipse.aether.transfer.TransferListener;
import org.apache.maven.logging.BuildEventListener;
import org.apache.maven.logging.LoggingOutputStream;
import org.jline.terminal.Terminal;
import org.jline.terminal.impl.ExternalTerminal;
import org.mvndaemon.mvnd.common.Environment;
import org.mvndaemon.mvnd.logging.slf4j.MvndSimpleLogger;
import org.mvndaemon.mvnd.logging.smart.BuildEventListener;
import org.mvndaemon.mvnd.logging.smart.LoggingExecutionListener;
import org.mvndaemon.mvnd.logging.smart.LoggingOutputStream;
import org.mvndaemon.mvnd.transfer.DaemonMavenTransferListener;
import org.slf4j.spi.LocationAwareLogger;

public class DaemonMavenInvoker extends DefaultResidentMavenInvoker {
public DaemonMavenInvoker(ProtoLookup protoLookup) {
Expand All @@ -44,25 +45,54 @@ public DaemonMavenInvoker(ProtoLookup protoLookup) {
@Override
protected void configureLogging(LocalContext context) throws Exception {
super.configureLogging(context);
}

@Override
protected Terminal createTerminal(LocalContext context) {
try {
Terminal terminal = new ExternalTerminal(
"Maven",
"ansi",
context.invokerRequest.in().get(),
context.invokerRequest.out().get(),
StandardCharsets.UTF_8);
doConfigureWithTerminal(context, terminal);
// If raw-streams options has been set, we need to decorate to push back to the client
if (context.invokerRequest.options().rawStreams().orElse(false)) {
BuildEventListener bel = determineBuildEventListener(context);
OutputStream out = context.invokerRequest.out().orElse(null);
System.setOut(out != null ? printStream(out) : new LoggingOutputStream(bel::log).printStream());
OutputStream err = context.invokerRequest.err().orElse(null);
System.setErr(err != null ? printStream(err) : new LoggingOutputStream(bel::log).printStream());
}
return terminal;
} catch (IOException e) {
throw new MavenException("Error creating terminal", e);
}
}

DaemonMavenOptions options = (DaemonMavenOptions) context.invokerRequest.options();
if (options.logFile().isEmpty() && !options.rawStreams().orElse(false)) {
MvndSimpleLogger stdout = (MvndSimpleLogger) context.loggerFactory.getLogger("stdout");
MvndSimpleLogger stderr = (MvndSimpleLogger) context.loggerFactory.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());
private PrintStream printStream(OutputStream outputStream) {
if (outputStream instanceof LoggingOutputStream los) {
return los.printStream();
} else if (outputStream instanceof PrintStream ps) {
return ps;
} else {
return new PrintStream(outputStream);
}
}

@Override
protected org.apache.maven.logging.BuildEventListener doDetermineBuildEventListener(LocalContext context) {
return protoLookup.lookup(BuildEventListener.class);
}

@Override
protected void helpOrVersionAndMayExit(LocalContext context) throws Exception {
MavenInvokerRequest<MavenOptions> invokerRequest = context.invokerRequest;
BuildEventListener buildEventListener =
context.invokerRequest.parserRequest().lookup().lookup(BuildEventListener.class);
if (invokerRequest.options().help().isPresent()) {
// TODO: ugly, clenup
// TODO: ugly, cleanup
buildEventListener.log(
MvndHelpFormatter.displayHelp((CommonsCliDaemonMavenOptions) context.invokerRequest.options()));
throw new ExitException(0);
Expand Down Expand Up @@ -95,29 +125,6 @@ protected void preCommands(LocalContext context) throws Exception {
return new DaemonPlexusContainerCapsuleFactory();
}

@Override
protected ExecutionListener determineExecutionListener(LocalContext context) {
if (context.lookup != null) {
LoggingExecutionListener listener = context.lookup.lookup(LoggingExecutionListener.class);
ExecutionEventLogger executionEventLogger =
new ExecutionEventLogger(context.invokerRequest.messageBuilderFactory());
listener.init(
context.eventSpyDispatcher.chainListener(executionEventLogger),
context.invokerRequest.parserRequest().lookup().lookup(BuildEventListener.class));
return listener;
} else {
// this branch happens in "early" step of container capsule to load extensions
return super.determineExecutionListener(context);
}
}

@Override
protected TransferListener determineTransferListener(LocalContext context, boolean noTransferProgress) {
return new DaemonMavenTransferListener(
context.invokerRequest.parserRequest().lookup().lookup(BuildEventListener.class),
super.determineTransferListener(context, noTransferProgress));
}

@Override
protected int doExecute(LocalContext context) throws Exception {
context.logger.info(MessageUtils.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,10 @@

import java.util.Collection;
import java.util.Map;
import java.util.Optional;

import org.apache.maven.api.cli.mvn.MavenOptions;

public interface DaemonMavenOptions extends MavenOptions {
/**
* Should use raw-streams to communicate with daemon.
*/
Optional<Boolean> rawStreams();

@Override
DaemonMavenOptions interpolate(Collection<Map<String, String>> properties);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,11 @@
import org.apache.maven.api.cli.mvn.MavenOptions;
import org.apache.maven.cling.invoker.PlexusContainerCapsuleFactory;
import org.apache.maven.cling.invoker.mvn.resident.DefaultResidentMavenInvoker;
import org.codehaus.plexus.logging.LoggerManager;
import org.mvndaemon.mvnd.common.Environment;
import org.mvndaemon.mvnd.logging.internal.Slf4jLoggerManager;

public class DaemonPlexusContainerCapsuleFactory
extends PlexusContainerCapsuleFactory<
MavenOptions, MavenInvokerRequest<MavenOptions>, DefaultResidentMavenInvoker.LocalContext> {
private final Slf4jLoggerManager slf4jLoggerManager = new Slf4jLoggerManager();

@Override
protected LoggerManager createLoggerManager() {
return slf4jLoggerManager;
}

@Override
protected List<Path> parseExtClasspath(DefaultResidentMavenInvoker.LocalContext context) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import io.takari.maven.builder.smart.DependencyGraph;
import org.apache.maven.execution.ExecutionEvent;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.logging.BuildEventListener;
import org.apache.maven.plugin.MojoExecution;
import org.apache.maven.project.MavenProject;
import org.eclipse.aether.transfer.TransferEvent;
Expand All @@ -37,12 +38,11 @@
import org.mvndaemon.mvnd.common.Message;
import org.mvndaemon.mvnd.common.Message.BuildException;
import org.mvndaemon.mvnd.common.Message.BuildStarted;
import org.mvndaemon.mvnd.logging.smart.BuildEventListener;

/**
* Sends events back to the client.
*/
public class ClientDispatcher extends BuildEventListener {
public class ClientDispatcher implements BuildEventListener {
private final Collection<Message> queue;
private static final Pattern TRAILING_EOLS_PATTERN = Pattern.compile("[\r\n]+$");

Expand Down
Loading
Loading