@wiredcraft/bunyan-logger-factory

    1.3.1 • Public • Published

    Why I create this libray

    From time to time, I found I have to create Bunyan instance with different streams(stdout/file/syslog) for different environments.

    How to use

    const loggerFactory = require('bunyan-logger-factory');
     
    const myLogger = loggerFactory.init({
      logName: 'my-logger-name',
      logStreams: [
        {
          streamType: process.env.LOGSTREAM,
          streamHost: process.env.LOGHOST,
          streamPort: process.env.LOGPORT,
          streamProto: process.env.LOGPROTO,
        }
      ],
    });

    Advanced usage

    You can also set the transformation to the logger.

    constant

    const loggerFactory = require('bunyan-logger-factory');
     
    const myLogger = loggerFactory.init({
      logName: 'my-logger-name',
      logStreams: [
        {
          streamType: process.env.LOGSTREAM,
          streamHost: process.env.LOGHOST,
          streamPort: process.env.LOGPORT,
          streamProto: process.env.LOGPROTO,
        }
      ],
      transform: [
        {
          constant: {
            ipsum: 'xxx',
          },
        },
      ],
    });

    This will add ipsum: xxx to each log created.

    clone

    const loggerFactory = require('bunyan-logger-factory');
     
    const myLogger = loggerFactory.init({
      logName: 'my-logger-name',
      logStreams: [
        {
          streamType: process.env.LOGSTREAM,
          streamHost: process.env.LOGHOST,
          streamPort: process.env.LOGPORT,
          streamProto: process.env.LOGPROTO,
        }
      ],
      transform: [
        {
          clone: {
            msg: 'message',
            time: 'timestamp',
          },
        },
      ],
    });

    This will generate two extra fields message and timestamp which have exact same value as msg and `time**.

    map

    const loggerFactory = require('bunyan-logger-factory');
    const fn = (v) => {return v + v;};
     
    const myLogger = loggerFactory.init({
      logName: 'my-logger-name',
      logStreams: [
        {
          streamType: process.env.LOGSTREAM,
          streamHost: process.env.LOGHOST,
          streamPort: process.env.LOGPORT,
          streamProto: process.env.LOGPROTO,
        }
      ],
      transform: [
        {
          map: {
            foo: fn,
          },
        },
      ],
    });

    This will populate the value of foo by calling the fn function.

    avoidChildTransform

    By default, the logger.child will inherit the transformation you set on the parent if there is a tranform in the init function, you can set avoidChildTransform to true to prevent that behavior.

    const logger = loggerFactory.init({
      logName: 'test-logger',
      logStreams: [
        {
          streamType: 'FILE',
          streamPath: filePath,
        }
      ],
      avoidChildTransform: true, // the transform only applies on the current logger instance.
      transform: [
        {
          constant: {
            ipsum: 'xxx',
          },
        },
      ],
    });
     

    multi streams

    It's possible to add multiple stream configs in logStreams.

    const logger = loggerFactory.init({
      logName: 'multiStream',
      logStreams: [
        {
          streamType: 'stdout'
        }, 
        {
          streamType: 'syslog',
          streamHost: 'localhost',
          streamPort: 514,
          streamProto: 'tcp'
        }
      ]
    });

    License

    MIT

    Keywords

    Install

    npm i @wiredcraft/bunyan-logger-factory

    DownloadsWeekly Downloads

    6

    Version

    1.3.1

    License

    MIT

    Unpacked Size

    9.32 kB

    Total Files

    5

    Last publish

    Collaborators

    • raohael_biland
    • yadong_zhang
    • kikiatwcl
    • liyachun01
    • echoyueyu
    • wiredcraft-bot
    • makara
    • xavierchow
    • wwayne
    • drakexiang
    • kaleocheng
    • flyingant
    • ccharlieli
    • illuminati1911
    • ilya_wiredcraft
    • rankun203
    • superbogy
    • miffyliye
    • lalawow
    • bsdelf
    • maxiaobao
    • shuaibird
    • juhas
    • cyrilselasi
    • xiao_t
    • agreatfool
    • haishanh
    • chopperlee2011
    • woodpig07
    • dowla
    • ivanbar
    • eronekogin
    • kuno
    • xpol
    • wangjianting
    • estemomento