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

pino-config

v1.0.9

Published

Custom Pino configuration for database logging.

Readme

pino-config

Custom Pino configuration for database logging with automatic cleanup and environment-based log levels.

Features

  • 🎨 Pretty logging with colored output for development
  • 💾 Database logging integration through custom handlers
  • 🧹 Automatic log cleanup with configurable cron jobs
  • 🌍 Environment-based log level configuration
  • 🔧 TypeScript support with full type definitions
  • ⚡ Optional initialization hook for database setup

Installation

npm install pino-config

Usage

Basic Setup

First, implement the IDbLogHandler interface to define how logs should be stored in your database:

import { IDbLogHandler } from 'pino-config';

class MyDatabaseHandler implements IDbLogHandler {
  environment: string;

  constructor(env: string) {
    this.environment = env;
  }

  async initialize(): Promise<void> {
    // Optional: Setup database connection, create tables, etc.
    console.log('Database initialized');
  }

  async saveLog(level: string, message: string, meta?: object): Promise<void> {
    // Save log to your database
    // Example: await db.logs.create({ level, message, meta, timestamp: new Date() });
  }

  async cleanUpLogs(): Promise<number> {
    // Clean up old logs (e.g., delete logs older than 30 days)
    // Return the number of deleted logs
    // Example: const result = await db.logs.deleteMany({ timestamp: { $lt: thirtyDaysAgo } });
    // return result.deletedCount;
    return 0;
  }
}

Creating the Logger

import { createLogger } from 'pino-config';

const handler = new MyDatabaseHandler(process.env.NODE_ENV || 'development');
const logger = createLogger(handler);

// Use the logger
logger.debug('Debug message', { userId: 123 });
logger.info('User logged in', { username: 'john' });
logger.warn('Deprecated API called', { endpoint: '/old-api' });
logger.error('Database connection failed', { error: 'Connection timeout' });

API Reference

IDbLogHandler Interface

Your database handler must implement this interface:

interface IDbLogHandler {
  environment: string;
  saveLog(level: string, message: string, meta?: object): Promise<void>;
  cleanUpLogs(): Promise<number>;
  initialize?(): Promise<void>;
}

Properties

  • environment: string - Current environment ('development', 'production', 'test', etc.)

Methods

  • saveLog(level, message, meta?): Saves a log entry to the database

    • level: Log level ('debug', 'info', 'warn', 'error')
    • message: Log message
    • meta: Optional metadata object
  • cleanUpLogs(): Deletes old logs from the database

    • Returns the number of deleted logs
  • initialize() (optional): Initialize database connection or setup

    • Called automatically when creating the logger

createLogger(handler)

Creates and configures a logger instance.

Parameters

  • handler: IDbLogHandler - Your database handler implementation

Returns

A logger object with the following methods:

  • debug(message, meta?): Log debug messages (only in development)
  • info(message, meta?): Log informational messages
  • warn(message, meta?): Log warning messages
  • error(message, meta?): Log error messages

Environment-Based Behavior

Development Environment

  • Log level: debug
  • Console output: Colorized and pretty-printed
  • Debug logs are saved to database
  • Cron job for cleanup is active

Production Environment

  • Log level: info (debug logs are ignored)
  • Console output: Colorized and pretty-printed
  • Only info, warn, and error logs are saved
  • Cron job for cleanup is active

Test Environment

  • Log level: debug
  • Console output: Colorized and pretty-printed
  • All logs are saved to database
  • Cron job is disabled to avoid interference with tests

Automatic Log Cleanup

By default, a cron job runs daily at 00:00 to clean up old logs by calling handler.cleanUpLogs().

This behavior is:

  • Active in development and production environments
  • Disabled in test environment

The cleanup logic is implemented in your cleanUpLogs() method, giving you full control over what gets deleted.

Example with MongoDB

import { createLogger, IDbLogHandler } from 'pino-config';
import { MongoClient, Db } from 'mongodb';

class MongoDbLogHandler implements IDbLogHandler {
  environment: string;
  private db?: Db;

  constructor(env: string) {
    this.environment = env;
  }

  async initialize(): Promise<void> {
    const client = await MongoClient.connect(process.env.MONGO_URL!);
    this.db = client.db('logs');
  }

  async saveLog(level: string, message: string, meta?: object): Promise<void> {
    await this.db!.collection('logs').insertOne({
      level,
      message,
      meta,
      timestamp: new Date(),
    });
  }

  async cleanUpLogs(): Promise<number> {
    const thirtyDaysAgo = new Date(Date.now() - 30 * 24 * 60 * 60 * 1000);
    const result = await this.db!.collection('logs').deleteMany({
      timestamp: { $lt: thirtyDaysAgo },
    });
    return result.deletedCount;
  }
}

const handler = new MongoDbLogHandler(process.env.NODE_ENV || 'development');
const logger = createLogger(handler);

logger.info('Application started', { version: '1.0.0' });

TypeScript Support

This package is written in TypeScript and includes full type definitions. No need to install additional @types packages.

Dependencies

  • pino: Fast and low overhead logging library
  • node-cron: Cron job scheduler for automatic log cleanup

License

MIT

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.