@jackdbd/tags-logger
TypeScript icon, indicating that this package has built-in type declarations

1.1.0 • Public • Published

@jackdbd/tags-logger

npm version Snyk Vulnerabilities for npm package

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

Table of Contents

Features

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

Installation

npm install @jackdbd/tags-logger

Usage

structured logging

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}
}

unstructured logging

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

Configuration

Environment variables

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.

Options

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

API docs generated with TypeDoc

Readme

Keywords

Package Sidebar

Install

npm i @jackdbd/tags-logger

Weekly Downloads

0

Version

1.1.0

License

MIT

Unpacked Size

45.7 kB

Total Files

50

Last publish

Collaborators

  • jackdbd