Redirecting Maven transfer messages to a file

One thing that is often bothering me about Maven is the extensive logging of download messages. Usually I am not interested in these messages unless something is really wrong, and then it is important to know, which URLs are accessed for download.

Before Maven 3.1 there were only poor chances to influence this behavior through the CLI. There were the options “-q” (quiet) and “-B” (batch mode), which influence the TransferListener implementation used by the Maven main class MavenCLI.

(from MavenCLI 3.0.4):

if ( quiet )
{
    transferListener = new QuietMavenTransferListener();
}
else if ( request.isInteractiveMode() )
{
    transferListener = new ConsoleMavenTransferListener( System.out );
}
else
{
    transferListener = new BatchModeMavenTransferListener( System.out );
}

With Maven 3.1 it was decided to use SLF4J as logging API. When using batch mode (-B), Maven will use Slf4jMavenTransferListener for logging, which is determined by method getBatchTransferListener().

(from MavenCLI 3.2.5):

if ( quiet )
{
    transferListener = new QuietMavenTransferListener();
}
else if ( request.isInteractiveMode() && !cliRequest.commandLine.hasOption( CLIManager.LOG_FILE ) )
{
    //
    // If we're logging to a file then we don't want the console transfer listener as it will spew
    // download progress all over the place
    //
    transferListener = getConsoleTransferListener();
}
else
{
    transferListener = getBatchTransferListener();
}


protected TransferListener getBatchTransferListener()
{
    return new Slf4jMavenTransferListener();
}

By default, the SLF4J SimpleLogger is used, which can be configured by the file

<MVN_HOME>/conf/logging/simplelogger.properties

This allows already some decent influence on the message layout, threshold etc. Transfer messages can be suppressed by adding this property to the file:

org.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn

All info level transfer messages will be suppressed, but this would also cover upload messages when deploying artifacts.

For a more advanced set up, the underyling logging framework can be replaced, e.g. with Log4J2. To do so, follow these simple steps:

1) delete lib/slf4j-simple-<version>.jar
2) add to lib/ folder:
log4j-slf4j-impl-2.2.jar
log4j-api-2.2.jar
log4j-core-2.2.jar
3) add to conf/logging folder file log4j2.xml


<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<!-- layout see http://logging.apache.org/log4j/2.0/manual/layouts.html -->
<PatternLayout pattern="[%level] %msg%n" />
</Console>
<File name="TransferLog" fileName="mvn_transfer.log" immediateFlush="false" append="false">
<PatternLayout pattern="%msg%n"/>
</File>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="Console" />
</Root>
<Logger name="org.apache.maven.cli.transfer.Slf4jMavenTransferListener" level="info" additivity="false">
<AppenderRef ref="TransferLog" />
</Logger>
</Loggers>
</Configuration>

As a result, all transfer messages will be redirected to file “mvn_transfer.log”, while all other messages go to the console.

Advertisements