Code Monkey home page Code Monkey logo

pino-abstract-transport's Introduction

pino-abstract-transport

npm version Build Status Coverage Status js-standard-style

Write Pino transports easily.

Install

npm i pino-abstract-transport

Usage

import build from 'pino-abstract-transport'

export default async function (opts) {
  return build(async function (source) {
    for await (let obj of source) {
      console.log(obj)
    }
  })
}

or in CommonJS and streams:

'use strict'

const build = require('pino-abstract-transport')

module.exports = function (opts) {
  return build(function (source) {
    source.on('data', function (obj) {
      console.log(obj)
    })
  })
}

Typescript usage

Install the type definitions for node. Make sure the major version of the type definitions matches the node version you are using.

Node 16

npm i -D @types/node@16

API

build(fn, opts) => Stream

Create a split2 instance and returns it. This same instance is also passed to the given function, which is called synchronously.

If opts.transform is true, pino-abstract-transform will wrap the split2 instance and the returned stream using duplexify, so they can be concatenated into multiple transports.

Events emitted

In addition to all events emitted by a Readable stream, it emits the following events:

  • unknown where an unparsable line is found, both the line and optional error is emitted.

Options

  • parse an option to change to data format passed to build function. When this option is set to lines, the data is passed as a string, otherwise the data is passed as an object. Default: undefined.

  • close(err, cb) a function that is called to shutdown the transport. It's called both on error and non-error shutdowns. It can also return a promise. In this case discard the the cb argument.

  • parseLine(line) a function that is used to parse line received from pino.

Example

custom parseLine

You can allow custom parseLine from users while providing a simple and safe default parseLine.

'use strict'

const build = require('pino-abstract-transport')

function defaultParseLine (line) {
  const obj = JSON.parse(line)
  // property foo will be added on each line
  obj.foo = 'bar'
  return obj
}

module.exports = function (opts) {
  const parseLine = typeof opts.parseLine === 'function' ? opts.parseLine : defaultParseLine
  return build(function (source) {
    source.on('data', function (obj) {
      console.log(obj)
    })
  }, {
    parseLine: parseLine
  })
}

Stream concatenation / pipeline

You can pipeline multiple transports:

const build = require('pino-abstract-transport')
const { Transform, pipeline } = require('stream')

function buildTransform () {
  return build(function (source) {
    return new Transform({
      objectMode: true,
      autoDestroy: true,
      transform (line, enc, cb) {
        line.service = 'bob'
        cb(null, JSON.stringify(line))
      }
    })
  }, { enablePipelining: true })
}

function buildDestination () {
  return build(function (source) {
    source.on('data', function (obj) {
      console.log(obj)
    })
  })
}

pipeline(process.stdin, buildTransform(), buildDestination(), function (err) {
  console.log('pipeline completed!', err)
})

License

MIT

pino-abstract-transport's People

Contributors

dependabot[bot] avatar mcollina avatar fdawgs avatar jsumners avatar jonathansamines avatar climba03003 avatar eomm avatar windupbird144 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.