Skip to content

feat(logging): attach structured JSON logging with sessionId to root logger#63

Merged
lliquid merged 1 commit into
mainfrom
improve_logging
May 7, 2026
Merged

feat(logging): attach structured JSON logging with sessionId to root logger#63
lliquid merged 1 commit into
mainfrom
improve_logging

Conversation

@luyuzhe111
Copy link
Copy Markdown
Contributor

Description of changes:

Problem

When running RL rollouts on ACR, logs from user code (rl_app.py, reward.py) and third-party libraries (migration_bench, httpx) were impossible to correlate in CloudWatch. The upstream SDK's RequestContextFormatter only attaches to the bedrock_agentcore.app logger — every other logger in the process used the default text formatter with no sessionId, making it impossible to filter all logs for a single rollout session.

Solution

  • Add CorrelatedFormatter that emits structured JSON with sessionId and requestId pulled from BedrockAgentCoreContext (the public API)
  • Attach it to the root logger in AgentCoreRLApp.__init__ via configure_logging() — every logger in the process inherits it automatically
  • Users don't need to change anything beyond logging.getLogger(__name__) to get full session correlation

Changes

  • src/agentcore_rl_toolkit/logging.py — new module with CorrelatedFormatter and configure_logging()
  • src/agentcore_rl_toolkit/app.py — call configure_logging() in __init__, switch bare logging.info() to named logger
  • Examples — print()logger.info() in reward functions for structured output
  • Honors user-specified log level (e.g., logging.basicConfig(level=DEBUG)) via min() logic

Test plan

  • Unit tests (tests/test_logging.py) — formatter output, context injection, idempotency
  • Local e2e — started server, submitted request, verified JSON output with requestId (migration agent)
  • ACR e2e — deployed to AgentCore, ran evaluate_async.py --limit 1, confirmed sessionId appears in CloudWatch for all loggers (reward, root, __main__, agentcore_rl_toolkit.app) (migration agent)

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@luyuzhe111 luyuzhe111 requested review from lliquid and lyzustc May 6, 2026 22:44
@lliquid lliquid merged commit f8304bf into main May 7, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants