npm package: @hackcapital/logger-nodejs
Description: It's a powerful logger built on top of Bunyan capable to write logs to stdout, file and send to LogStash out-of-the-box.
Author: Thales Pinheiro
Latest version: 1.1.7-ops

![node version status](https://img.shields.io/badge/NodeJS-9.x--LTS-green.svg?style=flat-square)

# logger

A powerful logger built on top of Bunyan capable to write logs to stdout, file and send to LogStash out-of-the-box.

## Usage

There's a full functional example at the `example/index.js` file.

## Features

- Buffer milions of messages
- Logs to Logstash, local FS and `stdout`
- `setTags()` allows to dynamically append to the original set tags

## Env Vars

- `process.env.NODE_DEBUG`: If specified, produces a lot of noise
- `process.env.LOGGER_PLUGINS_LOCALFILE_ENABLED`: If specified, enables the localfile plugin
- `process.env.LOGGER_PLUGINS_LOCALFILE_ROTATION`: Set log file rotation. Default: `1d`
- `process.env.LOGGER_PLUGINS_LOCALFILE_RETENTION`: Set log file retention. Default: `5`
- `process.env.LOGGER_PLUGINS_LOCALFILE_LOG_DIR_NAME`: Default directory name. Default: `logs`
- `process.env.LOGGER_PLUGINS_LOCALFILE_MIN_LEVEL`: Default: `debug`
- `process.env.LOGGER_PLUGINS_STDOUT_ENABLED`: If specified, enables the stdout plugin
- `process.env.LOGGER_PLUGINS_STDOUT_MIN_LEVEL`: Default: `debug`
- `process.env.LOGGER_PLUGINS_LOGSTASH_ENABLED`: If specified, enables the logstash plugin
- `process.env.LOGGER_PLUGINS_LOGSTASH_HOST`: Set the Logstash `host`
- `process.env.LOGGER_PLUGINS_LOGSTASH_PORT`: Set the Logstash `port`
- `process.env.LOGGER_PLUGINS_LOGSTASH_MIN_LEVEL`: Default: `info`

## Levels

The log levels in bunyan are as follows. The level descriptions are best practice opinions of the author.

- `fatal` (60): The service/app is going to stop or become unusable now. An operator should definitely look into this soon.
- `error` (50): Fatal for a particular request, but the service/app continues servicing other requests. An operator should look at this soon(ish).
- `warn` (40): A note on something that should probably be looked at by an operator eventually.
- `info` (30): Detail on regular operation.
- `debug` (20): Anything else, i.e. too verbose to be included in "info" level.
- `trace` (10): Logging from external libraries used by your app or very detailed application logging.

Setting a logger instance (or one of its streams) to a particular level implies that all log records at that level and above are logged. E.g. a logger set to level "info" will log records at level info and above (warn, error, fatal).

Source: [Bunyan Levels](https://github.com/trentm/node-bunyan#levels)

## Logger <-> Logstash Integration

Logstash can take actions based on levels and special tags. Follows the level vs action matrix:

| level | logstash | slack | email | pagerduty |
|-------|:--------:|------:|-------|-----------|
| debug |    no    |    no | no    | no        |
| info  |    yes   |    no | no    | no        |
| warn  |    yes   |    no | no    | no        |
| error |    yes   |   yes | no    | no        |
| fatal |    yes   |   yes | yes   | yes       |

_Notes: 

- _It requires Logstash to be already setup and integrated with these services_
- _Logger connects via `tcp` with Logstash_
- _If the Logstash server is not available, it will not block the Logger execution_

## Tips

- Do no concat strings
- Less is more, reduce noise
- If Logstash is routing to ES, pay attention to mapping conflicts
- Do not blindly log objects
- Do not directly logging errors -> mapping conflicts.
- Don't use these fields!
 - `message`
 - `timestamp`
 - `level`
 - `name`
 - `hostname`
 - `pid`
 - `time`
 - `msg`
 - `src`
 - Add `"outputCapture": "std"` to your VSCode launch.json to see logs in debug mode