Skip to content

Bun / Node.js

Add logging to Bun or Node.js apps

Terminal window
npm install github:karloscodes/lognorth-sdk-ts
import LogNorth from '@karloscodes/lognorth-sdk'
LogNorth.config('https://logs.yoursite.com', 'your-api-key')
LogNorth.log('User signed up', { user_id: 123 })
LogNorth.error('Checkout failed', err, { order_id: 42 })
// Express
import { middleware } from '@karloscodes/lognorth-sdk/express'
app.use(middleware())
// Hono
import { middleware } from '@karloscodes/lognorth-sdk/hono'
app.use(middleware())
// Next.js
import { withLogger } from '@karloscodes/lognorth-sdk/next'
export const GET = withLogger()(handler)

The middleware automatically:

  • Generates a trace_id per request (or uses X-Trace-ID from the incoming header)
  • Sets X-Trace-ID on the response
  • Logs the request with duration_ms and trace_id as top-level fields
  • All LogNorth.log() and LogNorth.error() calls within the request automatically inherit the same trace_id
app.use(middleware())
app.post('/users', (req, res) => {
// This log automatically gets the same trace_id as the middleware request log
LogNorth.log('User signed up', { user_id: 123 })
res.status(201).json({ ok: true })
})

Skip health checks and other noisy endpoints:

app.use(middleware({
ignorePaths: ['/healthz', '/_health', '/metrics']
}))

These paths won’t be logged by the middleware.

Keep your existing logger, add LogNorth as a transport:

import pino from 'pino'
import { transport } from '@karloscodes/lognorth-sdk/pino'
LogNorth.config('https://logs.yoursite.com', 'your-api-key')
const logger = pino({ level: 'info' }, transport())
logger.info({ user_id: 123 }, 'User signed up')

Pass your logger to middleware:

app.use(middleware(logger))
MethodBehavior
LogNorth.log()Batched (10 events or 5s)
LogNorth.error()Sent immediately with stack trace
MiddlewareLogs requests with trace_id + duration_ms
ShutdownAuto-flush on SIGINT/SIGTERM

LogNorth.error() automatically captures the error class, file, line, caller function, and stack trace. These are used for three-tier issue grouping.