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 🙏

© 2025 – Pkg Stats / Ryan Hefner

logzai-js

v1.0.11

Published

LogzAI JS - A TypeScript library for OpenTelemetry logging

Downloads

553

Readme

LogzAI JS

Official JavaScript/TypeScript client for LogzAI — send logs to the LogzAI platform using the OpenTelemetry Protocol (OTLP).

Installation

npm install logzai-js

Usage

Node.js Example

import logzai from 'logzai-js';

// Initialize LogzAI
logzai.init({
  ingestToken: 'your-ingest-token',
  ingestEndpoint: 'https://ingest.logzai.com',
  serviceName: 'my-node-app',
  environment: 'production'
});

// Send logs
logzai.info('Application started', { version: '1.0.0' });
logzai.error('Something went wrong', { error: 'details' });

// Cleanup
await logzai.shutdown();

Browser Example

<script type="module">
  import logzai from 'logzai-js/browser';

  // Initialize LogzAI
  logzai.init({
    ingestToken: 'your-ingest-token',
    ingestEndpoint: 'https://ingest.logzai.com',
    serviceName: 'my-web-app',
    environment: 'production'
  });

  // Send logs
  logzai.info('User action', { userId: '123', action: 'click' });
</script>

API Reference

Logging Methods

// Log levels
logzai.debug('Debug message', { key: 'value' });
logzai.info('Info message', { key: 'value' });
logzai.warn('Warning message', { key: 'value' });
logzai.error('Error message', { key: 'value' });

// Exception logging with stack traces
logzai.exception('Error occurred', new Error('Something failed'), {
  userId: '123',
  context: 'checkout',
});

Tracing

// Wrap functions with spans for distributed tracing
await logzai.span('my-operation', async (span) => {
  // Your operation here
  span.setAttribute('custom.attribute', 'value');
  return result;
}, { operation: 'database-query' });

Initialization Options

logzai.init({
  ingestToken: string;          // Required: Your LogzAI ingest token
  ingestEndpoint: string;        // Required: LogzAI ingest endpoint
  serviceName?: string;          // Optional: Service name (default: 'app')
  serviceNamespace?: string;     // Optional: Service namespace (default: 'default')
  environment?: string;          // Optional: Environment (default: 'production')
  mirrorToConsole?: boolean;     // Optional: Also log to console (default: false)
  timeoutMillis?: number;        // Optional: Request timeout (default: 10000)
});

Plugin System

LogzAI supports a plugin system for easy integration with different frameworks and environments.

Browser Plugin

The browser plugin automatically captures JavaScript errors and unhandled promise rejections.

Basic Usage

import logzai, { browserPlugin } from 'logzai-js/browser';

// Initialize LogzAI
logzai.init({
  ingestToken: 'your-ingest-token',
  ingestEndpoint: 'https://ingest.logzai.com',
  serviceName: 'my-app',
});

// Enable automatic error capture
logzai.plugin('browser', browserPlugin);

Advanced Configuration

import logzai, { browserPlugin } from 'logzai-js/browser';
import store from './store'; // Your Redux/Vuex store

logzai.init({ /* ... */ });

logzai.plugin('browser', browserPlugin, {
  // Capture both errors and unhandled rejections (default: true)
  captureErrors: true,
  captureUnhandledRejections: true,

  // Filter errors before logging
  errorFilter: (error) => {
    // Skip non-critical errors
    return !error.message?.includes('ResizeObserver');
  },

  // Inject context from your application state
  contextInjector: () => {
    const state = store.getState();
    return {
      userId: state.user?.id,
      userEmail: state.user?.email,
      currentRoute: window.location.pathname,
    };
  },

  // Custom error message formatting
  messageFormatter: (error) => {
    return `Error: ${error.message}`;
  },
});

Configuration Options

  • captureErrors (boolean, default: true): Enable window.onerror handler
  • captureUnhandledRejections (boolean, default: true): Enable window.onunhandledrejection handler
  • errorFilter (function): Filter errors before logging. Return false to skip an error.
  • contextInjector (function): Inject additional context (user info, route, etc.) into error logs
  • messageFormatter (function): Custom error message formatting

Express.js Plugin

The Express plugin automatically logs HTTP requests, responses, and errors.

Basic Usage

import express from 'express';
import logzai, { expressPlugin } from 'logzai-js';

const app = express();

logzai.init({ /* ... */ });

// Enable automatic request/response/error logging
logzai.plugin('express', expressPlugin, { app });

This will automatically:

  • Create a span for each HTTP request with timing information
  • Log requests and responses with duration: POST /ingest/otel/logs -> 200 2.35s
  • Associate all logs that happen during the request with the request span
  • Use appropriate log levels: info (2xx/3xx), warn (4xx), error (5xx)
  • Capture all unhandled errors with stack traces

Advanced Configuration

import express from 'express';
import logzai, { expressPlugin } from 'logzai-js';

const app = express();

logzai.plugin('express', expressPlugin, {
  app,

  // Skip health check endpoints
  skipPaths: ['/health', '/metrics', '/favicon.ico'],

  // Log slow requests (> 1 second)
  slowRequestThreshold: 1000,

  // Inject user context into all logs
  contextInjector: (req) => ({
    userId: req.user?.id,
    userEmail: req.user?.email,
    sessionId: req.sessionID,
  }),

  // Filter requests before logging
  requestFilter: (req) => {
    // Don't log static assets
    return !req.path.match(/\.(css|js|png|jpg)$/);
  },

  // Log request bodies (careful with sensitive data!)
  logRequestBody: true,
  logResponseBody: false,
});

Distributed Tracing

The Express plugin automatically creates OpenTelemetry spans for each HTTP request. All logs emitted during the request (including logs from your route handlers) are automatically associated with the request span, making it easy to trace the full lifecycle of a request.

app.post('/api/users', async (req, res) => {
  // These logs will be automatically associated with the "POST /api/users" span
  logzai.info('Validating user data', { email: req.body.email });

  const user = await createUser(req.body);
  logzai.info('User created successfully', { userId: user.id });

  res.json({ success: true, userId: user.id });
  // Span ends automatically when response is sent
  // Log message: "POST /api/users -> 200  0.52s"
});

The span includes:

  • HTTP method, path, and route
  • Status code and response time
  • Client IP and user agent
  • Query parameters (if present)
  • Custom attributes from contextInjector

Configuration Options

  • app (required): Express application instance
  • logRequests (boolean, default: true): Log incoming requests
  • logResponses (boolean, default: true): Log responses with status codes
  • captureErrors (boolean, default: true): Capture and log errors
  • skipPaths (string[], default: []): Paths to skip (supports wildcards: '/api/*')
  • slowRequestThreshold (number): Log requests slower than this (ms)
  • requestFilter (function): Filter requests before logging
  • contextInjector (function): Inject additional context (user info, session, etc.)
  • logRequestBody (boolean, default: false): Include request body in logs
  • logResponseBody (boolean, default: false): Include response body in logs

Creating Custom Plugins

You can create your own plugins for custom integrations:

import type { LogzAIPlugin } from 'logzai-js';

const myPlugin: LogzAIPlugin<{ threshold: number }> = (instance, config) => {
  // Setup your plugin
  console.log('Plugin initialized with threshold:', config?.threshold);

  // Use logzai instance methods
  instance.info('Plugin loaded');

  // Return cleanup function (optional)
  return () => {
    console.log('Plugin cleanup');
  };
};

// Use your plugin
logzai.plugin('my-plugin', myPlugin, { threshold: 100 });

// Later, unregister the plugin
logzai.unregisterPlugin('my-plugin');

Development

# Install dependencies
npm install

# Build the library
npm run build

# Run in development mode
npm run dev

Building

The library is built using Vite and outputs multiple formats:

  • ES modules (dist/logzai-js.es.js)
  • CommonJS (dist/logzai-js.cjs.js)
  • UMD (dist/logzai-js.umd.js)
  • TypeScript declarations (dist/index.d.ts)

Publishing

npm publish

License

MIT