Skip to content

Commit

Permalink
Make millisecond timer namespace specific and allow 'always enabled' …
Browse files Browse the repository at this point in the history
…output (#408)

* Make millisecond timer namespace specific

When debugging node apps, I find it much more useful for the
millisecond timer to be relative to last message from the same
namespace instead of any message. This is especially true when I'm
debugging across multiple libraries or multiple levels in the same
module and I'm interested in seeing all the messages but also need to
compare times from specific levels.

* Enable 'always enabled' output

Having to deal with 2 different logging mechanisms, one for debugging
and one for normal output, can be a nuisance.  It would be much easier to
always use the same facility and semantics for both.  This patch allows
an 'always enabled' namespace to be specified by appending a single '*'
to the namespace name.

var alwaysOn = require('debug')('normal:messages*');
alwaysOn('This will always display regardless of DEBUG');
  • Loading branch information
gtjoseph authored and TooTallNate committed Aug 8, 2017
1 parent 56ba594 commit be1b670
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -85,7 +85,7 @@ Then, run the program to be debugged as usual.

## Conventions

If you're using this in one or more of your libraries, you _should_ use the name of your library so that developers may toggle debugging as desired without guessing names. If you have more than one debuggers you _should_ prefix them with your library name and use ":" to separate features. For example "bodyParser" from Connect would then be "connect:bodyParser".
If you're using this in one or more of your libraries, you _should_ use the name of your library so that developers may toggle debugging as desired without guessing names. If you have more than one debuggers you _should_ prefix them with your library name and use ":" to separate features. For example "bodyParser" from Connect would then be "connect:bodyParser". If you append a "*" to the end of your name, it will always be enabled regardless of the setting of the DEBUG environment variable. You can then use it for normal output as well as debug output.

## Wildcards

Expand Down
11 changes: 5 additions & 6 deletions src/debug.js
Expand Up @@ -28,12 +28,6 @@ exports.skips = [];

exports.formatters = {};

/**
* Previous log timestamp.
*/

var prevTime;

/**
* Select a color.
* @param {String} namespace
Expand Down Expand Up @@ -62,6 +56,8 @@ function selectColor(namespace) {

function createDebug(namespace) {

var prevTime;

function debug() {
// disabled?
if (!debug.enabled) return;
Expand Down Expand Up @@ -174,6 +170,9 @@ function disable() {
*/

function enabled(name) {
if (name[name.length - 1] === '*') {
return true;
}
var i, len;
for (i = 0, len = exports.skips.length; i < len; i++) {
if (exports.skips[i].test(name)) {
Expand Down

0 comments on commit be1b670

Please sign in to comment.