Sub topic 2
Setting log levels in application properties
Assume you know basic Java, have a Spring Boot application, and can edit application.properties.
Set levels with properties
A logger records messages for a package or class. Spring Boot supports these levels, from least to most severe: TRACE, DEBUG, INFO, WARN, and ERROR. A configured level also enables more severe levels. For example, INFO shows INFO, WARN, and ERROR, but hides DEBUG.
logging.level.root=WARN
logging.level.com.example.orders=DEBUG
logging.level.org.springframework.web=INFOThe root logger is the default for every logger. A package setting overrides that default for matching classes. Use package names rather than individual classes when you want a subsystem to share one setting.
Group related loggers
A log group gives several packages one name. You can then change them together.
logging.group.orders=com.example.orders,com.example.billing
logging.level.orders=DEBUGThis group enables debug messages from both packages. Groups are useful when diagnosing one application area that crosses package boundaries.
Write to the console and files
Spring Boot writes logs to the console by default. To create a file, set its name and rolling policy. Rolling creates a new file when the current file reaches a limit and can retain a fixed history.
logging.file.name=logs/app.log
logging.logback.rollingpolicy.max-file-size=10MB
logging.logback.rollingpolicy.max-history=14The path is relative to the process working directory. Check the directory's permissions before deploying.
Customize the log pattern
A pattern controls the text on each line. This example includes the timestamp, level, request ID from MDC, logger name, and message.
logging.pattern.console=%d{HH:mm:ss.SSS} %-5level [%X{requestId}] %logger{36} - %msg%n%X{requestId} is empty unless application code places a value named requestId in the logging context.
Use logback-spring.xml for advanced rules
Use logback-spring.xml when properties cannot express your appenders, filters, or profile-specific rules. Spring Boot can process <springProfile> in this file.
<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>Exercise
Which property enables DEBUG messages for com.example.orders while leaving other packages at WARN?
Show answer
logging.level.root=WARN and logging.level.com.example.orders=DEBUG.