如何更改 Maven 日志记录级别以仅显示警告和错误?

发布于 2024-10-14 10:09:15 字数 82 浏览 2 评论 0原文

我想阻止 Maven 显示信息消息,我只想看到警告和错误(如果有)。

我怎样才能实现这一点,最好是通过更改调用 Maven 的命令行?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

坏尐絯℡ 2024-10-21 10:09:16

我在使用 2.20.1 版本的 maven sunfire 插件 时注意到,所有警告都会写入转储流文件中。例如 /myproject/target/surefire-reports/2017-11-11T23-02-19_850.dumpstream

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

半衾梦 2024-10-21 10:09:15

回答你的问题

我做了一个小调查,因为我也对解决方案感兴趣。

Maven 命令行详细选项

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

  • -e 用于错误
  • -X 用于调试
  • -q 仅用于错误

Maven 日志记录配置文件

目前,maven 3.1.x 使用 SLF4J 登录到 System.out 。
您可以在文件中修改日志记录设置:

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

根据页面:http://maven.apache。 org/maven-logging.html

命令行设置

我认为您应该能够通过命令行参数设置简单记录器的默认日志级别,如下所示:

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

但是我无法让它工作。我想唯一的问题是,maven 从类路径上的配置文件中获取默认级别。我还通过 System.properties 尝试了其他一些设置,但全部都不成功。

附录

您可以在 github 上找到 slf4j 的源代码: slf4j github

simplelogger 的源代码这里:slf4j/jcl-over-slf4j/src/main/java/org/apache/commons/logging/impl/SimpleLog.java

plexus 加载器加载 simplelogger.properties< /代码>。

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.

送你一个梦 2024-10-21 10:09:15

编辑:此答案来自2013,现在可能有更好的方法可以做到这一点,请考虑其他答案。

Linux:

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

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

Windows:

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

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\]"
凉风有信 2024-10-21 10:09:15

例如,您可以使用 MAVEN_OPTS 来实现此目的
MAVEN_OPTS=-Dorg.slf4j.simpleLogger.defaultLogLevel=warn mvn clean

而不是直接将系统属性放在命令行上。 (至少对于 maven 3.3.1。)

如果您希望在所有 maven 调用中更改登录日志记录,请考虑使用 ~/.mavenrc 来设置 MAVEN_OPTS

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.

自在安然 2024-10-21 10:09:15

如果您使用 Logback,只需将此 logback-test.xml 文件放入 src/test/resources 目录中:

<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>

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>
手心的海 2024-10-21 10:09:15

您可以通过在命令行本身中使用以下内容来实现此目的

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

例如:

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]

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]

寒尘 2024-10-21 10:09:15

最简单的方法是升级到 Maven 3.3.1 或更高版本以利用 ${maven.projectBasedir}/.mvn/jvm.config 支持。

然后,您可以使用 Maven 的 SL4FJ 的 SimpleLogger 支持中的任何选项来配置所有记录器或特定记录器。例如,以下是如何将所有警告设置为 warn 级别,但配置为在 error 处记录的 PMD 除外:

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

请参阅 此处 了解有关使用 Maven 进行日志记录的更多详细信息。

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.

2024-10-21 10:09:15

在 Debian 中,这对我来说就像一个魅力,可以在运行时更改 Maven 3.6 的日志级别,而无需更改 simplelogger.properties 文件:

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

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
十年九夏 2024-10-21 10:09:15

不幸的是,即使使用 Maven 3,唯一的方法就是修补源代码。

这是如何执行此操作的简短说明。

克隆或分叉 Maven 3 存储库:“git clone https://github.com/apache/maven- 3.git

编辑 org.apache.maven.cli.MavenCli#logging,并更改

cliRequest.request.setLoggingLevel( MavenExecutionRequest.LOGGING_LEVEL_INFO );

cliRequest.request.setLoggingLevel( MavenExecutionRequest.LOGGING_LEVEL_WARN );

In current snapshot version it's at line 270

然后只需运行“mvn install”,您的新 maven 发行版将位于“apache -maven\target\" 文件夹

请参阅此差异以获取参考:https://github。 com/ushkinaz/maven-3/commit/cc079aa75ca8c82658c7ff53f18c6caaa32d2131

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

软糯酥胸 2024-10-21 10:09:15

simplelogging.properties 文件中的 info 更改为 error 将有助于实现您的要求。

只需将下面一行的值更改

org.slf4j.simpleLogger.defaultLogLevel=info 

org.slf4j.simpleLogger.defaultLogLevel=error

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
一片旧的回忆 2024-10-21 10:09:15

转到 ${MAVEN_HOME}/conf/logging/ 中的 simplelogger.properties 并设置以下属性:

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

并注意:warn,而不是 警告

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文