Spring Boot - Save Logs to a file on disk
The Spring Boot application generates several logs while running, that can be seen on console from where it is run.
If the application is run on a IDE, the logs can be seen on IDE console. Similarly, if it is run using a command line tool, the logs can be seen on the tool itself.
However, we can save all such logs to a file on the machine hard disk with the below configuration.
Configuration Properties
Add the below properties in the application.properties
or application.yml
file as shown below.
logging.file.path=/path/to/a/file
logging.file.name=log.txt
We can even specify the logging level by adding the below property, which determines the root package to consider for logging level.
logging.level.<logger>=DEBUG
Similarly, the properties can be specified on yaml file as below.
logging:
file: logs/application-debug.log
pattern:
console: "%d %-5level %logger : %msg%n"
file: "%d %-5level [%thread] %logger : %msg%n"
level:
org.springframework.web: ERROR
com.o: INFO
org.hibernate: ERROR
com.randomcodez: INFO
Logging Levels
Here is the list of valid logging levels and their order.
Logging Order | Logging Level | Description |
1 | TRACE | Includes all TRACE, DEBUG, INFO, WARNING, and ERROR logs. |
2 | DEBUG | Includes all DEBUG, INFO, WARNING and ERROR logs. |
3 | INFO | Includes all INFO, WARNING, and ERROR logs. |
4 | WARNING | Includes all WARNING and ERROR logs. |
5 | ERROR | Includes all ERROR logs. |
Conclusion
Now, we know how to save the logs to a file.