Java logging (SLF4J, Log4j2, Logback)
46 questions in 13 subtopics.
Logging framework architecture
Describe the main parts of a Java logging framework such as Logback or Log4j2: loggers, the logger hierarchy, levels, appenders, layouts and filters. What happens, step by step, when code callslog.info("Order {} saved", id)?After adding an appender for the logger com.shop.payment in logback.xml, every payment log line appears twice in the console. The root logger also has the console appender. Explain the cause and two ways to fix it.Logs, metrics and traces are handled by different libraries and teams in your company. With OpenTelemetry now common, how would you design the relationship between application logging and tracing across 150 services?Java went through java.util.logging, Log4j 1, Commons Logging, SLF4J, Logback and Log4j2, and many applications still ship several of them. Explain why the ecosystem fragmented this way and what standard you would set for a company with hundreds of Java services today.SLF4J facade and bindings
What is SLF4J, and how does it find the logging backend at runtime in SLF4J 2.x compared with 1.7? What does the warning 'No SLF4J providers were found' mean?Rewrite these logging calls correctly with the SLF4J API and explain what is wrong with each:log.debug("Loaded " + items.size() + " items: " + items);
log.error("Failed: " + e.getMessage());
log.info("User {} logged in from {}", userId);
StackOverflowError deep inside logging, and the classpath contains both log4j-over-slf4j and log4j-slf4j-impl (or jcl-over-slf4j and slf4j-jcl). Explain what is happening and how you would find and fix the dependency problem.Your company publishes 60 internal libraries used by 300 services. Write the rules for how those libraries should log, and explain how you would enforce them.Appenders
Name the common appenders in Logback or Log4j2 and say what each is used for: console, file, rolling file, async, socket or syslog, and a database or message broker appender. Which ones would you use for a Spring Boot service running in Kubernetes?A team added a Kafka appender to send logs straight from 40 services to the log platform. During a Kafka outage, request latency rose sharply and some pods hung. Explain why and how you would redesign log shipping.Design the logging output standard for a platform of 300 Java services that run in Kubernetes and on some virtual machines, with a central log store. Cover appenders, format, buffering, delivery guarantees and what each service team is allowed to change.A vendor offers a proprietary logging agent that replaces your open source logging pipeline and promises 40 percent lower cost, but requires a vendor SDK in every service. How would you evaluate the build versus buy decision?Rolling files and retention
Write a LogbackRollingFileAppender configuration that rolls the file daily and when it reaches 100 MB, compresses old files, keeps 14 days and caps total size at 5 GB. Explain each setting.On a VM, a service's disk filled up although the rolling policy says maxHistory 30. The log directory holds thousands of files, some still open by the process after deletion (lsof shows deleted files). List the possible causes and how you would fix each.Legal says application logs must be kept for one year for some systems, while security wants personal data removed after 30 days, and finance wants logging costs cut by half. How would you set log retention policy across the company's Java services?Your company still runs about a hundred Java applications on virtual machines that write rolling log files, while new services are on Kubernetes with stdout logging. Over the next three years, how would you plan the move to a single logging model, and what risks would you manage?Layouts and patterns
Explain this Logback pattern and what each part prints: %d{yyyy-MM-dd'T'HH:mm:ss.SSSXXX} %-5level [%thread] %logger{36} [%X{traceId}] - %msg%n%ex. Which parts are expensive?Your log platform requires JSON logs. Compare the options for a Spring Boot 3.4+ service: Spring Boot structured logging (logging.structured.format.console=ecs or logstash), the logstash-logback-encoder library, and Log4j2 JsonTemplateLayout. What fields would you add and how do you keep stack traces usable?Filters
What kinds of filters exist in Logback and Log4j2 (level filters, threshold filters, evaluator or script filters, turbo filters, MDC or marker filters), and give one practical use for a filter versus simply setting a logger level.A support engineer needs DEBUG logs for one tenant that is having problems, in production, for one hour, without restarting and without turning on DEBUG for all 2,000 requests per second. How would you implement this with logging filters?One noisy dependency logs the same WARN thousands of times per minute during network blips, filling the log store and hiding other errors. Propose platform-wide protection against log floods for all services.Configuration files
In a Spring Boot application, what is the difference betweenlogback.xml and logback-spring.xml, and how do application.properties settings such as logging.level.* and logging.file.name interact with them?A service logs in the right format locally but in production prints the default Spring Boot format and ignores the logger levels from logback-spring.xml. The production container image is built from the same jar. What would you check?Every one of your 120 services has its own logback-spring.xml, copied and changed over years, with different patterns, appenders and levels. How would you consolidate logging configuration without breaking teams?Log levels
Explain what TRACE, DEBUG, INFO, WARN and ERROR should mean in a backend service, and classify these events: a request validation failure from a client, a retry that succeeded, a database connection lost and then restored, a payment provider returning 500 after all retries.The on-call team complains that ERROR logs from your 25 services fire about 50,000 times a day, mostly for things nobody acts on, so real problems are missed. How would you clean up level usage and keep it clean?You lead platform engineering. Teams set log levels in very different ways: some run DEBUG in production for months, some log nothing below WARN. Log storage cost doubled in a year. What standards and mechanisms would you introduce?A board-level incident review found the company could not reconstruct what happened during a customer data incident because key events were logged at DEBUG and turned off. How would you change the company's approach so this does not happen again, without logging everything?Runtime reconfiguration
How can you change a logger's level in a running Spring Boot service without a restart? Describe the Actuator loggers endpoint, Logback scan, and Log4j2monitorInterval, with one risk of each.Design a safe way for engineers to raise log levels across all instances of a service during an incident, with automatic revert after 30 minutes and an audit trail. Which components would you use?Engineers can change log levels through Actuator in production, and twice this year someone set the root logger to DEBUG, which filled disks and doubled log costs. Write the policy and technical guard rails you would put in place.Masking sensitive data
A developer logslog.info("Creating customer {}", customer) and the Customer class's toString() includes email, phone and card number. Show at least three ways to stop sensitive data from reaching the logs, and say which one you prefer.A security review finds bearer tokens and passwords in logs from HTTP client debug logging and from exception messages that include request bodies. Logs are already shipped to a central store kept for 90 days. Describe your immediate actions and the lasting fix.Under GDPR and PCI DSS, your company must show that personal and card data do not leak into logs across 200 services. Design the controls, ownership and evidence you would present to auditors.Logs are copied into many systems: log platforms, SIEM, data lakes, vendor support tickets and backups. At company scale, how would you think about the cost and risk of logging personal data, and what strategy would you set for the next five years?Asynchronous logging
How does Logback'sAsyncAppender work? Explain queueSize, discardingThreshold, neverBlock and includeCallerData, and what happens to queued events when the JVM shuts down.Compare Log4j2 async loggers based on the LMAX Disruptor with an async appender based on a blocking queue. What happens when the ring buffer is full, and how would you tune or configure it for a service that logs heavily under bursts?A trading platform must keep p99.9 latency under 2 milliseconds, but every request writes about ten log lines. Logging shows up at 15 percent of CPU and causes latency spikes during disk flushes. Describe how you would redesign logging for this system.Over 20 years, logging moved from synchronous file writes on application servers to asynchronous, structured, centrally shipped events. Looking ahead, what role should application logs play compared with metrics, traces and events, and how would you steer investment?Java.util.logging
Explainjava.util.logging: Logger, Handler, Formatter, its levels (SEVERE to FINEST), and how logging.properties is used. Why do most Spring applications not use JUL directly?In a Spring Boot service, a library logs through java.util.logging and its messages appear in a different format on stderr, and enabling FINE for it slows the service a lot after you installed the jul-to-slf4j bridge. Explain both problems and the fixes.A product built on an application server and several JDK-internal tools logs through java.util.logging, while newer modules use SLF4J and Logback. Operations wants one log format and one place to configure levels. How would you get there?Log4Shell and logging security
Explain how the Log4Shell vulnerability (CVE-2021-44228) worked in Log4j2 and why simply logging a user-supplied header could lead to remote code execution. Which versions fixed it?A new critical CVE in a logging library is announced on a Friday. You run 150 Java services, some with shaded or fat jars, some third-party vendor products. Describe your first 48 hours.After Log4Shell, your CTO asks how to govern logging libraries so the company is never again surprised by a library hidden in 400 artifacts. What governance, tooling and ownership do you propose?Log4Shell was a feature (lookups) that few users needed, inside a volunteer-maintained library used by most of the industry. Drawing on that, how should a large company decide which open source dependencies it relies on, and what does it owe those projects?No question matches these choices.