Core Concepts

Vite Plugin

Source Code
Build-time optimizations for any Vite-based framework — auto-init, debug stripping, source location injection, and optional auto-imports.

The evlog/vite plugin adds build-time DX features to any Vite-based project. It works with SvelteKit, Hono, Express, Fastify, Elysia, and any framework using Vite as its build tool.

Nuxt users: These features are already integrated into the evlog/nuxt module via strip and sourceLocation options. You don't need to install the Vite plugin separately.

Quick Start

1. Install

bun add evlog

2. Add to vite.config.ts

vite.config.ts
import { defineConfig } from 'vite'
import evlog from 'evlog/vite'

export default defineConfig({
  plugins: [
    evlog({
      service: 'my-api',
      environment: 'production',
    }),
  ],
})

That's it. The plugin automatically:

  • Initializes the logger at compile time (no initLogger() call needed)
  • Strips log.debug() calls from production builds

Features

Auto-initialization

The plugin injects logger configuration at compile time via Vite's define hook. Your code can use log, createLogger(), and createRequestLogger() immediately — no initLogger() call required.

// Before (manual setup)
import { initLogger, createLogger } from 'evlog'
initLogger({ env: { service: 'my-api' } })
const log = createLogger()

// After (with Vite plugin)
import { createLogger } from 'evlog'
const log = createLogger()

The service, environment, pretty, silent, enabled, and sampling options are serialized and injected at build time.

Debug stripping

By default, all log.debug() calls are removed from production builds. This is a compile-time transformation — the calls are completely eliminated from the output, not just silenced.

vite.config.ts
evlog({
  service: 'my-api',
  // Default: strip debug logs in production builds
  // strip: ['debug'],

  // Strip debug and info in production:
  // strip: ['debug', 'info'],

  // Disable stripping:
  // strip: [],
})

Stripping only activates during vite build (not vite dev).

Source location injection

When enabled, the plugin injects __source: 'file:line' into object-form log calls. This tells you exactly which file and line produced each log entry.

vite.config.ts
evlog({
  service: 'my-api',
  sourceLocation: true,      // Always inject
  // sourceLocation: 'dev',  // Only in development
})

Before transform:

log.info({ action: 'checkout', total: 99 })

After transform:

log.info({ action: 'checkout', total: 99, __source: 'src/checkout.ts:42' })

Auto-imports (opt-in)

Automatically detect and import evlog symbols (log, createEvlogError, parseError, etc.) without manual import statements. Disabled by default.

vite.config.ts
evlog({
  service: 'my-api',
  autoImports: true,
})

When enabled, the plugin:

  1. Scans your code for evlog symbols
  2. Adds the correct import statements automatically
  3. Generates a .d.ts file for TypeScript support
The auto-imported error constructor is createEvlogError, not createError. This avoids conflicts with framework-native createError (Nuxt, Nitro, h3). The standalone createError from evlog is still available via explicit import.

Client-side injection

When the client option is provided, the plugin injects a <script> tag into HTML pages that initializes the client-side logger. This enables log.info(), log.error(), etc. in browser code.

vite.config.ts
evlog({
  service: 'my-api',
  client: {
    console: false,
    transport: {
      enabled: true,
      endpoint: '/api/_evlog/ingest',
    },
  },
})

Configuration

OptionTypeDefaultDescription
servicestring'app'Service name in logs
environmentstringAuto-detectedEnvironment name
prettybooleantrue in devPretty print logs
silentbooleanfalseSuppress console output
enabledbooleantrueEnable/disable all logging
stripLogLevel[]['debug']Log levels to remove from production builds
sourceLocationboolean | 'dev'falseInject source file:line into log calls
autoImportsbooleanfalseAuto-import evlog symbols
clientobjectClient-side injection config (console, transport)
samplingobjectHead/tail sampling rates

Nuxt Integration

The Nuxt module exposes strip and sourceLocation directly in nuxt.config.ts:

nuxt.config.ts
export default defineNuxtConfig({
  modules: ['evlog/nuxt'],
  evlog: {
    env: { service: 'my-app' },
    strip: ['debug'],           // Default
    sourceLocation: 'dev',      // Inject in dev only
  },
})

Vite Compatibility

The plugin supports Vite 7+ and is optimized for Vite 8 (Rolldown). On Vite 8, transform hooks use Rolldown-native filter and moduleType for maximum performance — non-matching files are skipped entirely on the Rust side without crossing the JS bridge.