Maven Logging Options – Maven日志选项

最后修改: 2020年 7月 13日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

In this quick tutorial, we’re going to see how to configure logging options in Maven.

在这个快速教程中,我们将了解如何在Maven中配置日志选项。

2. Command Line

2.命令行

By default, Maven only logs the info, warning, and error logs. Also, for errors, it doesn’t show the full stacktrace of that log. In order to see the full stacktrace, we can use the -e or –errors option:

默认情况下,Maven只记录信息、警告、错误日志。另外,对于错误,它不会显示该日志的完整堆栈跟踪。为了查看完整的堆栈跟踪,我们可以使用-e-errors选项

$ mvn -e clean compile
// truncated
cannot find symbol
  symbol:   variable name
  location: class Compiled

    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:213)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:154)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:146)
    ...

As shown above, now Maven shows the full error report. It’s also possible to see debug level logs via the -X or –debug option:

如上图所示,现在Maven显示了完整的错误报告。还可以通过-X-debug选项查看调试级日志

$ mvn -X clean compile
// truncated
OS name: "mac os x", version: "10.15.5", arch: "x86_64", family: "mac"
[DEBUG] Created new class realm maven.api
[DEBUG] Importing foreign packages into class realm maven.api
...

When the debug is on, the output is very verbose. To combat this, we can ask Maven to don’t log anything expect errors via -q or –quiet option:

当调试开启时,输出会非常冗长。为了解决这个问题,我们可以通过-q-quiet选项要求Maven不记录任何错误。

$ mvn --quiet clean compile

Moreover, we can redirect the Maven log to a file using the -l or –log-file option:

此外,我们可以使用-l-log-file选项将Maven日志重定向到一个文件。

$ mvn --log-file ./mvn.log clean compile

Instead of standard output, all the logs can be found in the mvn.log file in the current directory. As an alternative, it’s also possbile to use OS features to redirect the Maven output to a file:

所有的日志都可以在当前目录下的mvn.log文件中找到,而不是标准输出。作为一种选择,也可以使用操作系统的功能将Maven的输出重定向到一个文件。

$ mvn clean compile > ./mvn.log

3. SLF4J Settings

3.SLF4J设置

Currently, Maven is using the SLF4J API for logging combined with the SLF4J Simple implementation. Therefore, to configure logging with SLF4J Simple, we can edit the properties in the ${maven.home}/conf/logging/simplelogger.properties file.

目前,Maven使用SLF4J API与SLF4J Simple实现相结合进行记录。因此,要用SLF4J Simple配置日志,我们可以编辑${maven.home}/conf/logging/simplelogger.properties文件中的属性。

For instance, if we add the following lines in this file:

例如,如果我们在这个文件中加入以下几行。

org.slf4j.simpleLogger.showDateTime=true
org.slf4j.simpleLogger.dateTimeFormat=yyyy-MM-dd HH:mm:ss

then Maven will show the date-time info in the above format.

那么Maven将以上述格式显示日期时间信息。

Let’s try another build:

让我们尝试另一种构建。

$ mvn clean compile
2020-07-08 12:08:07 [INFO] Scanning for projects...

We can also pass these properties via -D arguments from the command line:

我们也可以通过命令行的-D参数传递这些属性。

$ mvn compile -Dorg.slf4j.simpleLogger.showThreadName=true
[main] [INFO] Scanning for projects...

Here we’re displaying the thread name in addition to other information.

这里我们除了显示线程名称外,还显示其他信息。

In addition to the mentioned properties, we can also configure the simple logger with other properties, too:

除了提到的属性外,我们还可以用其他属性来配置简单的记录器。

  • org.slf4j.simpleLogger.logFile uses a log file for logging instead of standard output
  • org.slf4j.simpleLogger.defaultLogLevel represents the default log level. It can be one of trace, debug, info, warn, error, or off – the default value is info
  • org.slf4j.simpleLogger.showLogName shows the SLF4j logger name if it’s true
  • org.slf4j.simpleLogger.showShortLogName truncates the long logger names if it’s true

4. Conclusion

4.总结

In this short tutorial, we saw how to configure different logging and verbosity options in Maven.

在这个简短的教程中,我们看到了如何在Maven中配置不同的日志和口令性选项。