Skip to content

Commit

Permalink
Merge pull request #11 from quintoandar/feat/RELY-67-Update-Node-Logger
Browse files Browse the repository at this point in the history
[RELY-67] Update Node Logger
  • Loading branch information
MFrizo authored Jun 10, 2020
2 parents eb9fdd2 + c69007d commit f96592f
Show file tree
Hide file tree
Showing 5 changed files with 375 additions and 112 deletions.
62 changes: 54 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,29 +1,31 @@
# node-logger

Winston logger with sentry configuration included. Also show the file from which the log originated.
Winston logger with sentry configuration included. Also show the module, file path and caller from which the log originated.

## Environment variable

| Name | Description |
| -------------------- | -------------------------------------------- |
| SENTRY_DSN | Sentry's DNS |
| CONSOLE_LOG_LEVEL | The level of the logs displayed on the console (optional, defaults to info) |
| NODE_ENV | The application environment running (development, test, production) |
| PRETTY_LOGS | Enable colored logs with clean spacing |
| SENTRY_APP | The application's name |
| SENTRY_DSN | Sentry's DNS |
| SENTRY_ENVIRONMENT | The environment running (dev, staging, prod) |
| SENTRY_LOGGER_LEVEL | The level of the logs displayed at Sentry (optional, defaults to warn) |
| SENTRY_RELEASE | The current release |
| CONSOLE_LOG_LEVEL | The level of the logs displayed on the console (optional, defaults to info) |
| CAPTURE_UNHANDLED_REJECTIONS | A value (true or false) saying if you want these exceptions to be logged in you app |

## Setting up

```sh
npm install --save git+https://github.com/quintoandar/node-logger.git#<latest-release-version>
npm install github:quintoandar/node-logger#semver:~<latest-release-version>
```

Or add it on your `package.json` file like:

```sh
"dependencies": {
"quintoandar-logger": "git+https://github.com/quintoandar/node-logger.git#<latest-release-version>",
"quintoandar-logger": "github:quintoandar/node-logger#semver:~<latest-release-version>
},
```
Expand All @@ -33,6 +35,8 @@ Or add it on your `package.json` file like:
With info, warn and error messages the behaviour is the same. You are able to send the string (the info message) plus any other metadata you want as the second parameter, but be sure to add this data on a specific key named `extra` so that Sentry knows how to parse it and display it.
Since the Kibana application, by default, set a timestamp value to its logs, if the `NODE_ENV` env var was equals to `production` this field will be supressed at the logs. To enable it to show, its necessary to set a different value to this variable: (`development` or `test`).
```js
const logger = require('quintoandar-logger').getLogger(module);
Expand All @@ -43,13 +47,55 @@ logger.error(`Some error while processing cool object with id ${object.id}`, { e
```
On the console it will be logged as a json:
```sh
[info] Some info about processing cool object with id 11 { extra: { extra: { data: { id: 11, someInfo: 'someInfo' } } }, module: 'path/to/my/file.js', timestamp: '2020-06-09T22:46:21.759Z'}
```
With pretty log enabled:
```sh
{"level":"info","message":"Some info about processing cool object with id 10","extra_data":{"extra":{"data":{"id":"11","someInfo":"someInfo"}}},"logger_name":"path/to/my/file.js","timestamp":"2018-12-19T18:15:57.078Z"}
[info] Some info about processing cool object with id 11
{
extra: {
extra: {
data: {
id: 11,
someInfo: 'someInfo'
}
}
},
module: 'path/to/my/file.js',
timestamp: '2020-06-08T15:35:29.122Z'
}
```
And on Sentry the data on `extra` will be displayed under the field `Additional Data`.
### Tracer
If your project is using the honeycomb tracer library, you can include the tracer Id of the instance running to the logger.
On your code, you just need to intanciate the tracer within the logger library once.
```js
const tracer = { currentRootSpan: { traceId: 'TRACER-ID' } };
const quintoandarLogger = require('./src/main');
quintoandarLogger.setTracer(tracer);
const logger = quintoandarLogger.getLogger(module);
const object = { id: 11, someInfo: 'someInfo' }
logger.info(`Some info about processing cool object with id ${object.id}`, { extra: { data: object } });
```
At your console, the logs now contain the trace-id identifier:
```sh
[info] Some info about processing cool object with id 11 [trace-id: TRACER-ID] { extra: { extra: { data: { id: 11, someInfo: 'someInfo' } } }, module: 'path/to/my/file.js', timestamp: '2020-06-09T22:46:21.759Z'}
```
## TODO
- Create Express Middleware Request Logger

8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@
"description": "Winston logger with custom 5A configuration",
"main": "src/main.js",
"dependencies": {
"winston": "^3.1.0",
"winston-raven-sentry": "^2.0.0"
"@sentry/node": "^5.17.0",
"lodash": "^4.17.15",
"stack-trace": "0.0.10",
"util": "^0.12.3",
"winston": "^3.2.1",
"winston-transport": "^4.3.0"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
Expand Down
65 changes: 0 additions & 65 deletions src/customFormats.js

This file was deleted.

Loading

0 comments on commit f96592f

Please sign in to comment.