Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring boot logging

I am using Spring boot to build a simple REST service and I am wondering about the most appropriate way to handle the logging.

In my application.properties file I have the following:

logging.level.org.springframework.web: DEBUG

While developing the application I simply run it as such:

java -jar myapp.war

thus, I got all the nice log messages in stdout. However, I intend to deploy it, and I'm wondering what is the most appropriate way to deploy the application and still have my logs.

Sure, one can simply redirect the output

java -jar myapp.war >> somefile

but this is not very elegant, and I want to deploy my application so that it can easily be used as a service:

ln -s /my/app/xyz.war /etc/init.d/xyz

Then, doing

service xyz start|stop|restrart

to manage it. Seems like doing this prevents me from redirecting stdout ..

Any ideas or advice about this?

like image 762
nmpg Avatar asked Feb 17 '26 14:02

nmpg


2 Answers

What you are really after is Spring Boot file logging output functionality.

Quoting the above documentation:

By default, Spring Boot will only log to the console and will not write log files. If you want to write log files in addition to the console output you need to set a logging.file or logging.path property (for example in your application.properties).

Essentially adding in your application.properties:

logging.file=name.of.my.log.file.log
logging.path=/path/where/above/log/file/gets/stored
like image 120
dimitrisli Avatar answered Feb 19 '26 04:02

dimitrisli


In you application.properties file you can set two attributes for your logging.file.

Like in the documentation description: (26.3 File output)

By default, Spring Boot will only log to the console and will not write log files. If you want to write log files in addition to the console output you need to set a...

  • logging.file

    Writes to the specified log file. Names can be an exact location or relative to the current directory.

  • logging.path

    Writes spring.log to the specified directory. Names can be an exact location or relative to the current directory.

After setting one of those logging properties will be written in a file.

Here you can find the complete doc

like image 45
Patrick Avatar answered Feb 19 '26 02:02

Patrick



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!