@jackdbd/tags-logger

@jackdbd/tags-logger

npm version Snyk Vulnerabilities for npm package

A logger inspired by how logging is implemented in Hapi.js.

Table of Contents
  • tags to pinpoint the log statements you are actually interested in.
  • optional validation of all log statements with Joi.
  • easy switching bewteen structured/unstructured logging.
npm install @jackdbd/tags-logger

When you write this:

import makeLog from '@jackdbd/tags-logger'

const log = makeLog()

log({
message: 'something not very important about foo and bar',
tags: ['debug', 'foo', 'bar']
})

log({
message: 'something of critical importance about baz',
tags: ['critical', 'baz']
})

You get this:

{
"severity": "DEBUG",
"message": "something not very important about foo and bar",
"tags": ["bar", "foo"],
"tag": {"bar": true, "foo": true}
}

{
"severity": "CRITICAL",
"message": "something of critical importance about baz",
"tags": ["baz"],
"tag": {"baz": true}
}

When you write this:

import makeLog from '@jackdbd/tags-logger'

const log = makeLog({
namespace: 'my-app/my-module'
})

// same log statements as above

You get this (but with colors):

my-app/my-module [🔍 bar,foo] something not very important about foo and bar +0ms

my-app/my-module [🔥 baz] something of critical importance about baz +0ms

Don't like emojis? Then write this:

import makeLog from '@jackdbd/tags-logger'

const log = makeLog({
namespace: 'my-app/my-module',
should_use_emoji_for_severity: false // <--
})

// same log statements as above

And get this (but with colors):

my-app/my-module [debug bar,foo] something not very important about foo and bar +0ms

my-app/my-module [critical baz] something of critical importance about baz +0ms
Environment variable Explanation
DEBUG You must set this environment variable if you want to use unstructured logging and see some output. This library delegates unstructured logging to debug.
Option Default Explanation
namespace undefined The namespace for unstructured logging. This option has no effect when using structured logging.
should_use_emoji_for_severity true Whether to use an emoji for the severity level, when using unstructured logging. This option has no effect when using structured logging.
should_validate_log_statements false when process.env.NODE_ENV === 'production'. Otherwise true Whether each log statement should be validated against a Joi schema.

API docs generated with TypeDoc