How to use the stdSerializers function from bunyan

Find comprehensive JavaScript bunyan.stdSerializers code examples handpicked from public code repositorys.

bunyan.stdSerializers is an object containing a set of standard serializers used for common log fields in Bunyan logging.

24
25
26
27
28
29
30
31
32
33
34
35
    streams: [{


        path: "./logs/error.log",
        level: "error",
    }],
    serializers: bunyan.stdSerializers
});


require("mongoose-pagination");

fork icon0
star icon19
watch icon3

215
216
217
218
219
220
221
222
223
224

setupLogger() {
  const logParams = {
    serializers: {
      req: Logger.stdSerializers.req,
      res: Logger.stdSerializers.res,
      err: Logger.stdSerializers.err
    },
    level: this.config.logLevel
  };
fork icon0
star icon0
watch icon2

+ 203 other calls in file

How does bunyan.stdSerializers work?

bunyan.stdSerializers is an object containing a set of predefined serializers that can be used to standardize the serialization of certain types of objects when logging with the Bunyan logger, including req, res, err, and clientReq. When using these serializers, the resulting log output will contain a consistent set of fields with defined semantics for each of these objects.

150
151
152
153
154
155
156
157
158
159
    delete options.consoleOff;
}
//
// Init serializers (just the standard one for now, for errors)
//
options.serializers = bunyan.stdSerializers;
// Overwrite with our own for request
// see ~/gv/node_modules/gv-logger/node_modules/bunyan/lib/bunyan.js
options['serializers']['req'] = function (req) {
    if (!req) {
fork icon0
star icon0
watch icon2

+ 31 other calls in file

76
77
78
79
80
81
82
83
84

if (!logger) {
    opts.name = (opts.name || app.settings.shortname || app.settings.name || app.settings.title || 'express');
    opts.serializers = opts.serializers || {};
    opts.serializers.req = opts.serializers.req || bunyan.stdSerializers.req;
    opts.serializers.res = opts.serializers.res || bunyan.stdSerializers.res;
    err && (opts.serializers.err = opts.serializers.err || bunyan.stdSerializers.err);
    logger = bunyan.createLogger(opts);
}
fork icon0
star icon0
watch icon1

+ 23 other calls in file

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
const bunyan = require("bunyan");

const logger = bunyan.createLogger({
  name: "myapp",
  streams: [
    {
      level: "info",
      stream: process.stdout,
    },
    {
      level: "error",
      path: "/var/log/myapp-error.log",
      type: "rotating-file",
      period: "1d",
      count: 7,
      serializers: {
        err: bunyan.stdSerializers.err,
      },
    },
  ],
});

logger.error(new Error("Something went wrong!"));

This will log the error object to the specified error log file with all of its properties serialized.

25
26
27
28
29
30
31
32
33
34
    );
    logStreams.push(Config.logging.rotatingFile);
}

const serializers = {
    err: bunyan.stdSerializers.err, //  handle 'err' fields with stack/etc.
};

//  try to remove sensitive info by default, e.g. 'password' fields
['formData', 'formValue'].forEach(keyName => {
fork icon0
star icon0
watch icon0

+ 3 other calls in file

27
28
29
30
31
32
33
34
35
36

logger = bunyan.createLogger({
    name: config.PROJECT_NAME,
    streams: streams,
    serializers: {
        req: bunyan.stdSerializers.req,
        res: bunyan.stdSerializers.res,
        err: bunyan.stdSerializers.err
    }
});
fork icon0
star icon0
watch icon0

+ 2 other calls in file