0.6.4 • Published 5 years ago

@cork-labs/monkfish-logger v0.6.4

Weekly downloads
1
License
MIT
Repository
github
Last release
5 years ago

Monkfish Adapter Logger

Logger adapter for Node.js framework Monkfish.

Wraps bunyan logger library to facilitate injection, testing and configuration of logs.

See Monkfish for more information.

Getting Started

npm install --save @cork-labs/monkfish-logger

API

Logger

Logger.createLogger(config, data): Logger

Creates an instance of Logger from configuration.

  • config: object - of stream instances (OutBunyan, OutConsole, OutFile).
  • data: object (optional) - permanent log fields (tip: keep it flat).

Example config:

{
  name: 'my-app',
  streams: [
    // append to file (does not create directories, will fail if dirs are missings)
    {
      type: 'file',
      path: './logs/test.log',
      options: { ... } // override options for this stream only
    },
    {
      type: 'bunyan',
      bunyan: { /* Bunyan nativate options */ },
      // options: { ... } are ignored in bunyan stream
    },
    // for debug purposes only, do not use in production
    {
      type: 'console',
      // override options for this stream, e.g.: make console more human friendly
      options: {
        message: true,
        prettyJson: 2,
        dump: true
      }
    },
  ],
  // options for all streams
  options: {
    message: false, // log an extra line with a human readable message before the json payload
    prettyJson: 0, // format json payload (multi-line)
    dump: false // output error stack traces after payload (multi-line)
  }
}

Logger.flat(prefix, obj, ret): object

Flattens the object for logging, use on data objects before you log them to keep them flat.

Also accessible as instance method logger.flat(), see below for spec and examples.

new Logger(name, streams)

Creates an instance of Logger with the pre-configured stream instances.

  • name: string - added to the log fields as log_n
  • streams: array - of stream instances (OutBunyan, OutConsole, OutFile).
  • data: object (optional) - permanent log fields (tip: keep it flat).

logger.child(data): Logger

Returns a new logger, containing all the parent configuration and context, plus the provided optional data.

  • data: object (optional) - additional permanent log fields (tip: keep it flat).

logger.set(key, value): void

Adds key/value to permanent fields data or removes key when value not provided.

Avoid adding objects, keep it flat.

  • key: string
  • value: any (optional) - If value not provided (or undefined is provided), removes key from permanent fields.

logger.debug('message', data): void

Logs a debug level message, with adittional optional fields.

logger.info(message, data): void

Logs an info level message, with adittional optional fields.

  • message: string - logged as log_m.
  • data: object (optional) - additional fields for this log message only.

logger.warn('message', data): void

Logs an warning level message, with adittional optional fields.

  • message: string - logged as log_m.
  • data: object (optional) - additional fields for this log message only.

logger.error('message', data, err): void

Logs an error level message, with adittional optional fields, plus optional error (ideally a subclass of Error).

  • message: string - logged as log_m.
  • data: object (optional) - additional fields for this log message only.

If error is a subclass of Error, only the following key/values are added to the log.

  • err_name: err.constructor.name
  • err_msg: err.message
  • err_trace: err.stack

Otherwise, it is directly logged under the err key.

logger.flat(prefix, data, ret): object

Flattens the object for logging, use on data objects before you log them to keep them flat.

  • prefix: string - e.g. user.
  • data: object (optional) - additional fields for this log message only.
  • ret: object (optional) - if provided, fields are flattened into this object.
// flattening one object
const flatUser = logger.flat('todo', todo);
logger.info('todo.created', data);

// { log_m: 'todo.created', ..., todo_id: ..., todo_title: ... }

// flattening two objects into one
const data = logger.flat('article', article, {});
logger.flat('tag', tag, data)
logger.info('article.tags.created', data);

// { log_m: 'article.tags.created', ..., todo_id: ..., todo_title: ..., tag_id: ..., tag_title: ... }

Development

Install dependencies

npm install -g nodemon http-server

Code, test, publish

VSCode launchers:

  • test - run tests once

NPM scripts:

  • npm run dev - run tests (restarts when files saved)
  • npm run lint - lint
  • npm run lint-fix - lint and fix
  • npm test - run all test suites and produce code coverage reports
  • npm run test-u - run unit tests
  • npm run test-i - run integration tests
  • npm run coverage - serve test coverage reports
  • npm run clean - delete all build artifacts
  • npm run build - lint and test
  • npm run pub - publish a patch version (use npm-bump minor to publish a minor version)

Contributing

We'd love for you to contribute to our source code and to make it even better than it is today!

Check CONTRIBUTING before submitting issues and PRs.

Links

MIT License

Copyright (c) 2019 Cork Labs

0.6.4

5 years ago

0.6.2

5 years ago

0.6.2-pre

5 years ago

0.6.1-pre

5 years ago

0.6.0

5 years ago

0.5.1

5 years ago

0.5.0

5 years ago

0.4.0

5 years ago

0.3.1-pre

5 years ago