Skip to content
Logixlysia
Esc
↑↓navigate↵open⌘Jpreview
On this page

Usage

Learn how to use Logixlysia in your Elysia applications

Installation

bun add logixlysia

Elysia 2 beta

Logixlysia runs on Elysia 1.4 and on the Elysia 2 prereleases, from 2.0.0-beta.32 on. To try it on Elysia 2, install Elysia from its rc tag:

bun add logixlysia elysia@rc

What changes on Elysia 2:

  • A .ws() route needs Elysia’s WebSocket capability, .use(websocket()) from elysia/websocket, and a socket handler reads log and store from the socket itself (ws.log) rather than from ws.data.
  • The app no longer has a store property. Read store in a handler, or take pino from the plugin instance: logixlysia().store.pino.
  • The plugin’s TypeScript types still describe Elysia 1.4, so log and store are not yet part of a handler’s context type on Elysia 2. They are there at runtime.

The plugin registers an onAfterResponse hook, and on Elysia 2 any such hook changes what Elysia itself sends, with or without the plugin:

  • A generator that returns status() before its first yield answers with an empty 200 instead of that status.
  • On 2.0.0-beta.32, an sse() body is sent as plain text with no data: framing. 2.0.0-rc.3 frames it again, which is why the install above uses the rc tag.

Basic Usage

import { Elysia } from 'elysia'
import logixlysia from 'logixlysia'

const app = new Elysia()
  .use(logixlysia())
  .get('/', () => 'Hello World')
  .listen(3000)

Using the plugin in several modules

Create the plugin once and .use() that same instance in every module. If each module calls logixlysia() again, each module’s routes are still logged through its own instance, with the request’s request ID, its request-phase enrichers and its sampling, but Elysia runs only the first instance’s onRequest: a later instance’s lines are written when the handler returns rather than when a streamed body ends, and useLogger() in its routes resolves to the first instance’s logger. app.stop() flushes only the first instance’s transports.

// logging.ts
import logixlysia from 'logixlysia'

export const logging = logixlysia({ config: { requestId: true } })

// users.ts
import { Elysia } from 'elysia'
import { logging } from './logging'

export const users = new Elysia().use(logging).get('/users', () => [])

On a compiled app (Elysia’s default, aot: true) with the plugin mounted through .use(), the request boundary is app-wide: every request of the application gets an access line, routes on parent apps included. With aot: false, or with the plugin mounted inside group() or guard(), only the routes its hooks reach get an access line, so mount it on the root app. The log context is app-wide too, so an app that mounts the plugin through a shared setup plugin gets log in its handlers. A route on a module that mounts its own instance gets that instance’s log; any other route gets the log of the first instance mounted.

Request context

Accumulate fields during a request; they are merged into the automatic access log, and into any custom log line the handler writes after them:

.get('/users/:id', ({ request, store, params }) => {
  store.logger.mergeContext(request, { userId: params.id })
  return { ok: true }
})

See Request context for details.

Presets

logixlysia({ preset: 'dev' }) // pretty console + banner
logixlysia({ preset: 'prod' }) // one JSON line per record + autoRedact + request IDs
logixlysia({ preset: 'json' }) // one JSON line per record

Explicit config overrides preset defaults. Details: Presets.

Configuration

Basic Options

logixlysia({
  config: {
    showStartupMessage: true,
    startupMessageFormat: 'simple', // or 'banner'
    autoRedact: false, // Set to true to auto-redact sensitive PII (emails, IPs, credit cards, JWTs)
    logQueryParams: true,
    logFilePath: './logs/app.log'
  }
})

Custom Log Format

Customize log messages using placeholders:

logixlysia({
  config: {
    customLogFormat: '{now} {level} {duration} {method} {pathname} {status}'
  }
})

Available placeholders:

Placeholder Description Example
{now} Current timestamp; HH:MM:ss.SSS on a TTY with the default format 2025-12-21T10:00:00.000Z
{level} Log level (DEBUG, INFO, WARNING, ERROR) INFO
{duration} Request duration (formatted) 12ms, 1.5s
{method} HTTP method GET
{pathname} Request path (alias: {path}); includes query if logQueryParams: true /users
{query} Raw query string, printed whatever logQueryParams says. With logQueryParams: true, {pathname} already includes it, so use one or the other ?id=123
{status} Response status code 200
{statusText} HTTP status text Not Found
{message} Custom message User profile accessed
{icon} Logixlysia fox 🦊 in a level-colored chip when colors + TTY, the level word otherwise INFO
{speed} Slow-request badge when duration ≥ verySlowThreshold ⚡ slow
{service} Service prefix from config.service [my-api]
{context} Context JSON on main line when tree is off / empty {"id":1}
{ip} Client IP address 127.0.0.1
{epoch} Unix time in milliseconds 1766311200000
{requestId} Request ID when requestId is on 9f1c…

Log Filtering

Filter logs by level:

logixlysia({
  config: {
    logFilter: {
      level: ['ERROR', 'WARNING']
    }
  }
})

Logixlysia keeps only the listed levels and drops the rest.

Pino Integration

Logixlysia writes its own lines and exposes a pino instance as store.pino for your application’s structured logging:

app.get('/users/:id', ({ store, params }) => {
  const { pino } = store

  pino.info(
    {
      userId: params.id,
      action: 'view_profile'
    },
    'User profile accessed'
  )

  return { user: 'data' }
})

Configure Pino options:

logixlysia({
  config: {
    pino: {
      level: 'debug',
      prettyPrint: true,
      redact: ['password', 'token'],
      base: { service: 'my-api' }
    }
  }
})

Learn more in the Pino Integration guide.

File Logging

Save logs to files:

logixlysia({
  config: {
    logFilePath: './logs/app.log',
    logRotation: {
      maxSize: '10m',
      interval: '1d',
      maxFiles: '7d',
      compress: true
    }
  }
})

See File Logging and Log Rotation for details.

Output Control

Control where logs are sent:

logixlysia({
  config: {
    // Disable console output
    disableInternalLogger: false,

    // Disable file output
    disableFileLogging: false,

    // Use only transports (disable console and file)
    useTransportsOnly: false,

    // Custom transports
    transports: [customTransport]
  }
})

Error Handling

Logixlysia automatically captures and logs errors:

app.get('/error', () => {
  throw new Error('Something went wrong!')
})

Logixlysia logs the error automatically with the request method, path, status, and error message. For an HttpError, it also logs code, why, fix, and link. The text console line does not print a stack trace. With the context tree on, which is the default, it can show where a server error was thrown, as error.at. Transports and the JSON console line (consoleFormat: 'json') receive a thrown Error’s full stack, unless that error’s own status is below 500. A validation error is logged without a stack while logErrorPayload is off, which is the default.

Examples

Production Setup

const app = new Elysia()
  .use(
    logixlysia({
      config: {
        logFilePath: './logs/production.log',
        logRotation: {
          maxSize: '100m',
          interval: '1d',
          maxFiles: '30d',
          compress: true
        },
        logFilter: {
          level: ['ERROR', 'WARNING']
        },
        autoRedact: true,
        redactKeys: ['customerSsn']
      }
    })
  )
  .listen(3000)

Development Setup

const app = new Elysia()
  .use(
    logixlysia({
      config: {
        showStartupMessage: true,
        startupMessageFormat: 'banner',
        pino: {
          level: 'debug',
          prettyPrint: true
        }
      }
    })
  )
  .listen(3000)

Was this page helpful?