npm package: @novigi/logger
Description: Simple and minimalist logger for Node.js applications 🪵
Author: Novigi
Latest version: 1.0.0-0

![npm (scoped)](https://img.shields.io/npm/v/@novigi/logger) ![NPM](https://img.shields.io/npm/l/@novigi/logger) ![Statements](https://img.shields.io/badge/statements-98.73%25-brightgreen.svg?style=flat) ![Branches](https://img.shields.io/badge/branches-96.29%25-brightgreen.svg?style=flat) ![Functions](https://img.shields.io/badge/functions-94.11%25-brightgreen.svg?style=flat) ![Lines](https://img.shields.io/badge/lines-100%25-brightgreen.svg?style=flat)
# @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
```

2. Import the library
```
const lib = require('@novigi/logger');
```

## 📖 Documentation

<a name="module_logger"></a>

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

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


* [logger](#module_logger)
    * [.setLevel(level)](#module_logger.setLevel)
    * [.setCallback(cb)](#module_logger.setCallback)
    * [.setColors(flag)](#module_logger.setColors)
    * [.setLogPath(path)](#module_logger.setLogPath)
    * [.info([...messages], context)](#module_logger.info)
    * [.error([...messages], context)](#module_logger.error)
    * [.debug([...messages], context)](#module_logger.debug)
    * [.trace([...messages], context)](#module_logger.trace)

<a name="module_logger.setLevel"></a>

### 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 [<code>logger</code>](#module_logger)  

| Param | Type | Description |
| --- | --- | --- |
| level | <code>string</code> | required log level TRACE, DEBUG, INFO, WARN, ERROR or SILENT |

**Example**  
```js
logger.setLevel('DEBUG')
```
<a name="module_logger.setCallback"></a>

### 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 [<code>logger</code>](#module_logger)  

| Param | Type | Description |
| --- | --- | --- |
| cb | <code>function</code> | When a new log line appends callback function will invoke. |

**Example**  
```js
logger.setCallback((line) => { appendToCustomFile(line) })   // appending lines to custom method
logger.setCallback(null)                                     // reset the callback
```
<a name="module_logger.setColors"></a>

### logger.setColors(flag)
Sets log color preference to standard output

**Kind**: static method of [<code>logger</code>](#module_logger)  

| Param | Type | Description |
| --- | --- | --- |
| flag | <code>boolean</code> | required log color setting |

**Example**  
```js
logger.setColors(false) // disable printing logs with colors
logger.setColors(true)  // enable printing logs with colors
```
<a name="module_logger.setLogPath"></a>

### logger.setLogPath(path)
Sets log path to append log lines to a file

**Kind**: static method of [<code>logger</code>](#module_logger)  

| Param | Type | Description |
| --- | --- | --- |
| path | <code>string</code> | required log file path |

**Example**  
```js
logger.setLogPath('path/to/file.log')
logger.setLogPath(null)                 // Stop printing to a file
```
<a name="module_logger.info"></a>

### logger.info([...messages], context)
Log message at info level

**Kind**: static method of [<code>logger</code>](#module_logger)  

| Param | Type | Description |
| --- | --- | --- |
| [...messages] | <code>string</code> \| <code>object</code> | 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 | <code>object</code> | context variables for the templated messages |

**Example**  
```js
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!
```
<a name="module_logger.error"></a>

### logger.error([...messages], context)
Log message at error level

**Kind**: static method of [<code>logger</code>](#module_logger)  

| Param | Type | Description |
| --- | --- | --- |
| [...messages] | <code>string</code> \| <code>object</code> | 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 | <code>object</code> | context variables for the templated messages |

**Example**  
```js
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!
```
<a name="module_logger.debug"></a>

### logger.debug([...messages], context)
Log message at debug level

**Kind**: static method of [<code>logger</code>](#module_logger)  

| Param | Type | Description |
| --- | --- | --- |
| [...messages] | <code>string</code> \| <code>object</code> | 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 | <code>object</code> | context variables for the templated messages |

**Example**  
```js
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!
```
<a name="module_logger.trace"></a>

### logger.trace([...messages], context)
Log message at trace level

**Kind**: static method of [<code>logger</code>](#module_logger)  

| Param | Type | Description |
| --- | --- | --- |
| [...messages] | <code>string</code> \| <code>object</code> | 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 | <code>object</code> | context variables for the templated messages |

**Example**  
```js
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_