🌲 Timber - Node.js logging
This NPM library is for logging in Node.js.
Here's how to get started:
Install the package directly from NPM:
npm i @timberio/node
In ES6/Typescript, import the
For CommonJS, require the package:
const Timber = ;
Creating a client
Simply pass your Timber.io organization API + source keys as parameters to a new
Timber instance (you can grab both from the Timber.io console):
This Node.js library extends
@timberio/core, which provides a simple API for logging, adding middleware and more.
Visit the relevant readme section for more info/how-to:
In addition to
.log|debug|info|warn|error() returning a Promise, the Node.js logger offers a
.pipe() function for piping successfully synchronized logs to any writable stream.
This makes it trivial to additionally save logs to a file, stream logs over the network, or interface with other loggers that accept streamed data.
Here's a simple example of saving logs to a
// Import the Node.js `fs` lib;// Import the Node.js Timber library;// Open a writable stream to `logs.txt`;// Create a new Timber instance, and pipe output to `logs.txt`;timber.pipelogsTxt;// When you next log, `logs.txt` will get a JSON string copytimber.log"This will also show up in logs.txt";
Streamed logs passed to your write stream's
.write() function will be JSON strings in the format of type
ITimberLog, and always contain exactly one complete log after it has been transformed by middleware and synced with Timber.io.
If you want to further process logs in your stream, remember to
JSON.parse(chunk.toString()) the written 'chunk', to turn it back into an
.pipe() will return the passed writable stream, allowing you to chain multiple
.pipe() operations or access any other stream function:
// Import a 'pass-through' stream, to prove it works;// This stream won't do anything, except copy input -> output;// Passing to multiple streams works...timber.pipepassThroughStream.pipelogsTxt;