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

log4u

v1.0.6

Published

`Log4U` is a lightweight, production-grade and flexible logging library for Node.js, written in TypeScript. Designed to make logging effortless and customizable, it supports structured logs, custom formats, and daily log rotation.

Downloads

36

Readme

Log4U - A Flexible Logging Library for Node.js

Log4U is a lightweight, production-grade and flexible logging library for Node.js, written in TypeScript. Designed to make logging effortless and customizable, it supports structured logs, custom formats, and daily log rotation.


Features

  • 🌟 Easy-to-Use: Minimal setup to start logging in seconds.
  • 🛠️ Highly Configurable: Define your logging levels, formats, and more.
  • 🕒 Daily Log Rotation: Automatically creates a new log file for each day.
  • 📄 Rich Log Details: Supports structured logs with optional parameters like payloads, status codes, and URLs.
  • 🔍 Error Handling: Logs complete error stack traces for debugging.
  • TypeScript Support: Fully typed for seamless integration.

Installation

Install the package using npm or yarn:

npm install log4u
# or
yarn add log4u

Getting Started

Step 1: Import and Initialize

Start by importing the Logger class and configuring it with your service name.

import Logger from 'log4u';

const logger = new Logger({ serviceName: 'MyService' });

Step 2: Logging Your Data

You can log structured data with minimal effort:

logger.log({
  type: 'INFO', // Log level: INFO, DEBUG, ERROR
  message: 'This is an informational log.', // Required
});

logger.log({
  type: 'DEBUG',
  message: 'Logging with additional details.',
  payload: { id: 123, action: 'test' }, // Optional payload
  statusCode: 200,                      // Optional status code
  sourceURL: 'http://localhost:3000',   // Optional source URL
  targetURL: 'http://localhost:5173',   // Optional target URL
});

Step 3: Logging Errors

Log errors directly with stack traces for debugging:

try {
  throw new Error('Something went wrong!');
} catch (err) {
  logger.log({
    type: 'ERROR',
    message: err, // Pass the error object directly
    logId: 'ERR001',
  });
}

Advanced: Adding Middleware for Request Logging

Log incoming requests in an Express.js backend:

import { Request, Response, NextFunction } from 'express';
import Logger from 'log4u';

const logger = new Logger({ serviceName: 'MyBackendService' });

function logRequest(req: Request, res: Response, next: NextFunction) {
  const logData: any = {
    type: 'INFO',
    message: 'Incoming request',
    sourceURL: req.get('Origin') || req.get('Referer'),
    targetURL: req.originalUrl,
    payload: req.body,
  };

  res.on('finish', () => {
    logData.statusCode = res.statusCode;
    logger.log(logData);
  });

  next();
}

// Use the middleware in your app
app.use(logRequest);

Configuration Options

When initializing the logger, you can pass the following options:

| Option | Type | Default | Description | |----------------|----------|-------------|-------------------------------------------------| | serviceName | string | undefined | The name of your service (included in logs). | | logDirectory | string | ./logs | Directory where log files are stored. | | dateFormat | string | YYYY-MM-DD| Format for daily log file names. |

Log Data Structure

Here’s a breakdown of the log data you can provide (to be added more in future):

| Field | Required | Type | Description | |--------------|----------|-----------------|--------------------------------------------------| | type | Yes | string | Log level: INFO, DEBUG, or ERROR. | | message | Yes | string/Error| A short description or error object. | | logId | No | string | Unique identifier for the log. | | payload | No | object | Any additional data related to the log. | | statusCode | No | number | HTTP status code (e.g., 200, 404, 500). | | sourceURL | No | string | Source of the request or action (e.g., API endpoint). | | targetURL | No | string | Target URL for the action. |

Example Log Output

For a simple log:

INFO 2024-11-30T12:00:00Z MyService Testing log package

For a log with additional details:

DEBUG 2024-11-30T12:05:00Z MyService Logging with additional details {"id":123,"action":"test"} 200 http://localhost:3000 http://localhost:5173

For an error log:

ERROR 2024-11-30T12:10:00Z MyService ERR001 Something went wrong!
Error: Something went wrong!
    at Object.<anonymous> (/path/to/file.js:10:15)
    ...

Happy Logging! 🎉