Scrivio Interview library
DesignAdvanced7 min read · updated 12 November 2026

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.

Internal libraries depend only on slf4j-api, or on System.Logger for very low-level code with no dependencies, and never on a backend or binding; they use logger names that follow their own packages and never ship configuration files such as logback.xml. They log sparingly, DEBUG for detail and WARN only for something the application owner should act on, they never log and throw, they never log sensitive data or use System.out, and they publish counts as Micrometer metrics rather than log lines. We enforce the rules with build checks such as enforcer rules and ArchUnit tests, a library template that starts every project compliant, and code review with a checklist.

Why this question is asked

A library is a guest in someone else's application. When 60 internal libraries are used by 300 services, a careless logging choice in one library repeats itself 300 times: a forced backend, a configuration file that overrides the application's own, or a log line on every call that fills the log platform. Interviewers ask for written rules because it shows whether we can think from the consumer's side and turn good habits into something checkable. In this answer, we will recap why libraries are different, write the dependency rules, the naming and configuration rules, the content rules, and then the enforcement.

1Why Libraries Need Different Rules

An application owns its process: it chooses the logging backend, the format, the destinations and the levels. A library owns none of these. It runs inside many applications whose owners made different choices, and its logs end up in their log platforms, on their bills and in their on-call pages. So every library rule follows from one principle: the library provides useful, controllable information, and the application decides what to do with it.

In practice, this means a library must be easy to silence, easy to turn up when debugging, cheap when silent and safe in what it writes.

2Rule 1: Depend on the API Only

Libraries depend on slf4j-api and nothing else from the logging world. They never depend on logback-classic, log4j-core or any SLF4J adapter, because the application chooses the backend, and a second backend on the classpath causes warnings about multiple providers or even logging loops. For tests, a backend can be added in test scope only.

Very low-level libraries that must have no dependencies at all, such as a small parsing utility, may use System.Logger, the JDK's own logging facade since Java 9, which applications can route to SLF4J. The slf4j-api version should be compatible with the company BOM, so libraries do not force an upgrade.

XML
<!-- library pom.xml -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId> <!-- version from the company BOM -->
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<scope>test</scope> <!-- only for the library's own tests -->
</dependency>

3Rule 2: Package-Based Names and No Configuration Files

Every logger is created with LoggerFactory.getLogger(ThisClass.class), so its name follows the library's package, such as com.acme.payments.client. An application can then turn the whole library to DEBUG or WARN with one line, logging.level.com.acme.payments=DEBUG in Spring Boot, without knowing its internals. Custom logger names that do not match packages are allowed only for clearly documented categories, such as com.acme.payments.client.wire for request and response dumps.

Library jars must never contain logback.xml, logback-test.xml, log4j2.xml or similar files. Logback looks for logback.xml on the classpath at startup, so a copy inside a library jar can silently become the application's configuration, or change which file wins depending on classpath order.

4Rule 3: Log Sparingly and Choose Levels Deliberately

Libraries log at DEBUG for detail that helps someone diagnose the library: which endpoint was chosen, how long a retry waited. INFO is rare and reserved for once-per-lifetime events such as 'connected to cluster X'. WARN means something the application owner should act on, like a deprecated configuration value; if nobody would act on it, it is DEBUG. ERROR is almost never right in a library, because the library usually throws an exception and the application decides whether it is an error.

Above all, a library never logs and throws. If it logs an exception and then rethrows it, the application, which also handles the exception, logs it again, and one failure appears twice or three times in the logs. The library either handles the problem, and may then log it, or throws an exception with a clear message and the cause attached.

Java
// Not allowed in a library: log and throw
catch (IOException e) {
log.error("Call to {} failed", url, e); // logged here...
throw new PaymentClientException("call failed", e); // ...and again by the caller
}
// Allowed: throw with context, let the application decide
catch (IOException e) {
throw new PaymentClientException("POST " + url + " failed after " + attempts + " attempts", e);
}
Levels for library code
LevelUse in a libraryExample
ERRORalmost never; throw instead-
WARNthe application owner should actdeprecated property payments.timeout used
INFOrare lifecycle eventsconnected to payments cluster eu-1
DEBUGdiagnostic detailretry 2 of 3 after 400 ms
TRACEvery verbose detailfull request headers (redacted)

5Rule 4: Safe Content and Metrics Instead of Counters

Libraries often handle data they do not own, so they never log secrets, tokens, full card numbers or personal data, even at DEBUG; if a request dump is useful, it goes through a redaction helper and stays at TRACE. Libraries never write to System.out or System.err and never call printStackTrace(), because those bypass the application's logging configuration entirely.

When a library wants to report how often something happens, such as retries or cache misses, it exposes a Micrometer counter or timer instead of a log line per event, with micrometer-core as an optional dependency. Metrics are cheap, aggregatable and alertable, while a log line per retry across 300 services is expensive noise.

6Enforcing the Rules

The rules become real through automation. The shared library parent POM includes an enforcer rule that bans logging backends and adapters in compile and runtime scope. An ArchUnit test, which checks code structure as a unit test, forbids imports from backend packages and access to the standard streams; ArchUnit even ships ready-made rules for the standard streams and for java.util.logging. A CI step lists the jar contents and fails if a logging configuration file is inside.

Java
@AnalyzeClasses(packages = "com.acme")
class LoggingRulesTest {
@ArchTest static final ArchRule noBackendClasses = noClasses().should()
.dependOnClassesThat().resideInAnyPackage("ch.qos.logback..", "org.apache.logging.log4j.core..");
@ArchTest static final ArchRule noStandardStreams =
GeneralCodingRules.NO_CLASSES_SHOULD_ACCESS_STANDARD_STREAMS;
}

A library template, used to create every new library, already has the parent POM, the ArchUnit test and an example logger, so new libraries start compliant. Code review handles what tools cannot judge, such as level choices and sensitive data, with a short checklist. For existing libraries, we run the checks in report mode first, fix the findings in a few releases, and then turn them into failures.

7Follow-up questions

Should a library use MDC itself?

Generally no. The MDC belongs to the application, which puts request ids or trace ids there. A library that sets its own keys can overwrite the application's values or leave stale values on pooled threads. If a library has truly useful context, it should add it to its own log messages, or document a key, set it in a try block and remove it in finally, restoring any previous value.

Why not let libraries use java.util.logging since it is always available?

Because the company's applications use SLF4J backends, so JUL calls have to be bridged with jul-to-slf4j, which has a cost when levels are not propagated. SLF4J is the API the applications expect; System.Logger is the exception for code that cannot have any dependency.

How do you stop a chatty library in an application?

Because loggers follow packages, the application sets that package to WARN in its configuration, for example logging.level.com.acme.payments=WARN. That only works because of rule 2, which is why naming rules matter.

8Conclusion

In this answer, we wrote logging rules for internal libraries from the principle that the application, not the library, owns logging. Libraries depend only on slf4j-api or System.Logger, name loggers after their packages, ship no configuration files, log sparingly with deliberate levels, never log and throw, never log sensitive data or use the standard streams, and publish counts as Micrometer metrics. We enforced the rules with enforcer bans, ArchUnit tests, a jar content check, a library template and a review checklist.

What to say in the interviewAbout 90 seconds aloud

Open with

My starting principle is that a library is a guest in the application, so the application owns logging and the library only provides useful, controllable information. The rules follow from that.

Then, if they want more

First, libraries depend only on slf4j-api, or on System.Logger if they truly must have no dependencies, and never on Logback, Log4j core or any binding, because that would force a backend on 300 services.

Second, loggers are created per class, so names follow the library's packages and an application can turn the whole library up or down with one property, and no library jar contains logback.xml or log4j2.xml, since Logback could pick it up as the application's configuration.

Third, log sparingly: DEBUG for detail, INFO only for rare lifecycle events, WARN only when the application owner should act, and almost never ERROR. Libraries never log and throw, because the caller will log the same failure again; they throw with context and the cause.

Fourth, no secrets or personal data, no System.out or printStackTrace, and counts like retries go to Micrometer metrics instead of a log line per event.

To enforce it, I would put an enforcer rule in the library parent POM banning backends, an ArchUnit test banning backend packages and standard streams, a CI check for configuration files inside jars, a library template that starts compliant, and a short review checklist for what tools cannot judge, like level choices.

About this answer

Last updated
12 November 2026
Versions it names
Java 9
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 "Your company publishes 60 internal libraries used by 300 services. Write the rules for...": https://scrivio-interview-library.netlify.app/java/java-logging/3e9a2e6758/