LogBus

Put your logs on the bus and send them to their happy place.

Highlights


1st-Class Support for [De]Multiplexing

Scales Up

Scales Down

Example Use Cases

Compared to Others

Plugins

The plugin API is simple: return an object that defines some optional methods.

Here is an example "tap" plugin that lets events pass through when "on" and drops them when "off":

// Emit events based on results of `tap()`:
// - true to turn tap on
// - false to turn tap off
// Will be evaluated at start of pipeline and every `intervalSeconds`.

import {isFinite} from 'lodash'
import {LogEngine, LogEvent, PluginConfig} from '@tfks/logbus'

export default function Plugin(config: PluginConfig, logbus: LogEngine) {
  const intervalSeconds = config.intervalSeconds ? Number(config.intervalSeconds) : 60
  if (!isFinite(intervalSeconds) || intervalSeconds <= 0) {
    throw Error('invalid config: intervalSeconds must be > 0')
  }
  if (typeof config.tap !== 'function') {
    throw new Error('undefined config: tap')
  }
  const sandbox = {}
  const tap = config.tap.bind(sandbox)
  let on = false
  let timer: NodeJS.Timer

  async function start() {
    on = await tap()
    timer = setInterval(async () => { on = await tap() }, intervalSeconds * 1000)
    return {stage: logbus.stage}
  }

  function onInput(event: LogEvent) {
    if (on) logbus.event(event)
  }

  function stop() {
    if (timer) clearInterval(timer)
  }

  return {onInput, start, stop}
}