Skip to content
This repository has been archived by the owner on Jul 22, 2022. It is now read-only.

A Winston logger with Datadog support written in Typescript

Notifications You must be signed in to change notification settings

kioannou/winston-datadog-logger

Repository files navigation

code style: prettier Build Status

Winston Datadog Logger

A winston logger with datadog support.

How to install

$ npm i winston-datadog-logger

Transport

The library exposes the transport itself.
You can initialize a transport with the following options:

const options: IDogapiTransportOptions = {
   apiKey: null, // string. The datadog api key of your application
   appKey: null, // string. The datadog app key of your application
   handleExceptions: true, // boolean. It defaults to `true`
   level: 'debug', // (optional) WinstonLevel. Defaults to `debug`. Possible values `error`, `warning`, `info`, `verbose`, `debug`, `silly`
   logDatadogEvents: true, // boolean. It defaults to `true`
   silent: true, // boolean. It defaults to `true`
   tags: ['environment:production', 'version:1.2.3'], // allows transport level tagging in datadog
   title: 'test-title' // string. It defaults to empty and can be overridden in the log messages
}

Logger

The library provides a Logger instance.

The LoggerOptions (The options for initializing the logger)

The WinstonEvent enum that exposes the available event levels for your logs.

import { Logger, 
         LoggerOptions, 
         WinstonEvent } from 'winston-datadog-logger';

// Initializing the options. 
const options = {
  exitOnError: false, // boolean. It defaults to `false`
  environment: null, // (optional) string. It defaults to `null`
  instance: null, // (Optional) string. It defaults to `null`
  eventMapping: { // (Optional) The mapping of Winston events to Datadog ones. Possible values: `info`, `error`, `warning`, `success`
    debug: 'info',
    error: 'error',
    info: 'info',
    silly: 'info',
    verbose: 'info',
    warn: 'warning',
  },
  logToConsole: false, // (optional) boolean. It defaults to `null`
  consoleTransportOptions: {
    level: 'debug', // (optional) WinstonLevel. Defaults to `debug`. Possible values `error`, `warning`, `info`, `verbose`, `debug`, `silly`
    silent: true, // (optional) boolean. It defaults to `true`
    handleExceptions: true // (optional) boolean. It defaults to `true`
  },
  datadogLoggerEnabled: true, // (optional) boolean. It defaults to `true`
  dogapiTransportOptions: {
    apiKey: null, // string. The datadog api key of your application
    appKey: null, // string. The datadog app key of your application
    handleExceptions: true, // boolean. It defaults to `true`
    level: 'debug', // (optional) WinstonLevel. Defaults to `debug`. Possible values `error`, `warning`, `info`, `verbose`, `debug`, `silly`
    logDatadogEvents: true, // boolean. It defaults to `true`
    tags: ['environment:production', 'version:1.2.3'], // allows transport level tagging in datadog
    title: 'test-title' // string. It defaults to empty and can be overridden in the log messages
  }
};

// Initializing the Logger using the options
Logger.initialize(options);


// Use the logger like this
// Logger.log({ level }, { message }, { meta });

// e.g
Logger.log(WinstonEvent.Debug, 'example message', {'title': 'your-title'});

Additionally, the library provides a Transport instance if composing your own logger. This is consumed similarly to the logger, albeit with fewer options

import * as winston from 'winston';
import { Transport as DatadogTransport } from 'winston-datadog-logger';

const options = { /* same as above */ };

const logger = winston.createLogger({
  transports: [DatadogTransport(
    options.dogapiTransportOptions,
    options,
  )],
});

logger.log('debug', 'example message', { 'title': 'your-title' });

The second argument is optional, merely an escape hatch for any overrides from the larger logger options above.