Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE

1. Overview

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

2. Command Line

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:

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

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

$ mvn --quiet clean compile

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

$ 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 clean compile > ./mvn.log

3. SLF4J Settings

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.

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.

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:

$ 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

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

Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – Maven (eBook) (cat=Maven)
Comments are closed on this article!