If your app logs look like a toddler discovered a drum kit you are in the right place. This guide shows how to pick the logging starter framework manage logging levels tune patterns and send logs to files in Spring Boot without crying in production.
Spring Boot ships with Logback via spring boot starter logging by default. It is unopinionated and fast enough for most use cases. If you prefer a different feature set swap the starter. For example exclude the default and add spring boot starter log4j2 to use Log4j2 instead. Do not leave multiple logging frameworks on the classpath unless you enjoy duplicate messages and confusion.
Logging levels are how you stop your logs from turning into a firehose. Use application properties to set the root level and override packages as needed. Levels go from most verbose to least verbose like this
Example in application properties
logging.level.root=INFO
logging.level.com.example=DEBUG
Keep root at WARN or INFO in production and enable DEBUG only for the packages you are actively troubleshooting. This avoids scraping through megabytes of fluff when something actually goes wrong.
Make your logs machine friendly and human readable at the same time. Use clear patterns for timestamps logger names and messages. Here are common properties that work with the built in starters.
logging.pattern.console=%d{yyyy-MM-dd HH-mm-ss} %-5level %logger{36} - %msg%n
logging.file.name=app.log
logging.file.path=/var/log/myapp
Note that logging.file.name takes precedence if you set both name and path. Choose predictable file locations so grepping alert rules and log rotation do not become a surprise party.
Put environment specific rules in application production properties or pass overrides on the command line or as environment variables. Profiles let you run verbose logging locally and quiet logging in production without changing code.
If you see duplicate messages check for multiple logging frameworks on the classpath. If log routing looks wrong boost framework specific packages to TRACE to inspect filters and appenders. If timestamps or patterns look off verify the pattern property and confirm the active starter supports that format.
Use an asynchronous appender when logging is on the hot path. That reduces latency under load and keeps your application threads from getting stuck writing logs. Also keep the root level at WARN in production and enable DEBUG only for targeted packages.
Choose the right starter for your needs keep root level conservative and use package scoped overrides to narrow the noise. Configure clear patterns and file paths and use profiles for environment specific settings. When things go wrong use TRACE on framework packages to debug duplicates and consider async appenders for high throughput scenarios. Logging is not glamorous but it is the difference between calm incident triage and frantic stack overflow searches at 2 AM.
I know how you can get Azure Certified, Google Cloud Certified and AWS Certified. It's a cool certification exam simulator site called certificationexams.pro. Check it out, and tell them Cameron sent ya!
This is a dedicated watch page for a single video.