AI prompt: integrate & migrate to log4error¶
Copy this prompt into Cursor, Claude, ChatGPT, or similar. Paste your project context (build file, logging setup, sample classes) with it.
You are integrating the Java library log4error into this project and migrating existing logging to it.
- Spring Boot 3.x servlet apps: use io.github.parvez3019:log4error-spring-boot-starter:0.2.0 (preferred).
- Anything else: use the core io.github.parvez3019:log4error:0.2.0 plus a request filter.
Goal¶
Keep INFO/DEBUG context for failed requests without writing those logs on happy paths. On error, flush buffered context with the error log.
Library behavior¶
info()/debug()→ buffer in a request-scoped ThreadLocal (no I/O)error()→ print buffered logs, then log ERROR, then clear the bufferpInfo()/pDebug()/pWarn()/pError()→ immediate SLF4J pass-through (no buffering)- Needs a per-request lifecycle that creates and clears a
Logger: - Spring Boot: the starter auto-registers
Log4ErrorFilter; call sites useRequestLogger.get() - Other servlet apps: copy
examples/LoggerFilterExample.java(usesLoggerThreadLocal) - Default buffer cap is 500; oldest events drop with a one-time WARN
- Optional:
Logger.of(MyService.class)to preserve call-site logger names - Core JAR depends only on
slf4j-api(no Spring on the classpath unless you add it)
Integration steps¶
Spring Boot (preferred)¶
- Add
io.github.parvez3019:log4error-spring-boot-starter:0.2.0. Do not write a filter; the starter registers one. - Use
RequestLogger.get()at call sites (import io.github.parvez3019.spring.RequestLogger;). - Optional: set
log4error.buffer-size,log4error.request-id-header,log4error.mdc-keyinapplication.yml, and add%X{x-request-id}to the log pattern. - In the migration table below, read
Logger()asRequestLogger.get().
Other servlet apps¶
- Add the Maven/Gradle dependency for
io.github.parvez3019:log4error:0.2.0. - Add a Spring
OncePerRequestFilter(or plainFilter) that: set(new Logger())orset(Logger.of(SomeClass.class))on the request ThreadLocal at startremove()infinally(prevents leaks on pooled threads)- exposes
public static Logger Logger()for call sites - Copy from
examples/LoggerFilterExample.javain the log4error repo if useful. - Ensure SLF4J + a backend (Log4j2/Logback) remain configured; log4error writes through SLF4J.
- Prefer request/thread boundaries only — do not share one Logger across threads.
- Do not use Spring
@Autowired/@RequestScopeinjection ofio.github.parvez3019.Logger(annotations were removed in 0.1.0; ThreadLocal + filter is the supported path).
Migration mapping (from SLF4J / Log4j / Logback / java.util.logging)¶
| Existing call | Migrate to | When |
|---|---|---|
log.info(...) / logger.info(...) |
Logger().info(...) |
Request context useful only if something fails later |
log.debug(...) |
Logger().debug(...) |
Same as info |
log.warn(...) |
Logger().pWarn(...) |
Always emit (no buffer API for warn) |
log.error(...) |
Logger().error(...) |
Failures where prior info/debug context should flush |
log.error(...) (ops / always-on) |
Logger().pError(...) |
Must always write, even with empty buffer |
| Startup / shutdown / non-request logs | Keep existing logger or use p* methods |
Outside request scope |
Static LoggerFactory.getLogger(X) |
Keep for class-named loggers if needed; for buffered flow use filter Logger() |
Avoid mixing buffers incorrectly |
Migrating from log4error 0.0.11 → 0.1.0¶
- Bump dependency version to
0.1.0. - If you used
@Autowired/ component-scannedLogger, switch to filter + ThreadLocal. - Declare Spring Web / servlet yourself (no longer transitive).
- Copy filter from repo
examples/if you imported the old packaged example class. - Expect buffer capping on very chatty requests (default 500).
Rules while migrating¶
- Do NOT replace every log blindly.
- Buffer (
info/debug) for per-request breadcrumbs. - Use
p*for logs that must always appear (security, audit, metrics-adjacent, startup). - On catch blocks that currently only
log.error, switch toLogger().errorso buffered context flushes. - Preserve
{}placeholders and throwable-last argument style. - After migration, happy-path requests should not emit the buffered INFO/DEBUG lines.
- Update imports: remove unused
LoggerFactorywhere fully migrated; addRequestLogger(Boot) or a static import ofLogger()from the filter class. - If the app is not Spring MVC/servlet style, adapt the ThreadLocal lifecycle to the framework’s request/job boundary.
- Leave a short comment at the filter explaining why ThreadLocal must be cleared.
Deliverables¶
- Dependency change
- Starter dependency (Boot) or filter/lifecycle hook + static accessor
- Migrated call sites with the mapping above
- Brief summary of files touched and any logs intentionally left on the old logger