Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

strong-log-transformer

strongloop13.3mApache-2.02.1.0TypeScript support: definitely-typed

Stream transformer that prefixes lines with timestamps and other things.

logging, streams

readme

strong-log-transformer

A stream filter for performing common log stream transformations like timestamping and joining multi-line messages.

This is not a logger! But it may be useful for rolling your own logger.

Usage

Install strong-log-transformer and add it to your dependencies list.

npm install --save strong-log-transformer

CLI

When installed globally the sl-log-transformer CLI utility is exposed. It is primarily used for testing, but it can also be used as an alternative to awk or sed for jobs such as timestamping every line of another process's output. This can be useful for cron jobs, for example.

$ npm install -g strong-log-transformer
$ sl-log-tranformer --help
Usage: sl-log-transformer [options]

Stream transformer that prefixes lines with timestamps and other things.

OPTIONS:
   --format FORMAT        default: "text"
   --tag TAG              default: ""
   --mergeMultiline       default: off
   --timeStamp            default: off

Line Merging

In order to keep things flowing when line merging is enabled (disabled by default) there is a sliding 10ms timeout for flushing the buffer. This means that whitespace leading lines are only considered part of the previous line if they arrive within 10ms of the previous line, which should be reasonable considering the lines were likely written in the same write().

Example

Here's an example using the transformer to annotate log messages from cluster workers.

var cluster = require('cluster');

if (cluster.isMaster) {
  // Make sure workers get their own stdout/stderr streams
  cluster.setupMaster({silent: true});

  // require log transformer module
  var transformer = require('strong-log-transformer');

  // Following the 12-factor app model, we pipe to stdout, but we could easily
  // pipe to any other stream(s), such as a FileStream for a log file.

  // stdout is plain line-oriented logs, but we want to add timestamps
  var info = transformer({ timeStamp: true,
                           tag: 'INFO' });
  // stderr will only be used for strack traces on crash, which are multi-line
  var error = transformer({ timeStamp: true,
                            tag: 'ERROR',
                            mergeMultiline: true });

  // Each worker's stdout/stderr gets piped into our info and erro transformers
  cluster.on('fork', function(worker) {
    console.error('connecting worker');
    worker.process.stdout.pipe(info).pipe(process.stdout);
    worker.process.stderr.pipe(error).pipe(process.stdout);
  });

  //... cluster fork logic goes here ...
  cluster.fork();

} else {
  //... worker code here ...

  console.log('new worker, this line will be timestamped!');
  throw new Error('This will generate a multi-line message!');
}

When we run the example code as example.js we get:

$ node example.js
connecting worker
2014-06-08T18:54:00.920Z INFO new worker, this line will be timestamped!
2014-06-08T18:54:00.926Z ERROR /Users/ryan/work/strong-log-transformer/e.js:33\n    throw new Error('This will generate a multi-line message!');\n          ^
2014-06-08T18:54:00.926Z ERROR Error: This will generate a multi-line message!\n    at null._onTimeout (/Users/ryan/work/strong-log-transformer/e.js:33:11)\n    at Timer.listOnTimeout [as ontimeout] (timers.js:110:15)

changelog

2018-12-14, Version 2.1.0

  • src: replace byline with custom splitter (Ryan Graham)

  • test: add color tag tests (Ryan Graham)

  • test: ensure test fixtures use consistent newlines (Ryan Graham)

  • test: replicate shell tests in JS (Ryan Graham)

  • test: add tests for truncated utf8 sequences (Ryan Graham)

2018-08-11, Version 2.0.0

  • deps: remove dependency on moment (Ryan Graham)

  • deps: update minimist (Ryan Graham)

  • test: 100% test coverage :tada: (Ryan Graham)

  • test: cover all features (Ryan Graham)

  • test: add minimal json formatter test (Ryan Graham)

  • test: expand line-merge test coverage (Ryan Graham)

  • ci: add minimal Travis config (Ryan Graham)

  • deps: upgrade to tap@12 (Ryan Graham)

  • MAJOR: replace CLA with DCO (Ryan Graham)

  • MAJOR: relicense under Apache 2.0 license (Ryan Graham)

  • MAJOR: drop support for node < 4 (Ryan Graham)

2016-09-01, Version 1.0.6

  • package: update to byline 5.x (Sam Roberts)

  • Update URLs in CONTRIBUTING.md (#3) (Ryan Graham)

2016-05-05, Version 1.0.5

  • update copyright notices and license (Ryan Graham)

2016-04-11, Version 1.0.4

  • Refer to licenses with a link (Sam Roberts)

2015-10-01, Version 1.0.3

  • Use strongloop conventions for licensing (Sam Roberts)

  • test: generate coverage reports (Ryan Graham)

  • test: upgrade to tap@1 (Ryan Graham)

2015-06-03, Version 1.0.2

  • test: use official tap (Ryan Graham)

2015-01-12, Version 1.0.1

  • Fix bad CLA URL in CONTRIBUTING.md (Ryan Graham)

2014-10-24, Version 1.0.0

  • Update contribution guidelines (Ryan Graham)

  • test: more portable grep usage (Ryan Graham)

  • Don't let CI break tests if it modifies version (Ryan Graham)

2014-07-28, Version 0.2.1

  • Change to dual-license as Artistic/StrongLoop (Ryan Graham)

2014-07-23, Version 0.2.0

  • Hide tgzs so npm pack doesn't pack itself (Ryan Graham)

  • package: make description more concise (Ryan Graham)

  • doc: initial CLI description (Ryan Graham)

  • test: cli --help/--version (Ryan Graham)

  • Add CLI (Ryan Graham)

  • expose option defaults as Logger.DEFAULTS (Ryan Graham)

  • bin: strong-log-transformer to sl-log-transformer (Ryan Graham)

  • fix: make bin/t.js runnable (Ryan Graham)

2014-06-12, Version 0.1.0

  • Make transformer a duplex stream (Ryan Graham)

2014-06-11, Version 0.0.1

  • First release!