0.0.0 • Published 1 year ago

@mediamonks-ea/node-logger v0.0.0

Weekly downloads
-
License
ISC
Repository
-
Last release
1 year ago

NodeJS Logger

This is Typescript based library that provides a standardised interface for logging in NodeJS based applications.

It currently comes with 2 built in loggers:

ConsoleLogger

  • Writes logs to the console
  • Provides colourised output for different log levels

GCPBunyanLogger

  • Writes logs to the GCP Logging service utilising Bunyan under the hood

Installation

npm install @mediamonks-ea/nodejs-logger

Features

  • Standardised interface for logging so you can switch loggers without changing your code
  • Built in loggers for console and GCP
  • Ability to create your own loggers
  • A number of different log levels supported
  • Ability to add global labels to all logs
  • Ability to add labels to individual logs

Usage

Using a built-in logger

import { ConsoleLogger, LoggingLevel } from '@mediamonks-ea/nodejs-logger'

const logger = new ConsoleLogger('example-logger', LoggingLevel.info, {
  enableLabels: true
})

logger.info(`This message will be logged when the logger level is set to info`)
logger.debug(
  `This message will not be logged when the logger level is set to info`
)

Creating a custom logger

import { ILogger, LoggingLevel } from '@mediamonks-ea/nodejs-logger'

export class FileLogger implements ILogger {
  debug(message: any, labels?: { [p: string]: string }): void {
    // ...
  }

  error(message: any, labels?: { [p: string]: string }): void {
    // ...
  }

  fatal(message: any, labels?: { [p: string]: string }): void {
    // ...
  }

  info(message: any, labels?: { [p: string]: string }): void {
    // ...
  }

  setLabels(labels: { [p: string]: string }): void {
    // ...
  }

  setLoggingLevel(level: LoggingLevel): void {
    // ...
  }

  trace(message: any, labels?: { [p: string]: string }): void {
    // ...
  }

  warn(message: any, labels?: { [p: string]: string }): void {
    // ...
  }
}