Home

Awesome

Studio Log 2

👻 Log ndjson to an output stream, pretty print the output with emoji ✨

Note! Version 2 has significantly changed compared to the original announcement. Make sure to read the release notes for migration instructions!

Features

Usage

Log output is disabled by default to ensure logs don't get in the way when writing unit tests. Therefore you want to set this up as the first thing in your main:

// Sending raw ndJSON logs to stdout, e.g. in a server application:
const Stringify = require('@studio/ndjson/stringify');
require('@studio/log')
  .pipe(new Stringify())
  .pipe(process.stdout);

// Sending fancy formatted logs to stdout, e.g. in a command line tool:
const Format = require('@studio/log-format/fancy');
require('@studio/log')
  .pipe(new Format())
  .pipe(process.stdout);

// Sending logs to console.log, e.g. in a browser:
const Format = require('@studio/log-format/console');
require('@studio/log')
  .pipe(new Format())

Next, create a logger instance in a module and start writing logs:

const logger = require('@studio/log');

const log = logger('app');

exports.startService = function (port) {
  log.launch('my service', { port: 433 });
};

In the server example above, this output is produced:

{"ts":1486630378584,"ns":"app","topic":"launch","msg":"my service","data":{"port":433}}

Send your logs to the emojilog CLI for pretty printing:

❯ cat logs.ndjson | emojilog
09:52:58 🚀 app my service port=433

Install

❯ npm i @studio/log

Topics

Instead of log levels, this logger uses a set of topics. Unlike log levels, topics are not ordered by severity.

These topics are available: ok, warn, error, issue, ignore, input, output, send, receive, fetch, finish, launch, terminate, spawn, broadcast, disk, timing, money, numbers and wtf.

Topics and their mapping to emojis are defined in the Studio Log Topics project.

Log format

API

Creating a logger

Log instance API

Module API

Transform streams

Transform streams can be used to alter the data before passing it on. For example, Studio Log X is a Transform stream that can remove confidential data from the log data and Studio Log Format project implements the basic, fancy and console pretty printers.

Format transforms are node transform streams in writableObjectMode. Here is an example implementation, similar to the ndjson stringify transform:

const { Transform } = require('stream');

const ndjson = new Transform({
  writableObjectMode: true,

  transform(entry, enc, callback) {
    const str = JSON.stringify(entry);
    callback(null, `${str}\n`);
  }
});

Related modules

License

MIT

<div align="center">Made with ❤️ on 🌍</div>