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

pnpm 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

vite plugin · build-time transform·DEVELOPMENT
src/checkout.tssource
1import { log } from 'evlog'
2initLogger({ env: { service: 'checkout' } })
3
4log.debug('checkout', 'building cart from session')
5log.info('checkout', 'cart built')
6log.info({ action: 'checkout', total: 99 })
7log.debug('checkout', 'starting payment')
8log.info({ action: 'paid', amount: 99 })
dist/checkout.jsawaiting build
1import { log } from 'evlog'
2initLogger({ env: { service: 'checkout' } })
3
4log.debug('checkout', 'building cart from session')
5log.info('checkout', 'cart built')
6log.info({ action: 'checkout', total: 99 })
7log.debug('checkout', 'starting payment')
8log.info({ action: 'paid', amount: 99 })
run vite build to transform
log.debug stripped
__source injected
initLogger()manual

The plugin transforms your source at build time — log.debug() calls are deleted from the output, __source: 'file:line' is injected into object-form log calls, and initLogger() is wired in via Vite's define hook so you never have to call it yourself.

Auto-initialization

The plugin injects logger configuration at compile time via Vite's define hook. The service, environment, pretty, silent, enabled, and sampling options are serialized and injected at build time, so log, createLogger(), and createRequestLogger() work immediately without an initLogger() call.

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 so you know 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
})

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.