Catalog npm
Back to packageRELEASE DETAILS

1.0.0

npm

1001-logger

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

correlation-idloggertypescriptwinston
Published28 Aug 2024
Direct dependencies5
Transitive dependenciesNot calculated

README

Current package documentation

1001-Logger

1001-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();
    });
}

Downloads & popularity

Awaiting observations

No metric observations yet

Download counts and popularity will appear here as the collector receives them.

Declared dependencies 5

Native requirements
5 direct targetsNot calculated transitive count
Package / targetRequirementRelationship
winston
Environment
{
  "local_name": "winston",
  "alias": false,
  "peer_metadata": null
}
^3.14.2runtime
cls-hooked
Environment
{
  "local_name": "cls-hooked",
  "alias": false,
  "peer_metadata": null
}
^4.2.2runtime
typescript
Environment
{
  "local_name": "typescript",
  "alias": false,
  "peer_metadata": null
}
^5.5.4development
@types/node
Environment
{
  "local_name": "@types/node",
  "alias": false,
  "peer_metadata": null
}
^22.5.1development
@types/cls-hooked
Environment
{
  "local_name": "@types/cls-hooked",
  "alias": false,
  "peer_metadata": null
}
^4.3.8development

Distribution files

1 collected
Distribution 1

Source distribution

Source file
Checksums from source
{
  "shasum": "7a6457ccfccc49ae115e1237b8dcba1f07357f4e",
  "tarball": "https://registry.npmjs.org/1001-logger/-/1001-logger-1.0.0.tgz",
  "fileCount": 3,
  "integrity": "sha512-GwnilPGFMe+99Y380XMx3SzRtGus64yEkv8NCAwwmy76+rdcNU1BR5zU6xL8qt+OFnFOGr9aJwsIxO97XeZUnA==",
  "signatures": [
    {
      "sig": "MEUCIQCc159IaSW5Mva/C+Fh7nkBxdOp/F6ZjAx/5xpbrXHZJwIgA9QU+vS7Yq5I8ZrrwUwlKoU5iH9UmbdKrza7ZSoN0oc=",
      "keyid": "SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"
    }
  ],
  "unpackedSize": 1966
}

Metadata history

Meaningful changes
Current collected state Current

Observed since 20 Oct, 17:59 UTC

No superseded metadata states collected. Unchanged observations do not create new revisions.

Record identity

package_version:ebba9aa08d59d95ffc8fa413fe5c8ee5b883f80722bc7167dc9e083e2badecb6

Other collected versions

1.0.428 Aug1.0.328 Aug1.0.228 Aug1.0.128 AugView all collected versions →