@cork-labs/monkfish-logger
v0.6.4
Published
Logger adapter for Node.js framework Monkfish
Downloads
30
Readme
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 aslog_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 aslog_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 aslog_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 aslog_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
- lintnpm run lint-fix
- lint and fixnpm test
- run all test suites and produce code coverage reportsnpm run test-u
- run unit testsnpm run test-i
- run integration testsnpm run coverage
- serve test coverage reportsnpm run clean
- delete all build artifactsnpm run build
- lint and testnpm run pub
- publish a patch version (usenpm-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.