tracer-debug
This program is basically a wrapper for tracer, a logging library for node.js.
With tracer-debug
all output can be conveniently hidden at will, e.g. when running in production environments. For instance, you can choose to display output when NODE_ENV
is set and is different from production
(default behavior).
Different configuration options are available, although you'll be (hopefully) ok with the default ones:
var TracerDebug = logger = ;
Options
This program accepts the same options as tracer
(see https://www.npmjs.com/package/tracer), plus the following ones:
displayWhen
This option controls when should output messages be shown. To display output messages, you must specify any "truthy" value or an expression that evaluates to true
. Examples:
var TracerDebug = ; // This will show output messages when no environment is set.var myLogger = displayWhen: typeof processenvNODE_ENV === 'undefined'; // This will show output messages when running in a development environment.var otherLogger = displayWhen: processenvNODE_ENV === 'development'; myLogger;otherLogger;
By default, all messages are not shown when running in production, i.e. displayWhen
equals to typeof process.env.NODE_ENV === 'undefined'
.
stackTrace
If stackTrace
is set to true
, the full stack trace will be shown. Different levels of verbosity are possible, e.g. if stackTrace
is set to 1
, the first line of the stack trace will be shown after the output message; if stackTrace
is set to 2
, the first two lines of the stack trace will be shown after the output message; and so on. Examples:
var TracerDebug = ; // This will display the full stack trace (from caller file).var myLogger = stackTrace: true; // This will display the first 3 lines of the stack trace.var otherLogger = stackTrace: 3; myLogger;otherLogger;
Example
Do npm install
and create the file test.js
:
var TracerDebug = ; var logger = format: "logger/{{title}}: {{message}}" stackTrace: true singleton: true; logger; // Notice the stack trace. { logger;}; // The following examples are just to show the colored output.logger;logger;logger;logger; // Let's manipulate the original tracer instance.loggertransport;logger;logger; var logger2 = singleton: true ;// Notice that the output is the same as in the previous `logger` instance.logger2;
And then call it as NODE_ENV=debug nodejs test.js
or NODE_ENV=development nodejs test.js
to see the output.
Notice that if you call it as NODE_ENV=production nodejs test.js
or simply nodejs test.js
, no output is shown.