🌲 Timber - logging core
This is an NPM package that provides core logging functionality.
You typically wouldn't require this package directly, unless you're building a custom logger.
The Base class provides core features that are extended by loggers.
For example - you could create a custom logger that implements its own sync method, for getting data over to Timber.io
Logging to Timber is simple - just call the
.log() function with a string message:
// Simple log message (defaults to the 'info' log level)timber.log"Hello Timber!";// Or, add custom context keys to pass along with the logtimber.log"Once more, with context",;
There are four levels of logging, each corresponding to a function:
|.debug()||Debug - logs to report/diagnose system events||Currently logged in session object, during development|
|.info()||Info - data/events where no action is required; for information only||User successfully logged in|
|.warn()||Warning - advisory messages that something probably needs fixing, but not serious enough to cause an error||SQL query ran slower than expected|
|.error()||Error - something went wrong||Couldn't connect to database|
.log() logs at the 'info' level. You can use the above explicit log levels instead by calling the relevant function with your log message.
All log levels return a Promise that will resolve once the log has been synced with Timber.io:
// Will resolve when synced with Timber.io (or reject if there's an error)timber.log"some log message".then;
You can add your own middleware functions, which act as transforms on the ITimberLog log object.
This is useful for augmenting the log prior to syncing with Timber, or even pushing the log to another service.
Here's what a middleware function looks like:
;// In this example function, we'll add custom 'context' meta to the log// representing the currently logged in user.//// Note: a middleware function is any function that takes an `ITimberLog`// and returns a `Promise<ITimberLog>`
Then just attach to the Timber instance with
You can add any number of pipeline functions to your logger instance, and they'll run in order.
Middleware functions run before the final sync to Timber.io. Pipeline functions should return a
Promise<ITimberLog>, making it possible to augment logs with asynchronous data from external sources.
Note: If an exception is thrown anywhere in the pipeline chain, the log won't be synced. Wrap an async
try/catch block around your call to
.log|info|debug|warn|error() or tack on a
.catch() to ensure your errors are handled.
If you wish to remove middleware, pass in the original middleware function to
// `addCurrentUser` will no longer be used to transform logstimber.removeaddCurrentUser;
This will remove the middleware function from all future calls to
To re-add middleware, pass it to