Wide Events Support #152
-
|
I didn't see any mention of "wide events" in the LogTape docs. I'm curious what the community proposes for using this style of logging with logtape? evlog is another logging library I came across that had wide events front and center, but I really like what I've seen from LogTape, especially its answers for challenging environments like monorepos. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
|
Thanks for raising this. Wide events as a deliberate pattern aren't mentioned anywhere in the LogTape docs, and there's no dedicated API for them today. I think it's fair to say LogTape can express wide events, but doesn't support them natively in the way evlog does. Today the closest fit is a combination of pieces.
let log = getLogger(["api", "checkout"]).with({ requestId });
log = log.with({ user: { id: 1, plan: "pro" } });
log = log.with({ cart: { items: 3, total: 9999 } });
log.info("Checkout completed");
await withContext({ requestId }, async () => {
await handler(); // any logger.x() call inside picks up requestId
});Both of these merge into every log record inside their scope, which isn't quite the wide-events shape (one record per request). The trick for that is to keep a plain object as the accumulator and let handlers mutate it, then emit once at the end: const event: Record<string, unknown> = {};
await withContext({ event }, async () => {
await handler(); // can read the ambient event and set event.user, etc.
});
logger.info("HTTP request", event);The logger stays immutable. Only the payload object is mutable, and it lives just for the request. The HTTP framework integrations (Express, Hono, Koa, Fastify) already emit one record per request, but they only carry the metadata available at the middleware layer (method, status, duration, etc.). They don't yet expose a hook for handlers to enrich that record with business data, which is the part that wide events really want. My current answer is: it works, but having to create the accumulator by hand, and having no clean way to feed business data into the framework integrations' per-request record, is friction I don't want to leave there. I plan to add a built-in way to do this in a future LogTape version. I don't want to commit to the exact shape yet, I have a few sketches and want to sit with the tradeoffs. Treat this as a yes on the direction. If you had an API shape in mind, please sketch it here. |
Beta Was this translation helpful? Give feedback.
Thanks for raising this. Wide events as a deliberate pattern aren't mentioned anywhere in the LogTape docs, and there's no dedicated API for them today. I think it's fair to say LogTape can express wide events, but doesn't support them natively in the way evlog does.
Today the closest fit is a combination of pieces.
Logger.with({...})returns a child logger with bound properties. It's immutable, so accumulating fields means reassigning:withContext()does the same thing across async boundaries via