@jefferywa/node-logger
TypeScript icon, indicating that this package has built-in type declarations

2.4.6 • Public • Published

README

Simple runtime logging library based on bunyan

Version npm Version git Version git

NPM

Quick Start

The recommended use of NodeLogger is to create a singleton logger instance when the server application is initialized. The easiest way to do this is to use the static Logger.create method, passing the settings object into it.

// TypeScript
import { Logger } from '@jefferywa/node-logger';
// or JavaScript
const { Logger } = require('@jefferywa/node-logger');

const logger = Logger.create({}); // Your logger instance

Logging

Logging levels

The values for the levels are taken from the Bunyan library and extended with a Z value (for JSON logging)

const levels = {
  70: 'Z', // JSON
  50: 'E', // ERROR
  40: 'W', // WARNING
  30: 'I', // INFO
  20: 'D', // DEBUG
  10: 'T', // TRACE
};

Creating your Logger instance

You get started by creating a logger using Logger.create:

const logger = Logger.create({
  name: 'EXAMPLE_PROJECT_NAME', // - Write your project name
  type: 'backend', // - Write your project type, for example `backend` or `api` 
  level: 'INFO', // - Write default logger level, in default settings it is INFO
  serializers: {
    // Your serializers
    err: function (err: any): any {
      return {
        message: JSON.stringify(err.message),
        name: err.name,
        stack: err.stack,
      };
    },
    stringData: (data: any[] | object): string => {
      return JSON.stringify(data);
    },
    secureStringData: (data: any[] | object): string => {
      const dataStr = JSON.stringify(data);
      return maskString(dataStr); // You can use functions to hide values
    },
    ...
  },
  maxMessageLength: 256, // - Write maximum log row length, this setting worked with field `isTrim`
  isTrim: true, // - If set to `true`, will enable the `Trim` mode using the `maxMessageLength` parameter, this setting working with setting 'isMapper=true' 
  isMapper: false, // If set to `true`, Mapper mode will be enabled, for a more detailed listing of the value in the entry log line
  isJSON: true, //  If set to `true`, `logger.json` method support will be enabled, by default `false`
  isGelf: false, // If set to `true`, logs will be sent to graylog via gelf, this setting working with setting 'isMapper=true' 
  gelfConfig: {
    graylogPort: 12201,
    graylogHostname: '127.0.0.1',
    connection: 'wan',
    maxChunkSizeWan: 1420,
    maxChunkSizeLan: 8154
  }
});

Serializers

Configuration object containing functions that you can use in logging mods such as info and error

// Default serializers
const serializers = {
  header: (headers) => {
    const headerList = { ...headers };

    if (headerList.cookie) {
      headerList.cookie = headerList.cookie
        .replace(HEADER_SID_REGEX, HEADER_REPLACE_PATTERN)
        .replace(HEADER_RM_REGEX, HEADER_REPLACE_PATTERN);
    }

    if (headerList.authorization) {
      headerList.authorization = HEADER_AUTHORIZATION_PATTERN;
    }

    return headerList;
  },
  req: (request) => {
    return {
      url: request.url,
      method: request.method,
      headers: NodeLogger.Serializers.header(request.headers),
    };
  },
  err: (err) => {
    return {
      name: err.name,
      message: JSON.stringify(err.message),
      stack: err.stack,
    };
  },
};

You can also extend the set of these functions through the settings object, as shown above in the creation of a logger instance

Logger methods

logger.info('Your info log string'); // For logging string value
// {"@timestamp":"2022-08-12T15:15:30.999Z","name":"EXAMPLE_PROJECT_NAME","type":"backend","hostname":"notebook.local","pid":18585,"time":"2022-08-12T15:15:30.999Z","v":0,"level":"I","msg":"Your info log string","level_number":30}

logger.json({stringData: {data: {message: 'your data'}}}, 'Your log string'); // For logging json values
// {"@timestamp":"2022-08-12T15:15:30.999Z","name":"example","type":"example","hostname":"notebook.local","pid":18585,"stringData":"{\"data\":{\"message\":\"your data\"}}","time":"2022-08-12T15:15:30.999Z","v":0,"level":"Z","msg":"Your log string","level_number":70}

logger.error({err: {name: 'Error', message: 'Error message', stack: "Error: Error message stack trace" }}, 'Your error log string'); // For loggin errors
// {"@timestamp":"2022-08-14T17:01:24.499Z","name":"example","type":"example","hostname":"notebook.local","pid":18585,"err":{"message":"\"Error message\"","name":"Error","stack":"Error: Error message stack trace"},"time":"2022-08-14T17:01:24.498Z","v":0,"level":"E","msg":"Your error log string","level_number":50}

logger.warn('Your warning log string'); // For logging warnings
// {"@timestamp":"2022-08-14T17:04:16.330Z","name":"example","type":"example","hostname":"notebook.local","pid":18585,"time":"2022-08-14T17:04:16.330Z","v":0,"level":"W","msg":"Your warning log string","level_number":40}

// etc. As well as all the methods supported by bunyan

Installation

npm install @jefferywa/node-logger
yarn add @jefferywa/node-logger

TypeScript support

npm install @types/bunyan
yarn add @types/bunyan

Author: JefferyWa (Vsevolod Golubinov)

Package Sidebar

Install

npm i @jefferywa/node-logger

Weekly Downloads

58

Version

2.4.6

License

ISC

Unpacked Size

44.3 kB

Total Files

43

Last publish

Collaborators

  • jefferywa