H4ad / nodejs-logging-proposal

3 stars 0 forks source link

Node.js Structured Logging Proposal

In this repository, you will find the "skeleton" of the proposal to introduce Structured Logging at Node.js.

This proposal is based on the Pino logger, which is a very fast logger for Node.js.

References

API

The API consists of the following methods:

getLogger(name: string, options?: LoggerOptions): Logger

This method creates a new logger instance, calling this method twice with the same name will return the same instance.

The options for this method are the following:

This method will return an instance of Logger, which consists of the following methods:

setGlobalOptions(options: LoggerGlobalOptions): void

This method sets the global options for all loggers.

Every time you call getLogger, you are creating a child from the global logger, so the attributes defined in the global options will be included in every log message.

The options for this method are the following:

This method also can be configured with the following environment variables:

buildTransport

This method allows you to create a new transport for pino, you can read more about it here.

In summary, creating a new transport will look like:

import { buildTransport } from 'node:logging';

export default async function (opts) {
  return buildTransport(async function (source) {
    for await (let obj of source) {
      console.log(obj)
    }
  })
}