Quick revision
The short answer
A logging framework is built from named loggers arranged in a hierarchy by their dots, levels that decide what is worth recording, appenders that write to a destination, layouts or encoders that turn an event into text, and filters that can accept or reject events. When we call log.info("Order {} saved", id), the logger first checks INFO against its effective level and returns at once if INFO is disabled; otherwise it creates an event with the time, thread, logger name, level, message pattern, arguments and MDC, and hands it to its own appenders and, with additivity, to every ancestor's appenders up to root. Each appender runs its filters, formats the event, which is when {} is finally replaced by the id, and writes it out.
What to remember
In this answer, we recapped loggers and their dotted hierarchy, effective levels, appenders, layouts and encoders, and filters. We then followed log.info("Order {} saved", id): a cheap effective-level check, creation of an event that captures time, thread, logger, level, pattern, arguments and MDC, delivery to the logger's appenders and its ancestors' while additivity is true, and finally each appender's filters, formatting and writing. Because {} is replaced only at formatting time, parameterized messages cost almost nothing when the level is disabled.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
A logging framework is built from named loggers arranged in a hierarchy by their dots, levels that decide what is worth recording, appenders that write to a destination, layouts or encoders that turn an event into text, and filters that can accept or reject events. When we call log.info("Order {} saved", id), the logger first checks INFO against its effective level and returns at once if INFO is disabled; otherwise it creates an event with the time, thread, logger name, level, message pattern, arguments and MDC, and hands it to its own appenders and, with additivity, to every ancestor's appenders up to root. Each appender runs its filters, formats the event, which is when {} is finally replaced by the id, and writes it out.
Why this question is asked
We write log statements every day, and most of the time they just work. When they do not, the problem is almost always in one of these parts: a line that never appears because of a level on a parent logger, a line that appears twice because of additivity, or a slow request because an appender is blocking. Interviewers ask this question to see whether we have a mental model of the pipeline rather than a collection of configuration snippets. In this answer, we will recap each part with a small Logback configuration, then follow one log.info() call from start to finish, and finally see why parameterized messages are cheaper than concatenation.
1The Parts of a Logging Framework
A logger is the object our code calls. It has a name, usually the fully qualified class name, such as com.shop.order.OrderService. Names with dots form a hierarchy: com.shop.order is the parent of com.shop.order.OrderService, com.shop is its grandparent, and above everything sits the root logger. This lets us configure a whole package with one line.
A level says how important an event is. From least to most important, SLF4J has TRACE, DEBUG, INFO, WARN and ERROR. A logger may have its own level; if it does not, its effective level is that of its nearest ancestor that has one, and root always has one.
An appender is the part that writes events somewhere: the console, a file, a socket. A layout, or an encoder in Logback (an encoder wraps a layout and also turns the text into bytes), decides how an event looks as text, through a pattern such as %d %-5level [%thread] %logger - %msg%n. A filter is a small rule that can accept, deny or stay neutral about an event; filters can sit on an appender, and Logback also has turbo filters that run before an event is even created.
- Our code
log.info("Order {} saved", id)on the SLF4J API - Logger and level checkeffective level from the hierarchy; cheap early exit
- Logging eventtime, thread, logger, level, pattern, arguments, MDC, throwable
- Appenders up the hierarchyown appenders, then ancestors' while additivity is true
- Filters, then layout or encoderaccept or deny, then turn the event into text
- Destinationconsole, file, socket
6 layers, from the top down. Step through them one at a time.
Notice that com.shop has a level but no appender. Its events still reach the console, because of additivity, which we will see in the walk-through. Everything outside com.shop logs only at WARN and above.
2The Level Check
Our class holds a logger created with LoggerFactory.getLogger(OrderService.class), so its name is com.shop.order.OrderService. When we call info(), the logger first asks whether INFO is enabled. It has no level of its own, so it uses the nearest ancestor's level, com.shop at DEBUG, and INFO is above DEBUG, so the call continues.
If the effective level had been WARN, the method would return here. This check is a comparison of two small integers that Logback has already computed for each logger, so a disabled call costs almost nothing. Nothing has been formatted and no object has been created, except the argument array that varargs calls need. Logback's turbo filters, if configured, run just before or as part of this decision.
3Creating the Logging Event
Once the level passes, the framework creates a logging event, an object that captures everything known about this call at this moment: the timestamp, the current thread's name, the logger name, the level, the message pattern "Order {} saved", the argument array holding the id, any throwable passed as the last argument, and the MDC.
The MDC, short for Mapped Diagnostic Context, is a per-thread map of key and value pairs, such as a request id or a trace id, that a filter or interceptor put there at the start of a request. Because the event may be written later or on another thread, the framework copies the MDC into the event; Log4j2 does it when the event is created, while Logback copies it the first time the event's MDC map is read, and an async appender forces that copy on the calling thread before queueing the event.
4Walking Up the Hierarchy to the Appenders
Now the event is delivered to appenders. The logger com.shop.order.OrderService has none of its own, so it passes the event to its parent, com.shop.order, which has none either, then to com.shop, which also has none, and finally to root, which has CONSOLE. This walk is controlled by additivity: by default every logger is additive, meaning that after its own appenders, the event also goes to its parent's appenders, and so on up to root.
Notice that levels are checked only once, at the logger where the call was made. Root's level of WARN does not stop our INFO event on the way up, because the decision was already made at the start with the effective level. People are often surprised by this, but it is what lets us set com.shop to DEBUG while root stays at WARN.
If a logger sets additivity="false", the walk stops there. That is how we send one package's logs to its own file without also sending them to root's appenders.
5Filters, Formatting and Writing
Each appender that receives the event first runs its own filters. Our CONSOLE appender has a ThresholdFilter at INFO, so an INFO event passes, while a DEBUG event from com.shop would be denied here even though the logger allowed it.
Next, the encoder formats the event using its pattern. This is the moment the message is built: the framework takes "Order {} saved", replaces {} with the string form of the id, and produces "Order 42 saved". The rest of the pattern adds the time, level, thread and logger name. Finally, the appender writes the bytes to its destination, here standard output, usually through a lock so that lines from different threads do not interleave.
6Why Parameterized Messages Are Cheaper
Compare the parameterized call with string concatenation. With log.debug("Order " + id + " saved"), Java builds the full string before calling debug(), because arguments are evaluated before any method runs. If DEBUG is disabled, that work is thrown away. With log.debug("Order {} saved", id), only a reference to id is passed, the level check fails in step 1, and the message is never built.
The same applies after the level check: the placeholder is replaced only when an appender actually formats the event, so an event denied by every appender's filter is never formatted either. Arguments are still evaluated, though, so an expensive call such as computeSummary() as an argument still runs; for that, SLF4J 2's fluent API with a supplier, or an isDebugEnabled() guard, avoids it.
7Follow-up questions
What does additivity=false do and when do you need it?
It stops an event from travelling up to the parent loggers' appenders after the logger's own appenders have handled it. We need it when a logger has its own appender and we do not want the same event written again by an ancestor, for example sending com.shop.payment logs only to payment.log and not also to root's console, or avoiding duplicate lines when the child and root both reference the same appender.
Where is the MDC copied into the event?
In Log4j2, the ThreadContext data is injected into the LogEvent when the event is created on the calling thread. In Logback, LoggingEvent copies the MDC map lazily, the first time getMDCPropertyMap() is called, and prepareForDeferredProcessing(), which AsyncAppender calls before queueing, forces that copy, together with the formatted message, on the calling thread. That is why MDC values remain correct even when the writing happens on the async appender's worker thread.
Why does root's WARN level not drop INFO events from com.shop?
Because the level is checked once, against the effective level of the logger that was called. After that the event is handed to appenders up the hierarchy without further level checks on the ancestors. To limit what an appender writes, we use a threshold filter on the appender itself.
8Conclusion
In this answer, we recapped loggers and their dotted hierarchy, effective levels, appenders, layouts and encoders, and filters. We then followed log.info("Order {} saved", id): a cheap effective-level check, creation of an event that captures time, thread, logger, level, pattern, arguments and MDC, delivery to the logger's appenders and its ancestors' while additivity is true, and finally each appender's filters, formatting and writing. Because {} is replaced only at formatting time, parameterized messages cost almost nothing when the level is disabled.
Open with
A logging framework like Logback or Log4j2 has a few parts. Loggers are named, usually after the class, and their dotted names form a hierarchy, so com.shop.order inherits from com.shop and from root.
Then, if they want more
Each logger has an effective level, either its own or the nearest ancestor's. Appenders write to destinations like the console or a file, layouts or encoders turn an event into text with a pattern, and filters accept or reject events.
When I call log.info with Order curly braces saved and the id, the logger first compares INFO with its effective level. That check is just an integer comparison, so if INFO is disabled, the call returns immediately, before any formatting.
If it is enabled, the framework creates a logging event holding the timestamp, the thread, the logger name, the level, the message pattern, the arguments, any throwable and a copy of the MDC. Then it hands the event to the logger's own appenders and, because additivity is true by default, to every ancestor's appenders up to root.
The level is not rechecked on the way up. Each appender runs its filters, and only then does the encoder format the event, which is the moment the placeholder is replaced with the id, and finally it writes the bytes out.
That lazy formatting is why parameterized messages are cheaper than string concatenation: a disabled debug call never builds the string at all.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Describe the main parts of a Java logging framework such as Logback or Log4j2: loggers...": https://scrivio-interview-library.netlify.app/java/java-logging/e021176d72/