Log Management in Spring Boot
A tutorial on how to use Spring Boot to log in an output file, and configure where Spring will store your logs.
Join the DZone community and get the full member experience.Join For Free
Logging is for sure not a glamorous subject, but it’s a critical one – at least for DevOps and Ops teams. While there are plenty of material on the Web describing how to change your ASCII banner, there is not much on how to efficiently manage the log output.
By default, Spring Boot will output log on the console and not use any file at all.
However, it’s possible to tell Spring Boot to log in an output file. At the simplest level, the path where Spring Boot will put the
spring.log file can be easily configured in the
application.properties under the
Note that another alternative is to use the
logging.file key in order to not only set the file path but also the file name.
While this works very well for development purposes, it’s not an acceptable process for the Ops team to unzip the final jar, update the
application.properties file and repackage it — this for each and every different environment.
Spring Boot allows to override the value set in the packaged file (if any) on the command-line as a standard system property when launching the jar:
java -jar -Dlogging.path=/tmp myapp.jar
Finally, it’s also possible to override this value when invoking the Spring Boot Maven plugin on the command line. However, directly using the system property doesn’t work for the plugin will spawn another JVM. One has to use the
run.jvmArguments system property and delegate it the wanted value:
mvn spring-boot:run -Drun.jvmArguments="-Dlogging.path=/tmp"
Note that this works for every available property!
Published at DZone with permission of Nicolas Fränkel, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.