0.2.0 • Published 4 years ago

@tailwindapp/logger v0.2.0

Weekly downloads
26
License
-
Repository
github
Last release
4 years ago

Tailwind Logger

The purpose of the Tailwind Logger is to provide a way to create well typed, structured, consistent logs.

This will allow services like Elastic Search or Datadog to properly index all fields on the log.

Installation

npm install @tailwindapp/logger

Usage

Service Name

To use the logger, you must initialize it with a service name.

const log = new LambdaLogger({
  service: 'image-service'
});

Levels

You can also initialize it with a logging level. The default level is 'warning'.

const log = new LambdaLogger({
  service: 'image-service',
  level: 'debug'
});

If you log an error that is the same or above the initially set level, it will write the log.

If it is below the initially set level it will not write the log.

For example, if we set the level to warning:

const log = new LambdaLogger({
  service: 'image-service',
  level: 'warning'
});

log.error('Error Message') // ✔️ this will be written
log.warning('Warning Message') // ✔️ this will written
log.info('Info Message') // ❌ this will not be written
log.debug('Debug Message') // ❌ this will not be written

If we set the level to debug then all logs will be written

const log = new LambdaLogger({
  service: 'image-service',
  level: 'warning'
});

log.error('Error Message') // ✔️ this will be written
log.warning('Warning Message') // ✔️ this will written
log.info('Info Message') // ✔️ this will written
log.debug('Debug Message') // ✔️ this will written

. The valid levels in descending order of priority:

  • 'emerg'
  • 'alert'
  • 'critical'
  • 'error'
  • 'warning'
  • 'notice'
  • 'info'
  • 'debug'

See RFC 5424.

Data, Metrics & Errors

You can also pass in additional data or metrics that are relevant to the logs.

You can also pass in an error object if you are catching a thrown error somewhere.

data must be in a string: string format

metrics must be in a string: number format.

error can be any error that you catch. It will be unwrapped for you if possible

This is so they can be properly auto-indexed by Elastic Search.

const log = new LambdaLogger({
  service: 'image-service',
  level: 'warning'
});

const loginError = new Error('Malformed login request');

log.error('Login Failed!', {
  data: {
    username: 'piesupplies'
  },
  error: loginError
});

log.warning('Post rate is very high', {
  metrics: {
    posts: 50
  },
  data: {
    email: 'contact@piesupplies.com'
  }
});

Metrics Only

Metrics can also be logged directly, with an optional prefix.

This allows you to send metrics to ES for monitoring, no matter what the log level of the logger is set to.

Log.metrics({
  'image_saved': 5,
  'image_failed': 0
}, 'pablo');

Specific Loggers

LambdaLogger

Currently the only logger we have is the LambdaLogger.

Once we need another logger it would be good to split this into a generic log class, and specific loggers that extend it.

const log = new LambdaLogger({
  level: 'warning'
});
0.2.0

4 years ago

0.1.6

4 years ago

0.1.5

4 years ago

0.1.4

4 years ago

0.1.3

4 years ago

0.1.2

4 years ago