fastify-xlogger
createBoundaryLogger(vendor, operation, request?)
Create a timed boundary logger that automatically captures call duration.
createBoundaryLogger(vendor, operation, request?)
Creates a stateful boundary logger that starts a timer at call time and captures elapsed duration when success() or fail() is called. Prefer this over calling logBoundary directly when you want automatic duration tracking.
Signature
fastify.xlogger.createBoundaryLogger(
vendor: string,
operation: string,
request?: FastifyRequest
): {
retry(): void
success(params?: Partial<BoundaryParams>): void
fail(err: Error, params?: Partial<BoundaryParams>): void
}
Params
| Name | Type | Required | Description |
|---|---|---|---|
vendor | string | Yes | External service name (e.g. "twilio", "hubspot") |
operation | string | Yes | Operation name (e.g. "sendSMS", "createContact") |
request | FastifyRequest | No | Request to include context from |
Returns
An object with three methods:
| Method | Description |
|---|---|
retry() | Increment the internal retry counter |
success(params?) | Log a successful call; merges elapsed duration and retry count automatically |
fail(err, params?) | Log a failed call; merges elapsed duration, retry count, and the error |
Throws
Error: [xLogger] createBoundaryLogger requires a string 'vendor' parameterError: [xLogger] createBoundaryLogger requires a string 'operation' parameter
Examples
Wrap an external API call
fastify.post("/sms", async (request) => {
const bl = fastify.xlogger.createBoundaryLogger("twilio", "sendSMS", request);
try {
const result = await fastify.xTwilio.sms.send(request.body.to, request.body.text);
bl.success({ externalId: result.sid });
return { sid: result.sid };
} catch (err) {
bl.fail(err);
throw err;
}
});
With retry tracking
const bl = fastify.xlogger.createBoundaryLogger("openai", "createEmbedding", request);
let lastErr;
for (let i = 0; i < 3; i++) {
try {
const result = await openai.embeddings.create({ input: text, model: "text-embedding-3-small" });
bl.success({ metadata: { tokens: result.usage.total_tokens } });
return result;
} catch (err) {
lastErr = err;
bl.retry();
await new Promise((r) => setTimeout(r, 200 * (i + 1)));
}
}
bl.fail(lastErr);
throw lastErr;
See also
- logBoundary(params) — fire-and-forget boundary log (bring your own timing)
- logEvent(params) — log application events
AI Context
package: "@xenterprises/fastify-xlogger"
method: fastify.xlogger.createBoundaryLogger(vendor, operation, request?)
use-when: Wrap an external API call with automatic duration tracking — call success() or fail() when done
returns: { retry(), success(params?), fail(err, params?) }
