Package com.tnsai.quality.observability
Class AgentLogger
java.lang.Object
com.tnsai.quality.observability.AgentLogger
Structured logger for agent operations with automatic trace context propagation.
AgentLogger wraps SLF4J and adds:
- Automatic trace context in MDC
- Structured logging support with key-value pairs
- Agent-specific context (agent ID, role, action)
- Operation timing and metrics
Example usage:
AgentLogger logger = AgentLogger.forAgent("my-agent");
// Simple logging with context
logger.info("Processing request")
.with("userId", "123")
.with("action", "search")
.log();
// With trace context
logger.withTrace(traceContext)
.info("Action completed")
.with("result", "success")
.with("durationMs", 150)
.log();
// Timed operation
Object result = logger.timed("database-query", () -> {
return db.query("SELECT ...");
});
// Error logging
logger.error("Operation failed", exception)
.with("action", "processPayment")
.log();
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionfinal classFluent builder for structured log entries. -
Method Summary
Modifier and TypeMethodDescriptionCreates a debug log entry.Creates an error log entry.Creates an error log entry with exception.static AgentLoggerCreates a logger for an agent.static AgentLoggerCreates a logger for an agent with a custom logger class.Creates an info log entry.voidlogActionComplete(String actionName, long durationMs, boolean success) Logs action completion.voidlogActionError(String actionName, Throwable exception) Logs action failure.voidlogActionStart(String actionName, Map<String, Object> parameters) Logs action start.voidlogLLMCall(String model, int promptTokens, long durationMs) Logs LLM call.<T> TExecutes and times an operation.voidExecutes and times a void operation.Creates a warn log entry.Creates a warn log entry with exception.Uses the current thread's trace context.withTrace(TraceContext trace) Sets the trace context for subsequent log calls.
-
Method Details
-
forAgent
Creates a logger for an agent.- Parameters:
agentId- the agent identifier- Returns:
- a new agent logger
-
forAgent
Creates a logger for an agent with a custom logger class.- Parameters:
agentId- the agent identifierloggerClass- the class for logger naming- Returns:
- a new agent logger
-
withTrace
Sets the trace context for subsequent log calls.- Parameters:
trace- the trace context- Returns:
- this logger for chaining
-
withCurrentTrace
Uses the current thread's trace context.- Returns:
- this logger for chaining
-
debug
Creates a debug log entry.- Parameters:
message- the log message- Returns:
- a log entry builder
-
info
Creates an info log entry.- Parameters:
message- the log message- Returns:
- a log entry builder
-
warn
Creates a warn log entry.- Parameters:
message- the log message- Returns:
- a log entry builder
-
warn
Creates a warn log entry with exception.- Parameters:
message- the log messagethrowable- the exception- Returns:
- a log entry builder
-
error
Creates an error log entry.- Parameters:
message- the log message- Returns:
- a log entry builder
-
error
Creates an error log entry with exception.- Parameters:
message- the log messagethrowable- the exception- Returns:
- a log entry builder
-
logActionStart
Logs action start.- Parameters:
actionName- the action nameparameters- the action parameters
-
logActionComplete
Logs action completion.- Parameters:
actionName- the action namedurationMs- the duration in millisecondssuccess- whether the action succeeded
-
logActionError
Logs action failure.- Parameters:
actionName- the action nameexception- the exception
-
logLLMCall
Logs LLM call.- Parameters:
model- the model namepromptTokens- estimated prompt tokensdurationMs- call duration
-
timed
Executes and times an operation.- Type Parameters:
T- the return type- Parameters:
operationName- the operation nameoperation- the operation to execute- Returns:
- the operation result
-
timedRun
Executes and times a void operation.- Parameters:
operationName- the operation nameoperation- the operation to execute
-