@themost/json-logger
v1.1.0
Published
A simple JSON logger for Node.js
Downloads
512
Readme
@themost/json-logger
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 isinfo
. Available log levels areinfo
,warn
,error
,debug
andverbose
.format
: The log format. The default value isjson
. Available log formats arejson
andraw
.stdout
: The standard output stream. The default value isprocess.stdout
.stderr
: The standard error stream. The default value isprocess.stderr
.dateFormat
: The date format to use. The default value isDD/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 forinfo
method.