fastify-xlogger
logEvent(params)
Log a structured business event with canonical schema on the fastify.xlogger decorator.
logEvent(params)
Logs a structured business event using the canonical schema. Automatically adds request context (requestId, userId, orgId, route) when a request is provided.
Signature
fastify.xlogger.logEvent(params: {
event: string
msg?: string
level?: "fatal" | "error" | "warn" | "info" | "debug" | "trace"
data?: Record<string, unknown>
request?: FastifyRequest
}): void
Params
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
event | string | Yes | — | Event name in dot-notation (e.g. "user.created", "payment.completed") |
msg | string | No | event value | Human-readable log message |
level | string | No | "info" | Pino log level |
data | object | No | {} | Additional fields to merge into the log entry |
request | FastifyRequest | No | null | When provided, request context (requestId, userId, orgId, traceId) is merged in |
Returns
void
Throws
Error: [xLogger] logEvent requires a string 'event' parameter—eventis missing or not a string
Examples
Basic business event
fastify.post("/users", async (request) => {
const user = await createUser(request.body);
fastify.xlogger.logEvent({
event: "user.created",
data: { userId: user.id, plan: user.plan },
request,
});
return user;
});
Error event with explicit level
fastify.post("/payments", async (request) => {
try {
return await processPayment(request.body);
} catch (err) {
fastify.xlogger.logEvent({
event: "payment.failed",
level: "error",
msg: "Payment processing failed",
data: { orderId: request.body.orderId, reason: err.message },
request,
});
throw err;
}
});
See also
- logBoundary(params) — log external API calls
- createJobContext(params) — log background job lifecycle
AI Context
package: "@xenterprises/fastify-xlogger"
method: fastify.xlogger.logEvent(params)
use-when: Log a structured business event (e.g. user.created, payment.completed) with canonical schema and optional request context
params: event (required), msg, level, data, request
returns: void
