Class PromptLogger

java.lang.Object
com.tnsai.llm.observability.PromptLogger
All Implemented Interfaces:
LLMObserver

public class PromptLogger extends Object implements 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 ID
  • llm.provider - Provider name (OpenAI, Anthropic, etc.)
  • llm.model - Model name
  • llm.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:
  • Method Details

    • builder

      public static PromptLogger.Builder builder()
    • withPIIFiltering

      public static PromptLogger withPIIFiltering()
      Creates a default logger with PII filtering enabled.
    • withoutFiltering

      public static PromptLogger 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: LLMObserver
      Called before sending a request to the LLM.
      Specified by:
      onRequest in interface LLMObserver
      Parameters:
      client - The LLM client being used
      message - The user message
      systemPrompt - Optional system prompt
      history - Optional conversation history
      tools - Optional tools
    • onResponse

      public void onResponse(com.tnsai.llm.LLMClient client, com.tnsai.llm.ChatResponse response, long latencyMs)
      Description copied from interface: LLMObserver
      Called after receiving a successful response.
      Specified by:
      onResponse in interface LLMObserver
      Parameters:
      client - The LLM client that was used
      response - The response received
      latencyMs - Time taken in milliseconds
    • onError

      public void onError(com.tnsai.llm.LLMClient client, Exception error, long latencyMs)
      Description copied from interface: LLMObserver
      Called when an error occurs.
      Specified by:
      onError in interface LLMObserver
      Parameters:
      client - The LLM client that was used
      error - The exception that occurred
      latencyMs - Time taken before error in milliseconds
    • onStreamComplete

      public void onStreamComplete(com.tnsai.llm.LLMClient client, int totalChunks, long latencyMs)
      Description copied from interface: LLMObserver
      Called when streaming completes.
      Specified by:
      onStreamComplete in interface LLMObserver
      Parameters:
      client - The LLM client that was used
      totalChunks - Total number of chunks received
      latencyMs - Total time taken in milliseconds