0.1.0 • Published 7 years ago

bunyan-encoder v0.1.0

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

bunyan-encoder

A bunyan stream that allows to rename or remove bunyan core fields in output log records.

Installation

$ npm install bunyan-encoder

Usage

const bunyan = require('bunyan');
const bunyanEncoder = require('bunyan-encoder');

const logger = bunyan.createLogger({
  name: 'myapp',
  streams: [{stream: bunyanEncoder(), type: 'raw'}]
});

By default, bunyan-encoder will write records to stdout applying following mapping to the core fields:

Bunyan fieldOutput field
v-
levellogLevel
nameapplication
hostname-
pid-
timetimestamp
msgmessage
src-

Also, the log level value is converted from numeric to string representation:

Bunyan levelOutput level
10TRACE
20DEBUG
30INFO
40WARN
50ERROR
60ERROR

Customization

It is possible to override the default mapping of the core fields by providing a custom mapping function:

bunyanEncoder(rec => {
  return {
    severity: rec.level,
    event: rec.msg,
    time: rec.time,
    // ...
  }  
});

where rec is a raw bunyan record. The function should return a bundle that contains needed transformation of the core fields structure. Those core fields that are not included in the returned bundle will not appear in the output log record.

You can also override writing to stdout by providing another Node.js Stream object:

const socket = new net.Socket();
socket.connect(/* ... */);
bunyanEncoder(rec => {/* ... */}, socket);