Configuring Logging

Levels per Package

application.yml

application.yml

logging:
  level:
    root: INFO                          # everything
    com.shopease: DEBUG                 # our code: more detail
    org.springframework.web: DEBUG      # see request mapping decisions
    org.hibernate.SQL: DEBUG            # print SQL (when JPA is added)

Tip: You can also change a level for one run without editing files: java -jar app.jar --logging.level.com.shopease=DEBUG.

Writing to a File

application.yml

application.yml

logging:
  file:
    name: logs/shopease.log             # also write to this file
  logback:
    rollingpolicy:
      max-file-size: 10MB               # start a new file after 10 MB
      max-history: 7                    # keep 7 days of old files
  pattern:
    console: "%d{HH:mm:ss.SSS} %-5level [%thread] %logger{20} - %msg%n"

Example

With the custom console pattern the output becomes shorter:

./mvnw spring-boot:run

Example output

10:25:41.903 INFO  [main] c.s.ShopeaseApplication - Started ShopeaseApplication in 1.802 seconds
10:25:47.215 INFO  [nio-8080-exec-1] c.s.p.ProductController - Returning 4 products
10:25:47.216 DEBUG [nio-8080-exec-1] c.s.p.ProductController - Product ids: [1, 2, 3, 4]

Advanced: logback-spring.xml

For full control (JSON logs, different files per level, profile-specific settings), add src/main/resources/logback-spring.xml. Spring Boot picks it up automatically:

<configuration>
    <include resource="org/springframework/boot/logging/logback/defaults.xml"/>
    <include resource="org/springframework/boot/logging/logback/console-appender.xml"/>

    <springProfile name="dev">
        <root level="DEBUG"><appender-ref ref="CONSOLE"/></root>
    </springProfile>
    <springProfile name="prod">
        <root level="INFO"><appender-ref ref="CONSOLE"/></root>
    </springProfile>
</configuration>

Note: Since Spring Boot 3.4 you can switch the console to structured JSON logs with just logging.structured.format.console=ecs (or logstash), which log tools like ELK and Grafana Loki read easily.