Package com.tnsai.llm.observability
Class PromptLogger
java.lang.Object
com.tnsai.llm.observability.PromptLogger
- All Implemented Interfaces:
LLMObserver
Logs LLM prompts and responses with PII filtering support.
PromptLogger provides structured logging for all LLM interactions, with optional PII (Personally Identifiable Information) filtering to protect sensitive data in logs.
PII Filtering
The logger can automatically redact common PII patterns:
- Email addresses
- Phone numbers
- Credit card numbers
- Social Security Numbers
- API keys and tokens
Usage Example
// Create logger with PII filtering
PromptLogger promptLogger = PromptLogger.builder()
.filterPII(true)
.logLevel(PromptLogger.LogLevel.INFO)
.maxContentLength(500)
.build();
// Wrap client
LLMClient client = new ObservableLLMClient(baseClient, promptLogger);
// Logs are automatically filtered
client.chat("My email is john@example.com");
// Logged as: "My email is [REDACTED_EMAIL]"
Content uses com.tnsai.diagnostics.content.PromptLogger.
Applications must isolate this opt-in diagnostic namespace from operational
appenders; it is not a durable conversation transcript.
MDC (Mapped Diagnostic Context)
The logger adds useful context to MDC for correlation:
llm.requestId- Unique request IDllm.provider- Provider name (OpenAI, Anthropic, etc.)llm.model- Model namellm.latencyMs- Request latency
Log Format
[INFO] LLM Request [req-abc123] OpenAI/gpt-4o: "What is 2+2?" [INFO] LLM Response [req-abc123] OpenAI/gpt-4o (245ms): "2+2 equals 4"
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classBuilder for PromptLogger.static enum -
Field Summary
Fields inherited from interface com.tnsai.llm.observability.LLMObserver
NOOP -
Method Summary
Modifier and TypeMethodDescriptionstatic PromptLogger.Builderbuilder()voidCalled when an error occurs.voidonRequest(com.tnsai.llm.LLMClient client, String message, Optional<String> systemPrompt, Optional<List<Map<String, Object>>> history, Optional<List<Map<String, Object>>> tools) Called before sending a request to the LLM.voidonResponse(com.tnsai.llm.LLMClient client, com.tnsai.llm.ChatResponse response, long latencyMs) Called after receiving a successful response.voidonStreamComplete(com.tnsai.llm.LLMClient client, int totalChunks, long latencyMs) Called when streaming completes.static PromptLoggerCreates a logger without PII filtering.static PromptLoggerCreates a default logger with PII filtering enabled.Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.tnsai.llm.observability.LLMObserver
onStreamChunk, onStreamError
-
Method Details
-
builder
-
withPIIFiltering
Creates a default logger with PII filtering enabled. -
withoutFiltering
Creates a logger without PII filtering. -
onRequest
public void onRequest(com.tnsai.llm.LLMClient client, String message, Optional<String> systemPrompt, Optional<List<Map<String, Object>>> history, Optional<List<Map<String, Object>>> tools) Description copied from interface:LLMObserverCalled before sending a request to the LLM.- Specified by:
onRequestin interfaceLLMObserver- Parameters:
client- The LLM client being usedmessage- The user messagesystemPrompt- Optional system prompthistory- Optional conversation historytools- Optional tools
-
onResponse
public void onResponse(com.tnsai.llm.LLMClient client, com.tnsai.llm.ChatResponse response, long latencyMs) Description copied from interface:LLMObserverCalled after receiving a successful response.- Specified by:
onResponsein interfaceLLMObserver- Parameters:
client- The LLM client that was usedresponse- The response receivedlatencyMs- Time taken in milliseconds
-
onError
Description copied from interface:LLMObserverCalled when an error occurs.- Specified by:
onErrorin interfaceLLMObserver- Parameters:
client- The LLM client that was usederror- The exception that occurredlatencyMs- Time taken before error in milliseconds
-
onStreamComplete
public void onStreamComplete(com.tnsai.llm.LLMClient client, int totalChunks, long latencyMs) Description copied from interface:LLMObserverCalled when streaming completes.- Specified by:
onStreamCompletein interfaceLLMObserver- Parameters:
client- The LLM client that was usedtotalChunks- Total number of chunks receivedlatencyMs- Total time taken in milliseconds
-