Skip to content

feat: add per-job context to global Pino logger#1338

Open
karan-dhir wants to merge 1 commit intolivekit:mainfrom
karan-dhir:karan-dhir/per-job-log-context
Open

feat: add per-job context to global Pino logger#1338
karan-dhir wants to merge 1 commit intolivekit:mainfrom
karan-dhir:karan-dhir/per-job-log-context

Conversation

@karan-dhir
Copy link
Copy Markdown

Summary

Closes #1336.

Sets jobId and roomName on the global Pino logger when a job starts, so all SDK-internal logs (TTS/STT metrics, speech events, AgentSession lifecycle) include per-job fields on every log line. Context is cleared after shutdown callbacks complete to prevent stale fields leaking to the next job.

This enables filtering all logs for a single call in log aggregation tools:

-- NewRelic
SELECT * FROM Log WHERE jobId = 'AJ_xxx' SINCE 1 hour ago

-- Datadog
jobId:AJ_xxx

-- Grafana/Loki
{jobId="AJ_xxx"}

Changes

  • agents/src/log.ts: Add setJobContext() — creates a Pino child logger with the provided fields, storing the base logger so it can be restored. Also updates enableOtelLogging() to keep the base logger in sync.
  • agents/src/job.ts: Call setJobContext({ jobId, roomName }) in the JobContext constructor.
  • agents/src/ipc/job_proc_lazy_main.ts: Call setJobContext({}) after shutdown callbacks to clear context before the next job.

Why this is safe

LiveKit workers process one job at a time (single-threaded, sequential dispatch), so mutating the global logger has no concurrency risk.

Testing

Verified locally that Pino child logger correctly adds fields to all subsequent log() calls and that restoring the base logger cleanly removes them:

BEFORE: {"msg":"TTS metrics"}
AFTER:  {"jobId":"AJ_xxx","roomName":"room_abc","msg":"TTS metrics"}
CLEAR:  {"msg":"next job log"}  // no stale fields

@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Apr 28, 2026

🦋 Changeset detected

Latest commit: 2b543e4

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 26 packages
Name Type
@livekit/agents Major
@livekit/agents-plugin-anam Major
@livekit/agents-plugin-assemblyai Major
@livekit/agents-plugin-baseten Major
@livekit/agents-plugin-bey Major
@livekit/agents-plugin-cartesia Major
@livekit/agents-plugin-cerebras Major
@livekit/agents-plugin-deepgram Major
@livekit/agents-plugin-elevenlabs Major
@livekit/agents-plugin-google Major
@livekit/agents-plugin-hedra Major
@livekit/agents-plugin-inworld Major
@livekit/agents-plugin-lemonslice Major
@livekit/agents-plugin-livekit Major
@livekit/agents-plugin-mistral Major
@livekit/agents-plugin-neuphonic Major
@livekit/agents-plugin-openai Major
@livekit/agents-plugin-phonic Major
@livekit/agents-plugin-resemble Major
@livekit/agents-plugin-rime Major
@livekit/agents-plugin-runway Major
@livekit/agents-plugin-sarvam Major
@livekit/agents-plugin-silero Major
@livekit/agents-plugins-test Major
@livekit/agents-plugin-trugen Major
@livekit/agents-plugin-xai Major

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@CLAassistant
Copy link
Copy Markdown

CLAassistant commented Apr 28, 2026

CLA assistant check
All committers have signed the CLA.

devin-ai-integration[bot]

This comment was marked as resolved.

Set jobId and roomName on the global logger when a job starts so that
all SDK-internal logs (TTS/STT metrics, speech events, AgentSession
lifecycle) are filterable by job in log aggregation tools.

The job context is stored separately so enableOtelLogging() can
re-apply it when reconfiguring the logger mid-job.

Context is cleared after shutdown callbacks complete to prevent stale
fields leaking to the next job on the same worker.
@karan-dhir karan-dhir force-pushed the karan-dhir/per-job-log-context branch from 2f1f1dc to 2b543e4 Compare April 28, 2026 20:15
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.

Add per-job context to the global Pino logger for session-level log filtering

2 participants