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 🙏

© 2024 – Pkg Stats / Ryan Hefner

@1001tv/logger

v1.2.0

Published

A customizable logger using Winston and CLS-Hooked for correlation IDs.

Downloads

17

Readme

1001tv-logger

1001tv-logger is a customizable logging library built on top of Winston with support for CLS-Hooked for correlation IDs. This library is designed for use across multiple TypeScript projects, providing structured logging with mandatory and optional metadata fields for 1001tv services.

Features

  • Structured Logging: Logs are formatted in JSON, making them easy to parse and analyze using log aggregation tools like AWS CloudWatch.

  • Correlation IDs: Automatically injects correlation IDs into logs from CLS-Hooked, ensuring that all logs from a single request are grouped together and making it easy to trace requests across distributed.

  • Customizable Log Levels: Supports custom log levels (error, warn, info).

Metadata

1001-Logger requires certain fields if additional metadata is included in the log message. :

layer (mandatory): Indicates the layer where the log is generated (e.g., 'main', 'service', 'controller', 'repository', 'middleware', 'other'). function (mandatory): The name of the function where the log is printed. Any additional fields can be included as optional metadata (e.g., userId, errorCode).

Usage

1- Basic Setup Import the logger into your project:

import { logInfo, logError } from '1001-logger';

2- Logging Examples

Info Logging

logInfo('This is an informational message');

// Or
logInfo('This is an informational message', {
  layer: 'service', // Mandatory metadata
  function: 'fetchData', // Mandatory metadata
  userId: '1234' // Optional metadata
});

Warn Logging

logWarn('This is a warning message');

// Or
logWarn('This is a warning message', {
  layer: 'controller', // Mandatory
    function: 'handleRequest', // Mandatory metadata
    userId: '5678' // Optional metadata
});

Error Logging

logError('This is an error message');

// Or
logError('This is an error message', {
  layer: 'controller', // Mandatory metadata
  function: 'handleRequest', // Mandatory metadata
  errorCode: 'ERR123', // Optional metadata
  userId: '5678' // Optional metadata
});

Correlation IDs

The logger automatically injects a correlationId into every log message if it is available in the CLS-Hooked namespace (auth-middleware-namespace). This is useful for tracking a single request across various services and layers.

Example:

// Ensure a correlation ID is set up earlier in the middleware or service
import { Request, Response, NextFunction } from 'express';
import { createNamespace } from 'cls-hooked';

const session = createNamespace('auth-middleware-namespace');

export function storeCorrelationIdMiddleware(req: Request, res: Response, next: NextFunction) {
    session.run(() => {
        session.set('correlationId', req.headers['x-correlation-id']);
        next();
    });
}