quintoandar / node-logger

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

node-logger

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

Now with types

Environment variable

Name Description
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
JSON_LOGS Enable structured logs as JSON
SPAN_INFO Enable span context info on logs
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

Setting up

npm install github:quintoandar/node-logger#semver:~<latest-release-version>

Or add it on your package.json file like:

"dependencies": {
    "quintoandar-logger": "github:quintoandar/node-logger#semver:~<latest-release-version>
  },

See releases

Usage

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).

const logger = require('quintoandar-logger').getLogger(module);

const object = { id: 11, someInfo: 'someInfo' }
logger.info(`Some info about processing cool object with id ${object.id}`, object);
logger.warn(`Some warning about processing cool object with id ${object.id}`, object);
logger.error(`Some error while processing cool object with id ${object.id}`, object);

On the console it will be logged as a json:

[info] Some info about processing cool object with id 11 { extra: { id: 11, someInfo: 'someInfo' } }, module: 'path/to/my/file.js', timestamp: '2020-06-09T22:46:21.759Z'}

With pretty log enabled:

[info] Some info about processing cool object with id 11
{
  extra: {
        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.

Sentry

There is a method tha can start sentry for you.

const quintoandarLogger = require('quintoandar-logger');
const sentryParams = {} //there are some default values if object is empty
const logger = quintoandarLogger.startSentry(sentryParams).getLogger(module)

Tracer

On your code, you just need to intanciate the tracer within the logger library once.

const tracer = { currentRootSpan: { traceId: 'TRACER-ID' } };

const quintoandarLogger = require('quintoandar-logger');
const logger = quintoandarLogger.startSentry({}).setTracer(tracer).getLogger(module);

const object = { id: 11, someInfo: 'someInfo' }
logger.info(`Some info about processing cool object with id ${object.id}`, object } });

Obfuscation

For security reasons you can obfuscate sensitive information from the logs. You can also disable obfuscation based on the environment.

const quintoandarLogger = require('quintoandar-logger');
const logger = quintoandarLogger.setShouldObfuscate(true).getLogger(module);

Whenever you need to obfuscate log information call:

logger.info('Log message', obfuscate(payload))

then all values in the payload object will be obfuscated.

At your console, the logs now contain the trace-id identifier:

[info] Some info about processing cool object with id 11 [trace-id: TRACER-ID] { extra: { id: 11, someInfo: 'someInfo' } }, module: 'path/to/my/file.js', timestamp: '2020-06-09T22:46:21.759Z'}

Team

We have added a new concept that allows defining a team that owns the error.

If you create a custom error that contains a team property, this value will appear as team tag on sentry. It is useful in cross applications, like jaiminho, us-emails, markito, etc, because we can group errors by team

TODO

  • Create Express Middleware Request Logger

About


Languages

Language:JavaScript 100.0%