0.0.23 • Published 5 years ago

@qfin/logger v0.0.23

Weekly downloads
-
License
MIT
Repository
github
Last release
5 years ago

📦 Features

  • 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

💾 Install

Latest Release: @qfin/logger:0.0.23

npm install @qfin/logger

⚙ Module Documentation

Documentation is available at https://quantofin.github.io/logger/

Logger

The Logger class

Type: Logger

Parameters

  • options object? Configuration options for the logger.
    • options.base object An object with base properties that will be printed with every log line. (optional, default null)
    • options.name string The name of the logger. It is a good practice to give names for easier identification of modules for example. (optional, default undefined)
    • 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, default undefined)
    • options.prettyPrint boolean? Whether to pretty print the logs or output json lines
    • options.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 and remove.
      • 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, default false)
    • options.browser object Config for browsers only. (optional, default undefined)
      • 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.

Examples

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');

log

Write a log with default or set level. The level can be set in the constructor parameter

Parameters

trace

Write a 'trace' level log, if the configured level allows for it.

Parameters

debug

Write a 'debug' level log, if the configured level allows for it.

Parameters

info

Write a 'info' level log, if the configured level allows for it.

Parameters

warn

Write a 'warn' level log, if the configured level allows for it.

Parameters

error

Write a 'error' level log, if the configured level allows for it.

Parameters

fatal

Write a 'fatal' level log, if the configured level allows for it.

Parameters

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.

Parameters

  • 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.

Examples

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}

trace

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.

Parameters

debug

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.

Parameters

info

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.

Parameters

warn

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.

Parameters

error

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.

Parameters

fatal

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.

Parameters

0.0.23

5 years ago

0.0.22

5 years ago

0.0.21

5 years ago

0.0.19

5 years ago

0.0.18

5 years ago

0.0.16

5 years ago

0.0.14

5 years ago

0.0.11

5 years ago

0.0.10

5 years ago

0.0.9

5 years ago

0.0.8

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago

0.0.0

5 years ago

1.2.0

5 years ago

1.1.0

5 years ago

1.0.0

5 years ago