X Enterprises
fastify-xlogger

createJobContext(params)

Create a correlated child logger for background jobs with start, complete, and fail helpers.

createJobContext(params)

Creates a Pino child logger bound to a correlation ID and job name. Provides start(), complete(), and fail() helpers that emit structured job.started, job.completed, and job.failed events. Use this to correlate background job logs back to the originating request.

Signature

fastify.xlogger.createJobContext(params: {
  jobName: string
  requestId?: string
  orgId?: string
  userId?: string
  correlationId?: string
}): {
  context: object
  log: pino.Logger
  start(data?: object): void
  complete(data?: object): void
  fail(err: Error, data?: object): void
}

Params

NameTypeRequiredDefaultDescription
jobNamestringYesJob name included on every log entry
requestIdstringNoOriginal request ID to correlate back to the HTTP request
orgIdstringNoOrganization ID
userIdstringNoUser ID
correlationIdstringNoAuto-generatedStable ID for this job run; generated as job_{timestamp}_{random} if not provided

Returns

PropertyTypeDescription
contextobjectThe correlation context object (correlationId, jobName, etc.)
logpino.LoggerChild logger with all context fields bound
start(data?)functionEmits job.started at info level
complete(data?)functionEmits job.completed at info level
fail(err, data?)functionEmits job.failed at error level with the error

Throws

  • Error: [xLogger] createJobContext requires a string 'jobName' parameter

Examples

Basic background job with lifecycle logging

fastify.post("/emails/send-digest", async (request) => {
  const job = fastify.xlogger.createJobContext({
    jobName: "sendDigestEmail",
    requestId: request.id,
    userId: request.user.id,
  });

  // Hand off to background processing
  setImmediate(async () => {
    job.start({ recipientCount: users.length });

    try {
      await sendDigestEmails(users);
      job.complete({ sent: users.length });
    } catch (err) {
      job.fail(err, { failedAt: "sendDigestEmails" });
    }
  });

  return { queued: true };
});

Correlate with a queue worker

async function processQueueMessage(message) {
  const job = fastify.xlogger.createJobContext({
    jobName: "processOrder",
    requestId: message.headers["x-request-id"],
    orgId: message.body.orgId,
    correlationId: message.headers["x-correlation-id"],
  });

  job.start({ orderId: message.body.orderId });

  try {
    await fulfillOrder(message.body.orderId);
    job.complete({ orderId: message.body.orderId });
  } catch (err) {
    job.fail(err);
  }
}

See also

AI Context

package: "@xenterprises/fastify-xlogger"
method: fastify.xlogger.createJobContext(params)
use-when: Structured logging for background jobs with start/complete/fail lifecycle events and correlation back to the originating request
params: jobName (required), requestId, orgId, userId, correlationId
returns: { context, log, start(data?), complete(data?), fail(err, data?) }
Copyright © 2026