0.3.0 • Published 5 years ago

@cork-labs/monkfish-adapter-logger v0.3.0

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

Monkfish Adapter Logger

Logger adapter for Node.js framework Monkfish.

Getting Started

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

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

See Monkfish for more information.

API

Logger

Logger.createLogger(config, data)

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

new Logger(name, streams): void

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, obj): 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: ... }

Develop

# lint and fix
npm run lint

# run test suite
npm test

# lint and test
npm run build

# serve test coverage
npm run coverage

# publish a minor version
node_modules/.bin/npm-bump minor

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) 2018 Cork Labs

0.3.0

5 years ago

0.2.1

5 years ago

0.2.0

5 years ago

0.0.2

6 years ago