1.0.0 • Published 4 years ago

@chance-get-yours/server-logger v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
4 years ago

server-logger

Abstract

Logger for Node.js servers. This package provides a set of functions to log messages in the console, to Slack and to Rollbar.

Usage

Install dependency

$ yarn add @chance-get-yours/server-logger

Implementation

  1. Require the package.
const ServerLogger = require('@chance-get-yours/server-logger');
  1. Initialize the logger.
const myLogger = new ServerLogger(config);
  1. Call any log method on the logger.
myLogger.error(someError);

Api

constructor(config)

You can initialize your logger with a config object containing the following fields:

  • silent: boolean (optional, default false) - useful for silencing the logger in testing environments.
  • app: Object (required) - application data.
    • name: string (required) - name of the application.
    • environment: string (required) - environment the logger operates in.
  • transports: Object (optional) - transports data.
    • slack: Object (optional) - Slack transport data.
      • channel: string (required) - Slack channel to post the logs to.
      • token: string (required) - Slack API token (from an app).
      • username: string (optional, default Logger) - Slack username to post messages with.
    • rollbar: Object (optional) - Rollbar transport data.
      • token: string (required) - Rollbar secret token of your account.
  • allowedScopes: array of string (optional, default ["."]) - file system scopes that are allowed to log.

As an example of possible config:

{
  silent: false,
  app: {
    name: "The name of my app",
    environment: "production",
  },
  transports: {
    slack: {
      channel: "some-channel-name",
      token: "some-api-token",
      username: "Some cool username",
    },
    rollbar: {
      token: "some-secret-token",
    },
  },
  allowedScopes: ["/src"],
}

If no transports.slack or transports.rollbar configuration is provided the logger will ignore those services.

Regarding allowedScopes, you should define as many scopes as you want to allow (whitelist). By default "." is used to allow every subfolder inside your project.

.debug(filename, ...context)

Class method to emit a debug log about a context.

  • filename: String - the filename path where you are debugging.
  • ...context: ...String - any number of strings to give context to the debug message.

Example:

myLogger.debug(__filename, 'Create function', 'Check arguments');

// The logger would log something like this =>
//
// /src/some/path/bestFile.js :: Create function :: Check arguments

.info(filename, ...context)

Class method to emit an info log about a context.

  • filename: String - the filename path where you are debugging.
  • ...context: ...String - any number of strings to give context to the info message.

Example:

myLogger.info(__filename, 'Create function', 'Check arguments');

// The logger would log something like this =>
//
// /src/some/path/bestFile.js :: Create function :: Check arguments

.warn(errorObject)

Class method to emit a warn log about an error.

  • errorObject: Error - the error object to log.

Example:

myLogger.warn(someErrorObject);

// The logger would log something like this =>
//
// /src/some/path/bestFile.js :: createSomething : Argument `id` was expected
// ...stack

.error(errorObject)

Class method to emit an error log about an error.

  • errorObject: Error - the error object to log.

Example:

myLogger.error(someErrorObject);

// The logger would log something like this =>
//
// /src/some/path/bestFile.js :: createSomething : Argument `id` was expected
// ...stack

Notes

Winston logging levels

Please refer to the original docs.

As we don't specify any logging levels either as a config parameter or internally, the npm levels are used:

{
  error: 0,
  warn: 1,
  info: 2,
  verbose: 3,
  debug: 4,
  silly: 5
}

Note that when a level is specified in a transport, all levels that have lower values are also included.

The ConsoleTransport is set to debug level, the SlackTransport and RollbarTransport are set to warn level.

This package implements logging methods for error, warn, info and debug levels. The first two will propagate to Slack and Rollbar and all 4 of them will be logged in the console.