Skip to content
Ameen Abozaid
JNR-006

Logging

  • 7 Lessons
  • 76 min of reading
  • 2 h practice (estimate)
On this page 8 topics

Overview

Topics

  • SLF4J
  • Logback
  • log levels
  • structured messages
  • avoiding sensitive data

Learn first

76 min of reading · 2 h of practice (estimate)

Learning outcomes

  1. Log through SLF4J with parameterized messages and choose the correct level.
  2. Configure Logback appenders, patterns, and per-package levels.
  3. Log exceptions with their stack traces exactly once.
  4. Identify and remove sensitive data (passwords, tokens, personal data) from logs.
  5. Explain structured (JSON) logging and why machines read logs too.

Lessons

  1. 01Why System.out is not logging11 min read
  2. 02SLF4J and Logback11 min read
  3. 03Choosing log levels11 min read
  4. 04Logging exceptions well11 min read
  5. 05Sensitive data in logs11 min read
  6. 06Structured logging10 min read
  7. 07Demo: from println to structured JSON logs11 min read

Lab and exercises

Lab

JNR-006-LAB1 · Observable Shop

Outcomes: 1–5 · Time: ~1.5 h Add SLF4J logging to Shop Core and the CLI. Switch to JSON output by configuration only. Show a masked card number in the output. Acceptance: no secrets in logs; no duplicate stack traces; levels chosen correctly; JSON output enabled without code changes.

Exercises (assessment: review a log file and a logging config for problems, plus six exercises)

IDOutcomeTask
JNR-006-EX011Convert System.out.println("Order " + id + " saved") to an SLF4J call with a placeholder and the right level.
JNR-006-EX022Write a logback.xml with a console appender, a pattern, root INFO, and com.shop at DEBUG.
JNR-006-EX031Choose a level: payment declined, database down, order placed, loop variable value, retry succeeded.
JNR-006-EX043Fix a catch block that does log.error(e.getMessage()); throw e;.
JNR-006-EX054Write mask(String card) that returns only the last four digits, and list five values never to log.
JNR-006-EX065Explain in three sentences why JSON logs help, and show one example line.
Show solutions
  • EX01 log.info("Order {} saved", id);
  • EX02 <logger name="com.shop" level="DEBUG"/> and <root level="INFO"><appender-ref ref="STDOUT"/></root> with <pattern>%d %-5level %logger{20} - %msg%n</pattern>.
  • EX03 WARN; ERROR; INFO; DEBUG; INFO or WARN (team choice, justified).
  • EX04 Either log with the exception and handle: log.error("Payment failed", e); or just throw e and log at the top level, not both.
  • EX05 "**** **** **** " + card.substring(card.length() - 4). Never log: passwords, tokens, API keys, full card numbers, national IDs.
  • EX06 Machines can filter by field, count errors, and follow one request; people still read the message field.
  • Outcome 2 and 5 are also covered by the lab and quiz Q02, Q09–Q10.

Quiz

Outcomes: O1 SLF4J and levels · O2 Logback config · O3 exceptions · O4 sensitive data · O5 structured logging.

A self-check for you: nothing is graded, saved or sent anywhere. Choose an answer, then check it. You can try again as often as you like.

  1. 1SLF4J is:O1
  2. 2Which is the best log call?O1
  3. 3Which level for an expected "user not found"?O1
  4. 4In Logback, an appender defines:O2
  5. 5You can set com.shop to DEBUG and the rest to INFO using:O2
  6. 6How do you log an exception with its stack trace?O3
  7. 7Log-and-rethrow is a problem because:O3
  8. 8Which must never be logged?O4
  9. 9A good way to log a card number is:O4
  10. 10JSON logs help because:O5

Practical part

Six exercises JNR-006-EX01 to EX06 plus the lab; the assessment reviews a log file and a logging config for problems. Pass: at least 8 of 10 quiz answers, five exercises correct, and a lab with no secrets and no duplicate stack traces in the logs.

Common mistakes

String concatenation in log calls; log-and-rethrow; ERROR for expected situations.

Assessment

Review a log file and a logging config for problems.

Career relevance

Troubleshooting (JNR-054) and observability (MID-047).

Type to search levels, phases, modules and lessons.