Slf4jCacheListener

class Slf4jCacheListener @JvmOverloads constructor(logger: Logger = LoggerFactory.getLogger("dev.kmemo.cache"), level: Level = Level.DEBUG, redactPrompts: Boolean = true, maxPromptChars: Int = DEFAULT_MAX_PROMPT_CHARS, correlationId: () -> String? = { null }) : CacheListener

A CacheListener that writes one structured SLF4J line per cache event.

val cache = SemanticCache(embedder, listeners = listOf(Slf4jCacheListener()))

Each event becomes a log record with key/value fields — scope, similarity, reason, guard, the stage latencies — so a structured backend (Logback with a JSON encoder, ELK, Loki) can index them and a plain console still prints them readably. Everything logs at a single configurable level (default DEBUG, since hits and misses are per-request and noisy), and the whole method short-circuits when that level is disabled, so a listener left on in production costs almost nothing.

Prompt redaction is on by default

Prompts are user input and routinely carry PII, so by default the prompt text is never logged — only its length, as promptChars. That keeps the line useful for spotting a hot-scope or a latency spike without turning your log store into a copy of every question your users asked. Set redactPrompts to false in a development or trusted environment to log the (length-capped) text under the prompt field.

Correlation

Pass correlationId to stamp each line with a request/trace id — for example { MDC.get("traceId") } to pull whatever your web framework already put in the SLF4J MDC. It is read once per event and, when non-null, added as a correlationId field. (MDC values your logging config already renders are not duplicated here.)

Thread-safe as long as the logger is, which every SLF4J logger is.

Parameters

logger

the SLF4J logger to write to; defaults to one named dev.kmemo.cache.

level

the level every event is logged at.

redactPrompts

whether to withhold prompt text (on by default); false logs it, length-capped.

maxPromptChars

when prompts are not redacted, the longest prefix logged before truncation.

correlationId

supplies a per-event correlation id, or null for none.

Constructors

Link copied to clipboard
constructor(logger: Logger = LoggerFactory.getLogger("dev.kmemo.cache"), level: Level = Level.DEBUG, redactPrompts: Boolean = true, maxPromptChars: Int = DEFAULT_MAX_PROMPT_CHARS, correlationId: () -> String? = { null })

Types

Link copied to clipboard
object Companion

Functions

Link copied to clipboard
open override fun onEvent(event: CacheEvent)