2.0.3 • Published 2 months ago

log-vault v2.0.3

Weekly downloads
-
License
ISC
Repository
github
Last release
2 months ago

🪵 LogVault

A generator of Winston logger instance with pre-defined and configurable transports and formats.

Extra functionality:

  • capture console logging methods of your app process
  • collect processes names, timestamps, environment name automatically
  • send alarms on specific events to Telegram chat or group

Installation

npm install log-vault

or

yarn add log-vault

Usage

Log levels

By the default, max level of logs is set up to "info". All of the levels below will be supressed. You may define maxLevel in declaration of LogLevel class instance or set it up differently for each of the transports. Default levels:

  • error
  • warn
  • info
  • http
  • verbose
  • debug
  • silly

Extra data

Default LogVault formatters extract all of the additional data passed with log to "extra" object, which is placed differently for each of the transports.

Metadata

Flat key-value pairs may be used as labels for each of the log entry. There are three keys, passing by the default:

  • environment - NODE_ENV env variable of the process
  • project - project root directory name (basepath of PWD env var)
  • process - npm_package_name env variable (usually equal to "name" value in your project's package.json file).

You may extend the labels information using LogOptions constructor:

import { LogVault, LogOptions } from 'log-vault';

const { logger } = new LogVault()

logger.info(
  "A log record",
  new LogOptions({ meta: { myCustomKey: "value" } })
);

environment, project and process labels will be added automatically, no need to define it manually.

Masking sensitive data

Avoid exposing sensitive data by object keys. The default fields are being masked: "password", "token", "otp" and "secret". You may provide the configuration to the LogVault constructor:

import { LogVault, LogOptions } from 'log-vault';

const { logger } = new LogVault({
  maskOptions: {
    maskLabel: "***",
    fields: [ "secret-field-key" ]
  }
});

Truncating nested data

Limit the nested level of the information to keep you log messages light and simple. There is a configuration that is applied by the default, but you may override this:

import { LogVault, LogOptions } from 'log-vault';

const { logger } = new LogVault({
  truncateOptions: {
    depth: 2,
    stringLength: 1024,
    arrayLength: 5,
    replaceWith: "...(Truncated)"
  }
});

Console transport

import { LogVault } from 'log-vault';

const { logger } = new LogVault().withConsole();

logger.log({ level: "info", message: "Hey, I'm a log entry!" });
logger.warn("It's a warning");
logger.error("Whoops!");

Output:

output

Uncaught exceptions and promise rejections are captured to the defined transports by the default:

import { LogVault } from 'log-vault';

new LogVault().withConsole();

throw new Error("An error appear");

Output:

output

Native Console capturing

You may use an option to rewrite the native JS console methods: log, info, warn and error:

import { LogVault } from 'log-vault';

new LogVault().withConsole().captureConsole();

console.log('Something...');

Output:

output

Files transport

Usage:

import { LogVault } from 'log-vault';

const { logger } = new LogVault().withFiles();

By the default, LogVault will record log files into the "log" folder in the project root directory in json format with timestamp. You may configure placement of the log directory and other log options (see Configuration section).

Mongo transport

import { LogVault } from 'log-vault';

const { logger } = new LogVault().withMongo({
  db: "mongodb+srv://usr:pwd@cluster0.some.mongodb.net/?retryWrites=true&w=majority"
});

Loki transport

Use Loki to export your logs into Grafana.

import { LogVault } from 'log-vault';

const { logger } = new LogVault().withLoki();

Please, take into account that due to Loki nature, it is not recommended to have dynamic labels. By the default, only the process name and Node environment data is passed directly to the Loki labels. All of the remaining metadata will be placed into the "meta" object of the log message.

Example of log entry displayed with the default Grafana "Explore" view: grafana_default

I advice you to be more creative with the Grafana dashboard setup to reach a display that would be more convenient for your purposes: grafana_custom grafana_details

Notifications

In some cases, we should be notified with the events occuring in the system. You may set up LogVault Notificator and notification transport to be receive the specific log messages with the defined alarm channels. A Redis v^7 instance running on the machine or distant host is required for this feature.

Sending all logs from the process to the notificator:

const { logger } = new LogVault().withNotifications();

Redis host is considered to run on localhost port 6379 by the default.

Setting up a Notificator instance. You ought to run it in different nodejs process:

const notificator = new Notificator().add(
  new TelegramNotificationChannel({
    token: process.env.TG_BOT_TOKEN,
    chatId: YOUR_CHAT_ID,
    matchPatterns: [
      {
        level: 'error'
      },
      {
        match: {
          message: /error/gi
        }
      }
    ]
  })
);

You may add patterns to match the required log records. If any of the patterns is matched, the log record will be sent to the defined channel.

Supported pattern options:

  • level: any of the supported levels
  • meta: object containing plain key-value pairs
  • message: string or RegExp to match

Currently, only Telegram notification channel is supported. The messages will be sent to a single chat with 5 seconds time gap by the default.

Example Telegram alarm message with the default template:

tg_alarm

Notes on usage with microservices or distributed app

  • If you use files transport, provide the full path to your logs folder if you want to avoid storing the logs in each app directory
  • Use a separate Node process or microservice to serve Notificator instance
  • Configure Notificator BullMQ worker to use several threads to increase the productivity
  • Use the same queue name for the Notificator across the involved processes

Configuration reference

LogVault class constructor options

fielddesctypedefault value
projectNameName of your projectstringproject root directory name (basepath of PWD env var)
truncateOptionsTruncate log message dataobj-walker TruncateOptions{ depth: 5, stringLength: 2048, arrayLength: 12, replaceWith: "...Truncated"}
maskOptionsMasking sensitive fields optionsLogVaultMaskFieldsOptions{ maskLabel: "...Masked", fields: "password", "token", "otp", "secret" }
... (rest parameters)Winston logger creation optionsLoggerOptions{ levels: defaultLevels, level: "http", exitOnError: false, defaultMeta }

LogVault withConsole method options

fielddesctypedefault value
colorsWinston colors set for consoleWinston AbstractConfigSetColorsdefault colors
inspectOptionsOptions for Node's native util.inspectInspectOptions{ compact: false, maxArrayLength: 5, maxStringLength: 2048, depth: 5, colors: true}
... (rest parameters)Winston console transport optionsConsoleTransportOptionsLogVault console formats and defaults

LogVault withFiles method options

fielddesctypedefault value
errorLevelname of the level that is considered as errorstring"error"
... (rest parameters)Winston daily rotate file parametersDailyRotateFile.DailyRotateFileTransportOptionsLogVault file formats and defaults

LogVault withMongo method options

Only Winston MongoDBConnectionOptions are accepted there. LogVault applies a set of formats and defaults for the convenience.

LogVault withLoki method options

LokiTransportOptions are accepted there. By the default, the "host" parameter is pointing to the localhost 3100 port.

LogVault withNotifications method options

fielddesctypedefault value
nameProject's name for BullMQ QueuestringLogVault's instance projectName attribute value
queueOptionsBullMQ Queue optionsQueueOptions{ connection: defaultRedisConnection }
jobOptionsBullMQ Redis Job optionsRedisJobOptionsLogVault notifications Job options

LogVault captureConsole method options

fielddesctypedefault value
matchLevelsA map of Node's Console methods and Winston instance levels{ log: string; warn: string; info: string; error: string; }{ log: "info", warn: "warn", info: "info", error: "error" }

LogVault uncaptureConsole method

No parameters are required there. Use this method to give back the default methods to Node Console.

LogOptions constructor parameters

fielddesctypedefault value
metaAn Object with flat key-value pairsMeta---

Notificator constructor parameters

fielddesctypedefault value
queueNameName of LogVault notifications transport queuestringproject root directory name (basepath of PWD env var)
workerOptsBullMQ Worker optionsWorkerOptions{ connection: defaultRedisConnection, autorun: true }

Notificator class methods

  • "add" - receives a NotificationChannel class instance
  • "stop" - stops Notificator BullMQ Worker
  • "run" - resume worker

TelegramNotificationChannel constructor options

fielddesctypedefault value
tokenTelegram bot secretstringnone (mandatory)
chatIdTelegram chat or group idnumbernone (mandatory)
templateMessage template in mustache formatstringBasic template
workerOptionsBullMQ Worker optionsWorkerOptions{ limiter: { max: 1, duration: 5000 } }
matchPatternsA set of patterns to match in log message to trigger the message sendingMatchPattern
inspectOptionsNode's util.inspect options to render the message objectInspectOptions{ compact: false, maxArrayLength: 5, maxStringLength: 2048, depth: 5, colors: true}
2.0.3

2 months ago

2.0.2

2 months ago

2.0.1

2 months ago

2.0.0

2 months ago

1.2.12

3 months ago

1.2.13

3 months ago

1.2.11

3 months ago

1.2.14

3 months ago

1.2.0

3 months ago

1.2.1

3 months ago

1.1.12

4 months ago

1.1.1

4 months ago

1.1.0

4 months ago

1.1.11

4 months ago

1.0.23

8 months ago

1.0.22

8 months ago

1.0.21

8 months ago

1.0.2

8 months ago

1.0.1

8 months ago

1.0.0

8 months ago