PailFramework MiddlewareElysia

Elysia

Request-scoped wide event logging plugin for Elysia

Last updated:

Elysia Plugin

The Elysia adapter uses derive to inject a request-scoped Wide Event into every handler's context. The event auto-emits on response or error.

Setup

import { Elysia } from "elysia";
import { createPail } from "@visulima/pail";
import { pailPlugin } from "@visulima/pail/middleware/elysia";

const logger = createPail();
const app = new Elysia();

pailPlugin(app, { pail: logger });

app.listen(3000);

Accessing the Logger

Via context.log (auto-injected)

The log property is injected into every handler context via Elysia's derive:

app.get("/api/users", ({ log }) => {
    log.set({ user: { id: 1 } });
    log.info("Fetched user list");
    return { ok: true };
});

Via useLogger()

Access the logger from anywhere in the async call stack using AsyncLocalStorage:

import { useLogger } from "@visulima/pail/middleware/elysia";

async function fetchUser(id: number) {
    const log = useLogger();
    log.set({ user: { id } });
    log.info("Querying database");
    // ...
}

How It Works

The plugin registers three Elysia hooks (all with as: "global"):

  1. derive — Creates a WideEvent, stores it in AsyncLocalStorage via enterWith(), and returns { log: logger } to inject into context
  2. onAfterHandle — Emits the event with the response status (defaults to 200)
  3. onError — Emits the event with the error

A WeakSet tracks emitted requests to prevent double-emission if both hooks fire.

Error Handling

Errors are automatically captured by the onError hook:

app.get("/api/fail", ({ log }) => {
    log.set({ user: { id: 1 } });
    throw new Error("Something went wrong");
    // Wide event emits with error via onError hook
});

Route Configuration

pailPlugin(app, {
    pail: logger,
    service: "api-gateway",
    exclude: ["/health", "/metrics"],
    include: ["/api/**"],
    routes: {
        "/api/auth/**": { service: "auth-service" },
    },
});

TypeScript

import type { PailElysiaInstance } from "@visulima/pail/middleware/elysia";

Exported Types

ExportDescription
pailPluginRegister the plugin on an Elysia instance
useLoggerRetrieve logger from AsyncLocalStorage
PailElysiaInstanceElysia instance type with derive/hook API
ElysiaPluginOptionsOptions type for pailPlugin()
WideEventRe-exported WideEvent class
Support

Contribute to our work and keep us going

Community is the heart of open source. The success of our packages wouldn't be possible without the incredible contributions of users, testers, and developers who collaborate with us every day.Want to get involved? Here are some tips on how you can make a meaningful impact on our open source projects.

Ready to help us out?

Be sure to check out the package's contribution guidelines first. They'll walk you through the process on how to properly submit an issue or pull request to our repositories.

Submit a pull request

Found something to improve? Fork the repo, make your changes, and open a PR. We review every contribution and provide feedback to help you get merged.

Good first issues

Simple issues suited for people new to open source development, and often a good place to start working on a package.
View good first issues