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

1.1.0 • Public • Published

@themost/json-logger

NPM

npm version

A simple JSON logger for Node.js applications.

Installation

npm i @themost/json-logger

Usage

const { JsonLogger } = require('@themost/json-logger');
const logger = new JsonLogger();
logger.info('Hello, world!');

Use JsonLogger class to create a new instance of JSON logger.

const { JsonLogger } = require('@themost/json-logger');
const logger = new JsonLogger({
    level: 'info',
    dateFormat: 'DD/MMM/YYYY:HH:mm:ss Z',
    format: 'json',
    stdout: process.stdout,
    stderr: process.stderr
});

The JsonLogger class accepts an options object with the following properties:

  • level: The minimum log level to log. The default value is info. Available log levels are info, warn, error, debug and verbose.
  • format: The log format. The default value is json. Available log formats are json and raw.
  • stdout: The standard output stream. The default value is process.stdout.
  • stderr: The standard error stream. The default value is process.stderr.
  • dateFormat: The date format to use. The default value is DD/MMM/YYYY:HH:mm:ss Z. The date format is based on the date-and-time library.

If the format property is set to json, the log message will be formatted as a JSON object. Otherwise, the log message will be formatted as a raw string. If you try to instantiate a JsonLogger without log level, the default log level will be info or debug based on the NODE_ENV environment variable when it is set to test or development respectively.

Methods

The JsonLogger provides the following methods:

  • info(message: string, ...args: any[]): Logs an informational message.
  • warn(message: string, ...args: any[]): Logs a warning message.
  • error(message: string, ...args: any[]): Logs an error message.
  • debug(message: string, ...args: any[]): Logs a debug message.
  • verbose(message: string, ...args: any[]): Logs a verbose message.
  • log(message: string, ...args: any[]): A shorthand for info method.

Readme

Keywords

none

Package Sidebar

Install

npm i @themost/json-logger

Weekly Downloads

2

Version

1.1.0

License

BSD-3-Clause

Unpacked Size

29.3 kB

Total Files

12

Last publish

Collaborators

  • themost