@novigi/logger

1.0.0-0 • Public • Published

npm (scoped) NPM Statements Branches Functions Lines

@novigi/logger

Simple and minimalist logger for Node.js applications 🪵

🐿 Features

  • Supports log message templating → 'Hi ${name}!'
  • Color enabled log messages on terminal
  • Standard log levels → INFO, WARN, ERROR, DEBUG and TRACE
  • Append log lines to file or to a callback function

📦 Getting Started

  1. Install the dependency
npm install @novigi/logger
  1. Import the library
const lib = require('@novigi/logger');

📖 Documentation

logger

This library contains a collection of standard logging helper methods including logger.info,logger.error etc.

const logger = require('@novigi/logger');

logger.setLevel(level)

Using to set the loglevel to describe which message level will be logged by the relevant logger to standard output.

Kind: static method of logger

Param Type Description
level string required log level TRACE, DEBUG, INFO, WARN, ERROR or SILENT

Example

logger.setLevel('DEBUG')

logger.setCallback(cb)

Sets a callback function to call for each log line. This can use to redirect log messages to another destination (i.e., to a custom file). Passing a falsy value to the method will reset the callback.

Kind: static method of logger

Param Type Description
cb function When a new log line appends callback function will invoke.

Example

logger.setCallback((line) => { appendToCustomFile(line) })   // appending lines to custom method
logger.setCallback(null)                                     // reset the callback

logger.setColors(flag)

Sets log color preference to standard output

Kind: static method of logger

Param Type Description
flag boolean required log color setting

Example

logger.setColors(false) // disable printing logs with colors
logger.setColors(true)  // enable printing logs with colors

logger.setLogPath(path)

Sets log path to append log lines to a file

Kind: static method of logger

Param Type Description
path string required log file path

Example

logger.setLogPath('path/to/file.log')
logger.setLogPath(null)                 // Stop printing to a file

logger.info([...messages], context)

Log message at info level

Kind: static method of logger

Param Type Description
[...messages] string | object message arguments list accepts any number of log message strings and expects the last argument to be context object. If there are multiple log messages passed as arguments, method joins them with a space.
context object context variables for the templated messages

Example

var context = { ans: 'A', name: 'Timmy' }
logger.info('log message')                             // 2022-10-17 13:19:34.568 - INFO - log message
logger.info('Hi ${name}!', context)                    // 2022-10-17 13:19:34.568 - INFO - Hi Timmy!
logger.info('Answer: ${ans}.', 'Yes ${ans}!', context) // 2022-10-17 13:19:34.568 - INFO - Correct: A. Yes A!

logger.error([...messages], context)

Log message at error level

Kind: static method of logger

Param Type Description
[...messages] string | object message arguments list accepts any number of log message strings and expects the last argument to be context object. If there are multiple log messages passed as arguments, method joins them with a space.
context object context variables for the templated messages

Example

var context = { ans: 'A', name: 'Timmy' }
logger.error('log message')                             // 2022-10-17 13:19:34.568 - ERROR - log message
logger.error('Hi ${name}!', context)                    // 2022-10-17 13:19:34.568 - ERROR - Hi Timmy!
logger.error('Answer: ${ans}.', 'Yes ${ans}!', context) // 2022-10-17 13:19:34.568 - ERROR - Correct: A. Yes A!

logger.debug([...messages], context)

Log message at debug level

Kind: static method of logger

Param Type Description
[...messages] string | object message arguments list accepts any number of log message strings and expects the last argument to be context object. If there are multiple log messages passed as arguments, method joins them with a space.
context object context variables for the templated messages

Example

var context = { ans: 'A', name: 'Timmy' }
logger.debug('log message')                             // 2022-10-17 13:19:34.568 - DEBUG - log message
logger.debug('Hi ${name}!', context)                    // 2022-10-17 13:19:34.568 - DEBUG - Hi Timmy!
logger.debug('Answer: ${ans}.', 'Yes ${ans}!', context) // 2022-10-17 13:19:34.568 - DEBUG - Correct: A. Yes A!

logger.trace([...messages], context)

Log message at trace level

Kind: static method of logger

Param Type Description
[...messages] string | object message arguments list accepts any number of log message strings and expects the last argument to be context object. If there are multiple log messages passed as arguments, method joins them with a space.
context object context variables for the templated messages

Example

var context = { ans: 'A', name: 'Timmy' }
logger.trace('log message')                             // 2022-10-17 13:19:34.568 - TRACE - log message
logger.trace('Hi ${name}!', context)                    // 2022-10-17 13:19:34.568 - TRACE - Hi Timmy!
logger.trace('Answer: ${ans}.', 'Yes ${ans}!', context) // 2022-10-17 13:19:34.568 - TRACE - Correct: A. Yes A!

This is an auto generated file. Please don't make changes manually

Readme

Keywords

none

Package Sidebar

Install

npm i @novigi/logger

Weekly Downloads

1

Version

1.0.0-0

License

MIT

Unpacked Size

20.2 kB

Total Files

4

Last publish

Collaborators

  • buddhima
  • nov_user
  • madushak