- Support for ES6 modules
- Is a fast JSON logger that supports pretty printing in development mode
- Has static methods, instance methods, child loggers, log file support etc.
- Supports both Browsers and NodeJs
Latest Release: @qfin/logger:0.0.23
npm install @qfin/logger
Documentation is available at https://quantofin.github.io/logger/
The Logger class
Type: Logger
options
object? Configuration options for the logger.options.base
object An object with base properties that will be printed with every log line. (optional, defaultnull
)options.name
string The name of the logger. It is a good practice to give names for easier identification of modules for example. (optional, defaultundefined
)options.level
string The log level. One of 'fatal', 'error', 'warn', 'info', 'debug', 'trace' or 'silent'. (optional, default'info'
)options.file
string The location of the log file. If not given, logs will be outputted to std out/err. (optional, defaultundefined
)options.prettyPrint
boolean? Whether to pretty print the logs or output json linesoptions.redact
(object | array)? The object props to redact from the output. As an array, the redact option specifies paths that should have their values redacted from any log output. Each path must be a string using a syntax which corresponds to JavaScript dot and bracket notation. If an object is supplied, three options can be specified:paths
,censor
andremove
.options.redact.paths
array Required. An array of paths.options.redact.censor
(string | function | undefined) Censor option will overwrite keys which are to be redacted. (optional, default'[Redacted]'
)options.redact.remove
boolean Instead of censoring the value, remove both the key and the value. (optional, defaultfalse
)
options.browser
object Config for browsers only. (optional, defaultundefined
)options.browser.write
(function | object)? Instead of passing log messages to console.log they can be passed to a supplied function. If write is an object, it can have methods that correspond to the levels. When a message is logged at a given level, the corresponding method is called. If a method isn't present, the logging falls back to using the console.
import Logger from '@qfin/logger';
// this creates a parent logger with name (my-app)
const logger = new Logger({ name: 'my-app', level: 'info' });
logger.info('Hello world from my awesome app!');
logger.debug('This will not be printed');
// --- somewhere else in the codebase
import Logger from '@qfin/logger';
// this creates a child logger with name (db) and it takes the log-level of the parent logger
const dbLogger = new Logger({ name: 'db' });
dbLogger.info('Successfully connected to Database: ', `${db.host}:${db.port}/${db.name}`);
// --- static logger usage
import Logger from '@qfin/logger';
// If logger is already initialized, then the parent logger reference will be used in this static call
// otherwise, a default parent logger will be initialized and then that will be used for logging
Logger.log('This is a static logger log');
Write a log with default or set level. The level can be set in the constructor parameter
arg0
object? See [Logger's log method for more info]Logger.log.args
...any? See [Logger's log method for more info]Logger.log.
Write a 'trace' level log, if the configured level allows for it.
arg0
object? See [Logger's log method for more info]Logger.log.args
...any? See [Logger's log method for more info]Logger.log.
Write a 'debug' level log, if the configured level allows for it.
arg0
object? See [Logger's log method for more info]Logger.log.args
...any? See [Logger's log method for more info]Logger.log.
Write a 'info' level log, if the configured level allows for it.
arg0
object? See [Logger's log method for more info]Logger.log.args
...any? See [Logger's log method for more info]Logger.log.
Write a 'warn' level log, if the configured level allows for it.
arg0
object? See [Logger's log method for more info]Logger.log.args
...any? See [Logger's log method for more info]Logger.log.
Write a 'error' level log, if the configured level allows for it.
arg0
object? See [Logger's log method for more info]Logger.log.args
...any? See [Logger's log method for more info]Logger.log.
Write a 'fatal' level log, if the configured level allows for it.
arg0
object? See [Logger's log method for more info]Logger.log.args
...any? See [Logger's log method for more info]Logger.log.
Static Method
Write a log with default or set level.
If the logger has already been initialized then the log level will be referred from the initialized logger, otherwise a new logger will be initialized and its default properties will be set and used.
-
arg0
object? An object can optionally be supplied as the first parameter. Each enumerable key and value of the mergingObject is copied in to the JSON log line. -
args
...any?args
are of type varargs and each of the args can take various forms as follows:- A message string can optionally be supplied as the first parameter, or as the second parameter after supplying a mergingObject. By default, the contents of the message parameter will be merged into the JSON log line under the msg key.- All arguments supplied after message are serialized and interpolated according to any supplied printf-style placeholders (%s, %d, %o|%O|%j) or else concatenated together with the message string to form the final output msg value for the JSON log line.
logger.info({MIX: {IN: true}})
// {"level":30,"time":1531254555820,"MIX":{"IN":true},"v":1}
logger.info('hello world')
// {"level":30,"time":1531257112193,"msg":"hello world","v":1}
logger.info('hello', 'world')
// {"level":30,"time":1531257618044,"msg":"hello world","v":1}
logger.info('hello', {worldly: 1})
// {"level":30,"time":1531257797727,"msg":"hello {\"worldly\":1}","v":1}
logger.info('%o hello', {worldly: 1})
// {"level":30,"time":1531257826880,"msg":"{\"worldly\":1} hello","v":1}
Static Method
Write a 'trace' level log, if the configured level allows for it.
If the logger has already been initialized then the log level will be referred from the initialized logger, otherwise a new logger will be initialized and its default properties will be set and used.
arg0
object? See [Logger's log method for more info]Logger.log.args
...any? See [Logger's log method for more info]Logger.log.
Static Method
Write a 'debug' level log, if the configured level allows for it.
If the logger has already been initialized then the log level will be referred from the initialized logger, otherwise a new logger will be initialized and its default properties will be set and used.
arg0
object? See [Logger's log method for more info]Logger.log.args
...any? See [Logger's log method for more info]Logger.log.
Static Method
Write a 'info' level log, if the configured level allows for it.
If the logger has already been initialized then the log level will be referred from the initialized logger, otherwise a new logger will be initialized and its default properties will be set and used.
arg0
object? See [Logger's log method for more info]Logger.log.args
...any? See [Logger's log method for more info]Logger.log.
Static Method
Write a 'warn' level log, if the configured level allows for it.
If the logger has already been initialized then the log level will be referred from the initialized logger, otherwise a new logger will be initialized and its default properties will be set and used.
arg0
object? See [Logger's log method for more info]Logger.log.args
...any? See [Logger's log method for more info]Logger.log.
Static Method
Write a 'error' level log, if the configured level allows for it.
If the logger has already been initialized then the log level will be referred from the initialized logger, otherwise a new logger will be initialized and its default properties will be set and used.
arg0
object? See [Logger's log method for more info]Logger.log.args
...any? See [Logger's log method for more info]Logger.log.
Static Method
Write a 'fatal' level log, if the configured level allows for it.
If the logger has already been initialized then the log level will be referred from the initialized logger, otherwise a new logger will be initialized and its default properties will be set and used.
arg0
object? See [Logger's log method for more info]Logger.log.args
...any? See [Logger's log method for more info]Logger.log.