Skip to content

Instantly share code, notes, and snippets.

@shiftie
Last active October 12, 2018 10:24
Show Gist options
  • Save shiftie/ae77a916a79f1bf21b991df4bfb3f11f to your computer and use it in GitHub Desktop.
Save shiftie/ae77a916a79f1bf21b991df4bfb3f11f to your computer and use it in GitHub Desktop.
Visionmedia/Debug Logging Levels
/**
* Creates debug instances for different logging levels within your namespace &
* exposes functions to use them
* Usage:
* @example
* const debug = require('./debugLevels')('unicorn');
* debug.info('Hello %s', 'world'); // unicorn:info Hello world +0ms
* debug.error('Oops %O', new Error('damn')); // unicorn:error Oops Error: damn
* // unicorn:error at Object.<anonymous> (....)
*
* Original `debug` instance is accesible via `debug.debug`
* so you can still use `debug.debug.enable()` for instance.
*/
'use strict';
const debug = require('debug');
const colors = {
black : 0,
red : 1,
green : 2,
yellow : 3,
blue : 4,
magenta : 5,
cyan : 6,
white : 7
};
const levels = {
trace: colors.cyan,
log: '',
info: colors.green,
warn: colors.yellow,
error: colors.red,
};
exports = module.exports = namespace => {
const result = {debug};
const log = debug(namespace);
Object.keys(levels).forEach(levelName => {
result[levelName] = log.extend(levelName);
result[levelName].color = levels[levelName];
});
return result;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment