简体   繁体   中英

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?

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

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

I want to give a short update in 2020 :


logging.file and logging.path are now deprecated :

显示 logging.file 和 logging.path 已弃用的 application.properties 的屏幕截图


Instead, I suggest to use logging.file.name in your application.properties ( logging.file.path is also available).

Example: logging.file.name=./logdir/spring.log

Spring Boot support almost every logging frameworks you can use as per your convenience, but I will suggest you to use slf4j logging framework and customize using logback.xml it's very easy look at

1) Create LOGGER object adding this single line of code in your class

private static final Logger LOGGER = LoggerFactory.getLogger(YourClassName.class);

2) Create logback.xml file in /resource folder and copy below code

<?xml version = "1.0" encoding = "UTF-8"?>
<configuration>
   <appender name = "STDOUT" class = "ch.qos.logback.core.ConsoleAppender">
      <encoder>
         <pattern>[%d{yyyy-MMM-dd'T'HH:mm:ss.sss'Z'}] [%C] [%t] [%L] [%-5p] %m%n</pattern>
      </encoder>
   </appender>

   <appender name = "FILE" class = "ch.qos.logback.core.FileAppender">
      <File>/var/tmp/app.log</File> <!-- LOCATION of FILE WHERE YOU WANT TO SAVE -->
      <encoder>
         <pattern>[%d{yyyy-MM-dd'T'HH:mm:ss.sss'Z'}] [%C] [%t] [%L] [%-5p] %m%n</pattern>
      </encoder>
   </appender>

   <root level = "INFO">
      <appender-ref ref = "FILE"/>
      <appender-ref ref = "STDOUT"/> 
   </root>
</configuration>

Using logback.xml configuration you can customize your application logging in spring boot

Why wouldn't you wrap it in docker container?

It will give you possibility to have everything inside the container and distribute it easily.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM