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

sparsh-node-logger

v1.0.6

Published

Centralized logging and error handling for Node.js microservices

Readme

Node.js Centralized Logging & Error Handling

A comprehensive logging and error handling module for Node.js microservices using Winston.

Features

  • Standardized Error Handling: ApiError class with centralized error codes
  • Multi-transport Logging: Console and file logging with Winston
  • HTTP Request Tracking: Middleware for logging requests with performance metrics
  • Environment-aware Formatting: Colorized for development, JSON for production
  • Structured Logging: Consistent metadata across all log entries

Installation

npm install

Usage

Basic Setup

const express = require('express');
const { logger, errorHandler, requestLogger } = require('./path-to-module');

const app = express();

// Apply request logger as early as possible
app.use(requestLogger);

// Your routes and other middleware here

// Apply error handler as the last middleware
app.use(errorHandler);

app.listen(3000, () => {
  logger.info('Server started on port 3000');
});

Logging

const { logger } = require('./path-to-module');

// Different log levels
logger.error('Critical application error', { details: err });
logger.warn('Warning condition', { source: 'database' });
logger.info('Informational message');
logger.http('HTTP-specific information');
logger.debug('Debugging information');

// With metadata
logger.info('User logged in', {
  userId: user.id,
  email: user.email,
  timestamp: new Date()
});

Error Handling

const { ApiError } = require('./path-to-module');

// Using error factory methods
app.get('/users/:id', (req, res, next) => {
  const user = findUser(req.params.id);
  
  if (!user) {
    return next(ApiError.notFound('User not found'));
  }
  
  if (!canAccessUser(req.user, user)) {
    return next(ApiError.forbidden('You cannot access this user'));
  }
  
  res.json(user);
});

// Handling validation errors
app.post('/users', (req, res, next) => {
  const { name, email } = req.body;
  const errors = {};
  
  if (!name) errors.name = 'Name is required';
  if (!email) errors.email = 'Email is required';
  
  if (Object.keys(errors).length > 0) {
    return next(ApiError.validationError('Validation failed', errors));
  }
  
  // Create user...
  res.status(201).json(user);
});

// Catching and converting other errors
app.get('/data', async (req, res, next) => {
  try {
    const data = await fetchData();
    res.json(data);
  } catch (err) {
    // Convert to API error
    next(ApiError.internal('Failed to fetch data', {}, err));
  }
});

Example Server

Run the example server to see the logger in action:

npm start

Then visit:

  • http://localhost:3000/ - Basic route
  • http://localhost:3000/log-levels - See different log levels
  • http://localhost:3000/error - Triggers a generic error
  • http://localhost:3000/api-error - Triggers an API error
  • http://localhost:3000/not-found - 404 error
  • POST http://localhost:3000/users - Test validation errors

Environment Variables

  • NODE_ENV: Set to 'production' for production-optimized logging
  • LOG_LEVEL: Override default log level (default: 'debug' in dev, 'info' in prod)
  • SERVICE_NAME: Name of your service (included in logs)
  • LOG_DIR: Directory for log files (default: 'logs')