@novigi/logger
v1.0.0-0
Published
Simple and minimalist logger for Node.js applications 🪵
Downloads
19
Readme
@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
andTRACE
- Append log lines to file or to a callback function
📦 Getting Started
- Install the dependency
npm install @novigi/logger
- 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