I’m relatively new to Spring Boot, but I find the application.yml configuration pretty nice. But if you want to get fancy with spring boot logging, you have to resort to creating a logback file! Well I came up with this pattern to at least keep as much of the values as I can in application.yml.
First you do still need to add a logback-spring.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | <?xml version="1.0" encoding="UTF-8"?> <configuration> <include resource="org/springframework/boot/logging/logback/defaults.xml" /> <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>${LOG_PATH}/${LOG_FILE}.log</file> <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy"> <!-- daily rollover --> <fileNamePattern>${LOG_PATH}/${LOG_FILE}.%d{yyyy-MM-dd}.%i.log</fileNamePattern> <!-- each file should be at most 100MB, keep 30 days worth of history, but at most 500MB --> <maxFileSize>100MB</maxFileSize> <maxHistory>30</maxHistory> <totalSizeCap>500MB</totalSizeCap> </rollingPolicy> <encoder> <pattern>${FILE_LOG_PATTERN}</pattern> </encoder> </appender> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>${CONSOLE_LOG_PATTERN}</pattern> </encoder> </appender> <root level="DEBUG"> <appender-ref ref="STDOUT" /> <appender-ref ref="FILE" /> </root> </configuration> |
If you’re looking for a slightly different log rolling behavior, you can likely modify the above sample with the appender docs for ch.qos.logback.core.rolling.
And here is my application.yml. Note the use of classpath to keep the location of the config consistent.
1 2 3 4 5 6 | logging: config: classpath:logback-spring.xml # Gets exposed as $LOG_FILE file: my_fancy_service # Gets exposed as $LOG_PATH path: . |
Now in my application-production.yml I might have something like this:
1 2 3 | logging: # Gets exposed as $LOG_PATH path: /var/log |
This means in by default, logs will get put in ./my_fancy_service.log (and roll into my_fancy_service.2018-01-08.0.log and then my_fancy_service.2018-01-08.1.log etc as the log hits the maxFileSize). Assuming in production I’m using the application-production.yml profile, the log files will be the same but end up in /var/log. Keeping the configurations as close as possible between prod and dev means less surprises!