Spring boot logging

2019-09-02 10:21发布

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?

4条回答
Bombasti
2楼-- · 2019-09-02 10:31

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
查看更多
We Are One
3楼-- · 2019-09-02 10:42

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

查看更多
forever°为你锁心
4楼-- · 2019-09-02 10:51

Why wouldn't you wrap it in docker container?

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

查看更多
smile是对你的礼貌
5楼-- · 2019-09-02 10:53

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

查看更多
登录 后发表回答