Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

debug-logger

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

debug-logger - npm Package Compare versions

Comparing version 0.4.0 to 0.4.1

debug.log

108

debug-logger.js

@@ -43,3 +43,4 @@ 'use strict';

namespaceSuffix : ':trace',
level : 0
level : 0,
fd : 1 // currently only 1 (stdout) is supported. stderr is debug's standard
},

@@ -50,3 +51,4 @@ debug : {

namespaceSuffix : ':debug',
level : 1
level : 1,
fd : 1
},

@@ -57,3 +59,4 @@ log : {

namespaceSuffix : ':log',
level : 2
level : 2,
fd : 1
},

@@ -64,3 +67,4 @@ info : {

namespaceSuffix : ':info',
level : 3
level : 3,
fd : 1
},

@@ -86,2 +90,46 @@ warn : {

function time(label){
this.timeLabels[label] = process.hrtime();
}
function timeEnd(label, level){
level = level || 'log';
var diff = process.hrtime(this.timeLabels[label]);
var diffMs = (diff[0] * 1e9 + diff[1]) / 1e6;
this[level](label + ':', diffMs + 'ms');
return diffMs;
}
function dir(obj, options, level){
if(!level && this[options]){
level = options;
options = undefined;
}
options = options || exports.inspectOptions;
level = level || 'log';
this[level](util.inspect(obj, options));
}
function assert(expression){
if (!expression) {
var level = 'error';
var arr = Array.prototype.slice.call(arguments, 1);
if(this[arr[arr.length-1]]){
level = arr[arr.length-1];
arr = arr.slice(0, -1);
}
var assrt = require('assert');
var err = new assrt.AssertionError({
message: util.format.apply(this, arr),
actual: false,
expected: true,
operator: '==',
stackStartFunction: assert
});
this[level](err);
throw err;
}
}
var ensureNewlineEnabled = false;

@@ -218,6 +266,17 @@ var fd = parseInt(process.env.DEBUG_FD, 10) || 2;

function disableColors(loggerLevel, disable){
if(disable){
loggerLevel.color = '';
loggerLevel.reset = '';
loggerLevel.inspectionHighlight = '';
}
}
var debugInstances = {};
function getDebugInstance(namespace, color){
function getDebugInstance(namespace, color, fd){
if(!debugInstances[namespace]){
debugInstances[namespace] = vmDebug(namespace);
if(fd === 1 && isNaN(parseInt(process.env.DEBUG_FD))){
debugInstances[namespace].log = console.log.bind(console);
}
if(!isNaN(color)){

@@ -233,16 +292,23 @@ debugInstances[namespace].color = color;

var levels = exports.levels;
var debugLoggers = { 'default': getDebugInstance.bind(this, namespace) };
var debugLoggers = { 'default': getDebugInstance.bind(this, namespace, '') };
var logger = {};
var logger = function(){
debugLoggers['default']().apply(this, arguments);
};
logger.logLevel = getLogLevel(namespace);
logger.timeLabels = {};
logger.time = time;
logger.timeEnd = timeEnd;
logger.dir = dir;
logger.assert = assert;
Object.keys(levels).forEach(function(levelName) {
var loggerNamespaceSuffix = levels[levelName].namespaceSuffix ? levels[levelName].namespaceSuffix : 'default';
if(!debugLoggers[loggerNamespaceSuffix]){
debugLoggers[loggerNamespaceSuffix] = getDebugInstance.bind(this, namespace + loggerNamespaceSuffix, levels[levelName].color);
debugLoggers[loggerNamespaceSuffix] = getDebugInstance.bind(this, namespace + loggerNamespaceSuffix, levels[levelName].color, levels[levelName].fd);
}
var levelLogger = debugLoggers[loggerNamespaceSuffix];
var color = vmDebug.useColors ? getForeColor(levels[levelName].color) : '';
var reset = vmDebug.useColors ? exports.colorReset : '';
var inspectionHighlight = vmDebug.useColors ? exports.styles.underline : '';
var initialized = false;

@@ -255,4 +321,9 @@ function logFn() {

if(!initialized){
initialized = true;
disableColors(logger[levelName], !levelLog.useColors);
}
if (isString(arguments[0]) && hasFormattingElements(arguments[0])){
arguments[0] = color + levels[levelName].prefix + reset + arguments[0];
arguments[0] = logger[levelName].color + levels[levelName].prefix + logger[levelName].reset + arguments[0];
return levelLog.apply(this, arguments);

@@ -274,10 +345,10 @@ }

if (param.length > 1) {
var highlightStack = param[1].indexOf('Stack') >= 0 ? color : '';
var highlightStack = param[1].indexOf('Stack') >= 0 ? logger[levelName].color : '';
inspections += '\n' +
inspectionHighlight + '___' + param[1] + ' #' + n++ + '___' + reset +'\n' +
highlightStack + param[2] + reset;
logger[levelName].inspectionHighlight + '___' + param[1] + ' #' + n++ + '___' + logger[levelName].reset +'\n' +
highlightStack + param[2] + logger[levelName].reset;
}
};
levelLog(color + levels[levelName].prefix + reset + message + inspections);
levelLog(logger[levelName].color + levels[levelName].prefix + logger[levelName].reset + message + inspections);
};

@@ -292,6 +363,9 @@

logger[levelName] = ensureNewlineEnabled ? logNewlineFn : logFn;
logger[levelName] = ensureNewlineEnabled ? logNewlineFn : logFn;
logger[levelName].level = levels[levelName].level;
logger[levelName].logger = function(){ return levelLogger(); };
logger[levelName].enabled = function(){ return logger.logLevel <= logger[levelName].level && levelLogger().enabled; };
logger[levelName].color = getForeColor(levels[levelName].color);
logger[levelName].reset = exports.colorReset;
logger[levelName].inspectionHighlight = exports.styles.underline;
});

@@ -298,0 +372,0 @@

8

examples/index.js

@@ -40,5 +40,7 @@ var log = require('..')('myapp');

console.log();
log.debug.logger()("the debug instance of debug, using 'myapp:debug' namespace");
log('the root/default debug instance');
log.info.logger()("the info instance of debug, using 'myapp:info' namespace");
// debugLogger.debug references the debug module, e.g.: debugLogger.debug == require('debug')
var debug = debugLogger.debug('myapp:visionmedia');
debug('Nothing tastes better than the original!');
debug('nothing tastes better than the original!');

@@ -82,2 +84,3 @@

console.log();

@@ -100,2 +103,1 @@ var alwaysPrintAtStartOfLineLog = debugLogger.config({ ensureNewline: true })('myapp');

{
"name": "debug-logger",
"version": "0.4.0",
"version": "0.4.1",
"description": "A wrapper for visionmedia/debug logger, adding levels and colored output",

@@ -15,3 +15,3 @@ "main": "debug-logger.js",

"colors",
"colours",
"console",
"log",

@@ -18,0 +18,0 @@ "info",

@@ -12,2 +12,5 @@ [![npm version](https://badge.fury.io/js/debug-logger.svg)](http://badge.fury.io/js/debug-logger)

`debug-logger` uses the same syntax as [node.js console](https://nodejs.org/api/console.html) so you can use it as drop in replacement.
Check and run [examples/console.parity.js](https://github.com/appscot/debug-logger/blob/master/examples/console.parity.js) for more details.
At AppsCot we use `debug-logger` in [waterline-orientdb](https://github.com/appscot/waterline-orientdb).

@@ -109,2 +112,62 @@

### Measure code execution time
```javascript
log.time('100-elements');
for (var i = 0; i < 100; i++) {
;
}
log.timeEnd('100-elements');
log.time('setTimeout');
setTimeout(function(){
var diff = log.timeEnd('setTimeout', 'debug');
log.trace('log.timeEnd returns diff so it can be reused:', diff);
}, 262);
```
![code time](https://raw.githubusercontent.com/wiki/appscot/debug-logger/time.png)
### Inspect object
```javascript
log.dir({ foo: { bar: 1 } });
log.dir({ foo: { bar: 1 } }, { depth: 0 }, 'warn');
```
![dir inspect](https://raw.githubusercontent.com/wiki/appscot/debug-logger/dir.png)
### Assert condition
```javascript
log.assert(1 == 0);
// More elaborate example
var log = require('..').config({
levels: {
fatal: {
color : 5, //magenta
prefix : '',
namespaceSuffix : ':fatal',
level : 6
}
}
})('myapp');
log.assert(1 == 0, 'testing %s %d', 'log.assert', 666, 'fatal');
```
![assert](https://raw.githubusercontent.com/wiki/appscot/debug-logger/assert.png)
### stderr vs stdout
By default trace, debug, log and info output to stdout while warn and error output to stderr.
This is configurable, [example](https://github.com/appscot/debug-logger/blob/master/examples/stdout_stderr.js):
```javascript
var log = require('debug')('myapp');
log.trace("goes to stdout!");
log.debug("goes to stdout!");
log.log("goes to stdout!");
log.info("goes to stdout!");
log.warn("goes to stderr");
log.error("goes to stderr");
// outputting only to stdout
var stdout = require('debug').config({ levels: { warn: { fd: 1 }, error: { fd: 1 } } })('stdoutapp');
stdout.warn("goes to stdout!");
stdout.error("goes to stdout!");
```
### Filter by log level (instead of namespace)

@@ -137,4 +200,10 @@ ```sh

#### `log([message][, ...])`
Outputs the message using the root/default `debug` instance, without the level suffix. Example:
```
myapp I'm a root/default debug instance output +0ms
```
#### `log[level].logger()`
Returns the default debug instance used by `level`.
Returns the default `debug` instance used by `level`.

@@ -144,2 +213,17 @@ #### `log[level].enabled()`

#### `log.time(label)`
Mark a time.
#### `log.timeEnd(label[, level])`
Finish timer, record output. `level` will determine the logger used to output the result (defaults to 'log').
Return duration in ms.
#### `log.dir(obj[, options][, level])`
Uses util.inspect on obj and prints resulting string to the appropriate logger. This function bypasses any custom inspect() function on obj. An optional [options object](https://nodejs.org/api/console.html#console_console_dir_obj_options) may be passed that alters certain aspects of the formatted string.
`level` will determine the logger used to output the result (defaults to 'log').
#### `log.assert(value[, message][, ...][, level])`
Similar to [console.assert()](https://nodejs.org/api/console.html#console_console_assert_value_message).
Additionally it outputs the error using the appropriate logger set by `level` (defaults to 'error').
### Module

@@ -146,0 +230,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc