npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@mihari/logger-nuxtjs

v0.1.2

Published

Open-source log collection module for Nuxt 3 apps

Readme

@mihari/logger-nuxtjs

Open-source log collection module for Nuxt 3 applications. Captures server-side request logs, client-side errors, and provides a composable logger for structured log ingestion.

Features

  • Auto-imported useLogger() composable for structured logging in components and setup functions
  • Server middleware for automatic HTTP request/response logging via Nitro
  • Client error capture with Vue error handler and unhandled error hooks
  • Batched transport with configurable batch size, flush interval, and exponential-backoff retry
  • Secure server-side proxy for client logs — the token never leaves the server
  • Auto-enrichment of log entries with server metadata (hostname, pid) and client metadata (userAgent, route)
  • TypeScript support with full type definitions

Quick Start

1. Install

npm install @mihari/logger-nuxtjs
# or
pnpm add @mihari/logger-nuxtjs
# or
yarn add @mihari/logger-nuxtjs

2. Configure

Add the module to your nuxt.config.ts:

export default defineNuxtConfig({
  modules: ['@mihari/logger-nuxtjs'],
  mihari: {
    token: process.env.MIHARI_TOKEN,
    endpoint: 'https://api.mihari.io/v1/logs',
  },
})

That's it. The module auto-registers the plugin, composable, and server middleware.

3. Use the Logger

<script setup>
const logger = useLogger()

logger.info('User clicked checkout', {
  userId: '123',
  cartTotal: 59.99,
})
</script>

Configuration

All options can be set in nuxt.config.ts under the mihari key:

| Option | Type | Default | Description | |--------|------|---------|-------------| | token | string | '' | Bearer token for API authentication. Also reads MIHARI_TOKEN env var. | | endpoint | string | '' | API endpoint URL. Also reads MIHARI_ENDPOINT env var. | | enabled | boolean | true | Enable or disable the module | | batchSize | number | 10 | Number of log entries to batch before flushing | | flushInterval | number | 5000 | Flush interval in milliseconds | | maxRetries | number | 3 | Maximum retry attempts for failed requests | | retryDelay | number | 1000 | Base delay (ms) for exponential backoff | | gzip | boolean | true | Enable gzip compression | | logLevel | string | 'debug' | Minimum log level: debug, info, warn, error, fatal | | serverLogging | boolean | true | Enable automatic server request/response logging | | clientErrorCapture | boolean | true | Auto-capture client-side Vue and app errors |

Environment Variables

The module reads these environment variables as fallbacks:

  • MIHARI_TOKEN - Bearer token for authentication
  • MIHARI_ENDPOINT - API endpoint URL

Example: Full Configuration

export default defineNuxtConfig({
  modules: ['@mihari/logger-nuxtjs'],
  mihari: {
    token: process.env.MIHARI_TOKEN,
    endpoint: 'https://api.mihari.io/v1/logs',
    enabled: process.env.NODE_ENV === 'production',
    batchSize: 20,
    flushInterval: 10000,
    maxRetries: 5,
    retryDelay: 2000,
    logLevel: 'info',
    serverLogging: true,
    clientErrorCapture: true,
  },
})

API

useLogger()

Auto-imported composable. Returns a MihariLogger instance.

const logger = useLogger()

logger.debug('Debugging info', { key: 'value' })
logger.info('User action', { action: 'click', target: 'button' })
logger.warn('Deprecation notice', { feature: 'oldApi' })
logger.error('Operation failed', { code: 'TIMEOUT', retries: 3 })
logger.fatal('Critical failure', { service: 'payment' })

// Force flush pending logs
await logger.flush()

$mihari

The logger is also available as $mihari on the Nuxt app instance:

// In a plugin
export default defineNuxtPlugin((nuxtApp) => {
  nuxtApp.$mihari.info('Plugin initialized')
})

Log Entry Format

Each log entry sent to the API follows this structure:

{
  "dt": "2024-01-15T10:30:00.000Z",
  "level": "info",
  "message": "User logged in",
  "runtime": "client",
  "userAgent": "Mozilla/5.0 ...",
  "route": "/dashboard",
  "userId": "abc123"
}

Server-side entries include hostname and pid. Client-side entries include userAgent and route.

Server Middleware

When serverLogging is enabled (default), all HTTP requests are automatically logged with:

  • HTTP method and path
  • Status code
  • Response duration (ms)
  • User agent
  • Request ID (x-request-id header or auto-generated UUID)

Server logs are categorized by status code:

  • 2xx/3xx -> info level
  • 4xx -> warn level
  • 5xx -> error level

Architecture

Client-side proxy

Client-side logs are not sent directly to the remote endpoint. Instead, they go through a built-in Nitro server route (/api/_mihari/logs) that injects the Bearer token server-side before forwarding to the configured endpoint.

Browser  ──POST /api/_mihari/logs──▶  Nitro proxy  ──POST + Bearer token──▶  Remote API

This ensures the authentication token is never exposed in the client bundle or in browser network requests.

Server-side logs (middleware, SSR) are sent directly to the remote endpoint with the token from runtimeConfig.

Transport

The transport layer batches log entries and sends them via HTTP POST:

  • Batching: Logs are buffered and sent when the batch size is reached or the flush interval fires
  • Retry: Failed requests are retried with exponential backoff (base delay doubles each attempt)
  • Error handling: 4xx errors (except 429) are not retried; 5xx and 429 errors trigger retries
  • Flush on unload: Client-side logs are flushed on beforeunload

API Contract

Request (server-side / proxy to remote):

  • POST to configured endpoint
  • Authorization: Bearer <token>
  • Content-Type: application/json
  • Body: array of log entries

Request (client-side to proxy):

  • POST /api/_mihari/logs
  • Content-Type: application/json
  • Body: array of log entries

Response (202):

{
  "status": "accepted",
  "count": 10
}

Development

# Install dependencies
npm install

# Prepare module for development
npm run dev:prepare

# Run playground
npm run dev

# Build the module
npm run build

# Run tests
npm run test

License

MIT