uconsole

Hook, namespace, and shim console for the browser and node.js

Usage no npm install needed!

<script type="module">
  import uconsole from 'https://cdn.skypack.dev/uconsole';
</script>

README

uconsole Build Status NPM version Dependency Status

Hook, namespace, and shim console for the browser and node.js

  • Hook console methods while preserving line number
  • Create namespaced consoles and filter logging using a pattern
  • Shim console for environments where it is not provided.
  • Works in the browser or node.js

Installation

Install using npm:

npm install uconsole

Install using bower:

bower install uconsole

Using browser script tag and global (UMD wrapper):

// Available via window.uconsole
<script src="lib/uconsole.js"></script>

Usage

uconsole does not patch the global console on load. Instead, replace the global console manually using patch():

uconsole.patch(console);

API Reference

Members

uconsole.namespace(namespace)

Create a namespaced console.

Params

  • namespace String

Returns: console - returns namespaced console
Example

var debug = console.namespace('mymodule');

debug.log('another thing happened');

Filter namespaced console logs by providing a pattern using localStorage in the browser or the DEBUG environment variable for node.js:

localStorage.setItem('debug', 'mymodule');
DEBUG=mymodule

You can match multiple namespaces by separating them with a comma:

DEBUG=mymodule,anothermodule

Or use wildcards:

DEBUG=server:*

would match both server:request and server:response namespaces:

uconsole.patch(console)

Patch given console with uconsole methods.

Params

  • console Object

Returns: Object
Example

uconsole.patch(window.console);

uconsole.use(hook)

Hook console methods and run your own functions.

Params

  • hook function

Returns: console
Example

console.use(function (namespace, method, message) {
  // ... persist log message to elasticsearch ...
});

Contributing

Please submit all issues and pull requests to the alexmingoia/uconsole repository!

Tasks

List available tasks with gulp help.

Tests

Run tests using npm test or gulp test.

Support

If you have any problem or suggestion please open an issue here.