ChatGPT解决这个技术问题 Extra ChatGPT

How to change maven logging level to display only warning and errors?

I want to prevent maven from displaying INFO messages, I want to see only WARNINGS and ERRORS (if any).

How can I achieve this, preferably by changing the command line that calls maven?

Relevant topic: stackoverflow.com/questions/71069/… . -B, --batch-mode or -q will make mvn less verbose.

C
Community

Answering your question

I made a small investigation because I am also interested in the solution.

Maven command line verbosity options

According to http://books.sonatype.com/mvnref-book/reference/running-sect-options.html#running-sect-verbose-option

-e for error

-X for debug

-q for only error

Maven logging config file

Currently maven 3.1.x uses SLF4J to log to the System.out . You can modify the logging settings at the file:

${MAVEN_HOME}/conf/logging/simplelogger.properties

According to the page : http://maven.apache.org/maven-logging.html

Command line setup

I think you should be able to setup the default Log level of the simple logger via a command line parameter, like this:

$ mvn clean package -Dorg.slf4j.simpleLogger.defaultLogLevel=debug

But I could not get it to work. I guess the only problem with this is, maven picks up the default level from the config file on the classpath. I also tried a couple of other settings via System.properties, but all of them were unsuccessful.

Appendix

You can find the source of slf4j on github here : slf4j github

The source of the simplelogger here : slf4j/jcl-over-slf4j/src/main/java/org/apache/commons/logging/impl/SimpleLog.java

The plexus loader loads the simplelogger.properties.


did you get the simplelogger.properties change working? When I change the org.slf4j.simpleLogger.warnLevelString setting mvn compile updates appropriately, but it appears to be ignoring the org.slf4j.simpleLogger.defaultLogLevel setting. It also logs at the info level, even when i set that to warn or error.
@Alden, I am getting the same behaviour. it is simply ignored.
For me it turned out that adding "-Dorg.slf4j.simpleLogger.defaultLogLevel=info" to the build command does not have any effect. If I define this before the build in MAVEN_OPTS like this: export "MAVEN_OPTS=$MAVEN_OPTS -Dorg.slf4j.simpleLogger.defaultLogLevel=debug", then it works perfectly without changing the ${MAVEN_HOME}/conf/logging/simplelogger.properties file.
Alden, Gábor Lipták: and for me none of the two methods work. :( I am using Maven 3.2.5.
if windows, in bat file ,use set MAVEN_OPTS=%MAVEN_OPTS% -Dorg.slf4j.simpleLogger.defaultLogLevel=warn
T
Tom

Edit: this answer was from 2013, there are probably better ways to do this now, please consider the other answers.

Linux:

mvn validate clean install | egrep -v "(^\[INFO\])"

or

mvn validate clean install | egrep -v "(^\[INFO\]|^\[DEBUG\])"

Windows:

mvn validate clean install | findstr /V /R "^\[INFO\] ^\[DEBUG\]"

but if the point is to reduce the redundant IO during mvn... then it's not really does the trick
How can we achieve the same on Windows machine as egrep doesn't work on it?
something like mvn validate clean install | findstr /V /B "\[INFO\]" | findstr /V /B "\[WARN\]"
Looks like findstr supports regex and other options that might make it more succinct - ss64.com/nt/findstr.html
mvn validate clean install | findstr /V /R "^\[INFO\] ^\[WARN\]"
J
Josmar

You can achieve this with MAVEN_OPTS, for example
MAVEN_OPTS=-Dorg.slf4j.simpleLogger.defaultLogLevel=warn mvn clean

Rather than putting the system property directly on the command line. (At least for maven 3.3.1.)

Consider using ~/.mavenrc for setting MAVEN_OPTS if you would like logging changed for your login across all maven invocations.


This seems to work as I could disable warning and info message by setting the level to error, but unfortunately it also turned off the regular output. I was actually trying to disable some warnings while running help:evaluate target to print the value of project.version and this output (though doesn't seem to come through slf4j) also got turned off.
The only answer that works without permanently messing with Maven installation. Another reason Maven is a dinosaur among the build system - it just doesn't have support for common sense requirements.
G
Greg Dubicki

If you are using Logback, just put this logback-test.xml file into src/test/resources directory:

<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
        <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
    </encoder>
</appender>
<root level="INFO">
    <appender-ref ref="STDOUT" />
</root>
</configuration>

M
MD AFSAR ALI

you can achieve this by using below in the commandline itself

-e for error -X for debug -q for only error

e.g :

mvn test -X -DsomeProperties='SomeValue' [For Debug level Logs] mvn test -e -DsomeProperties='SomeValue' [For Error level Logs] mvn test -q -DsomeProperties='SomeValue' [For Only Error Logs]


Would have been good to have more info, like does this enable or disable the corresponding log
b
btiernay

The simplest way is to upgrade to Maven 3.3.1 or higher to take advantage of the ${maven.projectBasedir}/.mvn/jvm.config support.

Then you can use any options from Maven's SL4FJ's SimpleLogger support to configure all loggers or particular loggers. For example, here is a how to make all warning at warn level, except for a the PMD which is configured to log at error:

cat .mvn/jvm.config
-Dorg.slf4j.simpleLogger.defaultLogLevel=warn -Dorg.slf4j.simpleLogger.log.net.sourceforge.pmd=error

See here for more details on logging with Maven.


D
Dmitry Sidorenko

Unfortunately, even with maven 3 the only way to do that is to patch source code.

Here is short instruction how to do that.

Clone or fork Maven 3 repo: "git clone https://github.com/apache/maven-3.git"

Edit org.apache.maven.cli.MavenCli#logging, and change

cliRequest.request.setLoggingLevel( MavenExecutionRequest.LOGGING_LEVEL_INFO );

to

cliRequest.request.setLoggingLevel( MavenExecutionRequest.LOGGING_LEVEL_WARN );

In current snapshot version it's at line 270

Then just run "mvn install", your new maven distro will be located in "apache-maven\target\" folder

See this diff for the reference: https://github.com/ushkinaz/maven-3/commit/cc079aa75ca8c82658c7ff53f18c6caaa32d2131


This answer is OBSOLETE as of now - a) repo URL has changed, b) there is no INFO level statement in this code, c) diff URL doesn't work.
Doesn't matter where the code uses INFO - when developing plugins, your mojo should rely on the maven logger with getLog() - if you can live with the configurability.
G
Gangnus

Go to simplelogger.properties in ${MAVEN_HOME}/conf/logging/ and set the following properties:

org.slf4j.simpleLogger.defaultLogLevel=warn
org.slf4j.simpleLogger.log.Sisu=warn
org.slf4j.simpleLogger.warnLevelString=warn

And beware: warn, not warning


H
Hakan Özler

I have noticed when using the 2.20.1 version of the maven sunfire plugin, all warnings are written down to a dumpstream file. e.g. /myproject/target/surefire-reports/2017-11-11T23-02-19_850.dumpstream


S
Sumith08

Changing the info to error in simplelogging.properties file will help in achieving your requirement.

Just change the value of the below line

org.slf4j.simpleLogger.defaultLogLevel=info 

to

org.slf4j.simpleLogger.defaultLogLevel=error

Downvoted as this is a change of the source, whereas I believe the poster wanted a command line method, I certainly do!
c
confirmator

In Debian this worked for me like a charm to change the log level for Maven 3.6 at runtime without having to change the simplelogger.properties file:

MAVEN_OPTS='-Dorg.slf4j.simpleLogger.defaultLogLevel=error' mvn test