Logging Basics
Orders uses SLF4J with Logback by default in Spring Boot.
Introduction
Orders uses SLF4J with Logback by default in Spring Boot. Inject nothing exotic: private static final Logger log = LoggerFactory.getLogger(OrderService.class);
Log at the right level: INFO for business milestones (order placed), WARN for recoverable problems, ERROR for failures needing action, DEBUG for local diagnosis. Avoid logging every GET at INFO in production.
Prefer parameterized messages: log.info("placed order id={}", id) — not string concatenation. Never log tokens, passwords, or full card-like payloads.
Configure levels via logging.level.com.techlearningpro.orders=INFO in YAML profiles.
Purpose of this lesson
Add safe, structured-enough logging to Orders services and configure levels per environment.
Understanding the topic
Logs are an operations API.
- SLF4J API / Logback impl: Boot's default pairing.
- Levels: ERROR, WARN, INFO, DEBUG, TRACE.
- Parameterized messages: defer formatting and guard work.
- MDC: correlation ids for a request (optional next step).
- Config: application.yml and env-specific overrides.
Step-by-step explanation
- Add a logger to
OrderService. - Log id and sku on place at INFO — not the entire request body.
- Set DEBUG locally and INFO in prod profile.
- Trigger an error path and confirm ERROR includes a stack trace in logs only.
Informative example
Service logging.
@Servicepublic class OrderService {private static final Logger log = LoggerFactory.getLogger(OrderService.class);// ...public OrderView place(CreateOrderRequest req) {OrderView created = /* ... */;log.info("order_placed id={} sku={} qty={}", created.id(), req.sku(), req.qty());return created;}}
YAML level pin:
logging:level:com.techlearningpro.orders: INFOorg.hibernate.SQL: WARN
Real-world use
On-call engineers find checkout incidents by searching order_placed and correlation ids. TechLearningPRO Orders should make those events obvious without turning log sinks into a PII warehouse.
Security implications
Logs are sensitive storage.
- Redact secrets and personal data.
- Restrict who can read production log streams.
- Do not log full payment payloads.
Best practices
- Stable event-like messages (
order_placed) for grep/metrics. - Log exception objects as the last SLF4J argument to capture stacks.
- Tune third-party loggers (Hibernate, security) separately.
Common mistakes
- Logging passwords or bearer tokens.
- INFO spam on high-volume GETs.
System.out.printlnin services.
Advanced interview questions
Interview Prep
Practice concise answers, then expand each card for the explanation.
1BeginnerQuestionWhat logging facade does Spring Boot apps typically use?+
Answer
2IntermediateQuestionWhy use parameterized log messages?+
Answer
3IntermediateQuestionWhat must never appear in Orders logs?+
Answer
Hands-on exercise
Lab. Add order_placed INFO logging. Set package DEBUG locally, then switch prod profile to INFO and confirm debug lines disappear.
Summary
Orders logging makes operations possible without leaking data. Next you connect the service to a real database.