Micro, powerful and customizable logger for the browser and node that provides log level mapping to the console.
- Lightweight. 511b minified and compressed with Brotli!
- No functions wrapping console. All stack traces are pure!
- No unnecessary bloat or fancy code. Only the core functionality!
- Use workers to asynchronously process logs as they come through.
- Import it or use it directly in the browser. All file formats are available.
- Use templates to make your output look how you want it to.
- SSR friendly.
- Types included.
Table of Contents
Support
Supports pretty much all browsers, IE9+ and Node 0.12+.
Important: if you want to use Workers in IE9+ then you will require a Promise polyfill.
If you'd like to support older versions of IE (<= 8) or something else ancient, you'll need to consider polyfills
for Promise
, Array.prototype.indexOf
and Array.prototype.splice
.
Installation
npm install whisp --save-dev
or yarn add whisp -D
You can also load it via the unpkg CDN
https://unpkg.com/whisp
will download the latest UMD bundle.
All formats (UMD, CJS and ESM) are available in the dist folder inside the package.
Quickstart
// name, level (optional - defaults to "debug")const whisp = 'my-app' 'debug' // Log away (info, debug, trace, warn, error or trace)// `log` is available as an alias to debugwhisp // Get current levelwhisp // Set level// Whisp will not produce output for any log level beneath the specified level// The order is: trace, debug (default), info, warn, error, silent// For example, if the level was set to `warn` then only calls to `warn` and `error` will be displayed in the terminalwhisp // Add workers to process logswhisp // Run a callback when all workers promises have resolvedwhisp { // Do something with the results} // Run a callback when any of the workers promises have rejectedwhisp { // Do something with the reason} // Add templates to customize outputwhisp // Use your templates// Note: `default` templates are special in that they are called automatically if set.whisp // Default template is used here if set. // Chain callswhisp
Workers
Callback: (level, ...args) => Promise
Workers are simple asynchronous callbacks that return a Promise. You can use them to do anything, for example writing to a file or sending logs to a server.
const whisp = 'my-app' // Set// Workers receive all the arguments you passed into the log call// For example if you call `whisp.debug('message1', 'message2')` then the worker will receive ('debug', 'message1', 'message2')whisp // Getwhisp // Run this callback each time all workers are complete// The `results` argument passed in is an array of all the results from each of the workers resolved promiseswhisp { // Do stuff here} // Run this callback if any of the workers promises rejectwhisp { // Do stuff here}
Templates
Callback: (level, ...args) => string
Templates are simple callbacks that modify the style of the logs and return a string.
You can also set a default
template to be called automatically when no other
template is used.
Important: this doesn't affect the workers input, it's only for styling your output.
const whisp = 'my-app' // Setwhisp // Getwhisp // Usewhisp // Set defaultwhisp // Use defaultwhisp
Example 1
See source code.
Example 2
See source code.
Example 3
See source code.