node package manager

timber

ūüĆ≤ Timber - Simple Node Structured Logging

CircleCI Coverage Status npm version ISC License

Overview

Timber solves node structured logging so you don't have to. Go from raw text logs to rich structured events in seconds. Spend more time focusing on your app and less time focusing on logging.

  1. Easy setup. - npm install --save timber, get setup in seconds.

  2. Automatically structures yours logs. - Third-party and in-app logs are all structured in a consistent format. See how it works below.

  3. Seamlessly integrates with popular libraries and frameworks. - Express, Winston, Morgan, etc. Automatically captures user context, HTTP context, and event data.

  4. Pairs with a modern structured-logging console. - Designed specifically for structured data, hosted, instantly usable, tail users, trace requests. Checkout the docs.

Installation

  1. In your shell run npm install --save timber

  2. Add the following lines to your entry file

const timber = require('timber');
 
// first we create a writeable stream that the logs will get sent to 
const transport = new timber.transports.HTTPS('your-timber-api-key');
 
// attach the stream to stdout 
timber.install(transport);

How it works

For example, Timber turns this raw text log:

Sent 200 in 45.ms

Into a rich http_server_response event.

Sent 200 in 45.2ms @metadata {"dt": "2017-02-02T01:33:21.154345Z", "level": "info", "context": {"http": {"method": "GET", "host": "timber.io", "path": "/path", "request_id": "abcd1234"}}, "event": {"http_response": {"status": 200, "time_ms": 45.2}}}

Notice that instead of completely replacing your log messages, Timber augments your logs with structured metadata. Turning them into rich events with context without sacrificing readability. And you have complete control over which data is captured.

Usage

Basic logging

No special API, Timber works directly with console.log:

console.log("My log message")
 
// => My log message @metadata {"level": "info", "context": {...}} 
 
console.info("My log message")
 
// => My log message @metadata {"level": "info", "context": {...}} 
 
console.warn("My log message")
 
// => My log message @metadata {"level": "warn", "context": {...}} 
 
console.error("My log message")
 
// => My log message @metadata {"level": "error", "context": {...}} 

Express middleware

If you're using express, you can use the timber middleware to automatically log all http request/response events

const express = require('express')
const timber = require('timber')
 
// first we create a writeable stream that your logs will get sent to 
const transport = new timber.transports.HTTPS('your-timber-api-key');
 
// attach the stream to stdout 
timber.install(transport);
 
// create our express app 
const app = express()
 
// attach the timber middleware 
app.use(timber.middlewares.express)
 
app.get('/', function (req, res) {
  res.send('hello, world!')
})
 
// Output: 
// => Started GET "/" @metadata {"level": "error", "context": {...}} 
// => Outgoing HTTP response 200 in 2ms @metadata {"level": "error", "context": {...}} 

Attaching to a custom stream

In most applications, you're going to want to attach the timber transport to stdout and stderr. This is why we supply the convenient timber.install(transport) function. However, it's possible to attach the transport to any writeable stream using the timber.attach() function!

const transport = timber.transports.HTTPS('timber-api-key')
 
// This is what the install() command is doing: 
timber.attach([transport], process.stdout)
timber.attach([transport], process.stderr)
// => This sends all logs from stdout directly to Timber 
 
// By default, those transports are "hijacking" the output of the original stream 
// If you want to send logs to timber and also preserve their output in stdout: 
timber.attach([transport, process.stdout], process.stdout)
timber.attach([transport, process.stderr], process.stderr)
// => This sends all logs from stdout to Timber and stdout 
 
// You can attach multiple unique transport streams to each stream: 
const file_transport = fs.createWriteStream("./output.log", {flags: "a"})
timber.attach([transport, file_transport], process.stdout)
// => This sends all logs from stdout to Timber and a custom log file 

Custom events

Custom events are currently not supported in the current version of the Node library. We are planning to add support for them soon!


Custom contexts

Custom contexts are currently not supported in the current version of the Node library. We are planning to add support for them soon!

Jibber-Jabber

Which events and contexts does Timber capture for me?

Out of the box you get everything in the Timber::Events namespace.

We also add context to every log, everything in the Timber::Contexts namespace. Context is structured data representing the current environment when the log line was written. It is included in every log line. Think of it like join data for your logs. It's how Timber is able to accomplished tailing users (context.user.id:1).


Won't this increase the size of my log data?

Yes, but it's marginal compared to the benefits of having rich structured log data. A few of things to note:

  1. Timber generally reduces the amount of logs your app generates, trading quality for quantity. It does so by providing options to consolidate request / response logs, template logs, and even silence logs that are not of value to you. (see configuration for examples).
  2. Timber lets you pick exactly which events and contexts you want. (see configuration for examples)
  3. Your logging provider should be compressing your data and charging you accordingly. Log data is notoriously repetitive, and the context Timber generates is repetitive. Because of compression we've seen somes apps only incur a ~15% increase in data size.

Finally, log what is useful to you. Quality over quantity certainly applies to logging.


What about my current log statements?

They'll continue to work as expected. Timber adheres to the default console interface. Your previous logger calls will work as they always do. Just import the timber library to your project and you're good to go!

In fact, traditional log statements for non-meaningful events, debug statements, etc, are encouraged. In cases where the data is meaningful, consider logging a custom event.