Socket
Socket
Sign inDemoInstall

console-log-level

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

console-log-level - npm Package Compare versions

Comparing version 1.1.0 to 1.1.1

40

index.js

@@ -1,38 +0,40 @@

'use strict';
'use strict'
var util = require('util');
var util = require('util')
var levels = ['debug', 'info', 'warn', 'error', 'fatal'];
var levels = ['debug', 'info', 'warn', 'error', 'fatal']
module.exports = function (opts) {
opts = opts || {};
opts.level = opts.level || 'info';
opts = opts || {}
opts.level = opts.level || 'info'
var logger = {};
var logger = {}
var shouldLog = function (level) {
return levels.indexOf(level) >= levels.indexOf(opts.level);
};
return levels.indexOf(level) >= levels.indexOf(opts.level)
}
levels.forEach(function (level) {
logger[level] = function () {
if (!shouldLog(level)) return;
if (!shouldLog(level)) return
var prefix = opts.prefix;
var prefix = opts.prefix
var normalizedLevel
switch (level) {
case 'debug': level = 'info'; break;
case 'fatal': level = 'error'; break;
case 'debug': normalizedLevel = 'info'; break
case 'fatal': normalizedLevel = 'error'; break
default: normalizedLevel = level
}
if (prefix) {
if (typeof prefix === 'function') prefix = prefix();
arguments[0] = util.format(prefix, arguments[0]);
if (typeof prefix === 'function') prefix = prefix()
arguments[0] = util.format(prefix, arguments[0])
}
console[level].apply(console, arguments);
};
});
console[normalizedLevel].apply(console, arguments)
}
})
return logger;
};
return logger
}
{
"name": "console-log-level",
"version": "1.1.0",
"version": "1.1.1",
"description": "The most simple logger imaginable",
"main": "index.js",
"scripts": {
"test": "node test.js"
"test": "standard && tape test.js"
},

@@ -30,8 +30,9 @@ "repository": {

"devDependencies": {
"standard": "^5.2.2",
"tape": "^3.0.3"
},
"coordinates": [
55.687684,
12.5955911
55.68783401556421,
12.595827270202676
]
}

@@ -1,174 +0,172 @@

'use strict';
'use strict'
var test = require('tape');
var Logger = require('./');
var test = require('tape')
var Logger = require('./')
var origInfo = console.info;
var origWarn = console.warn;
var origError = console.error;
var origInfo = console.info
var origWarn = console.warn
var origError = console.error
var mock = function () {
console.info = function () { this.infoCalled = true; };
console.warn = function () { this.warnCalled = true; };
console.error = function () { this.errorCalled = true; };
};
console.info = function () { this.infoCalled = true }
console.warn = function () { this.warnCalled = true }
console.error = function () { this.errorCalled = true }
}
var restore = function () {
delete console.infoCalled;
delete console.warnCalled;
delete console.errorCalled;
console.info = origInfo;
console.warn = origWarn;
console.error = origError;
};
delete console.infoCalled
delete console.warnCalled
delete console.errorCalled
console.info = origInfo
console.warn = origWarn
console.error = origError
}
var spyOn = function (method, spy) {
console['~' + method] = console[method];
console[method] = function () {
spy.apply(console, arguments);
};
};
console['~' + method] = console[method]
console[method] = spy
}
var spyOff = function (method) {
console[method] = console['~' + method];
delete console['~' + method];
};
console[method] = console['~' + method]
delete console['~' + method]
}
test('log all', function (t) {
var logger = Logger({ level: 'debug' });
var logger = Logger({ level: 'debug' })
mock();
logger.debug('foo');
t.ok(console.infoCalled, 'info called');
t.notOk(console.warnCalled, 'warn not called');
t.notOk(console.errorCalled, 'error not called');
restore();
mock()
logger.debug('foo')
t.ok(console.infoCalled, 'info called')
t.notOk(console.warnCalled, 'warn not called')
t.notOk(console.errorCalled, 'error not called')
restore()
mock();
logger.info('foo');
t.ok(console.infoCalled, 'info called');
t.notOk(console.warnCalled, 'warn not called');
t.notOk(console.errorCalled, 'error not called');
restore();
mock()
logger.info('foo')
t.ok(console.infoCalled, 'info called')
t.notOk(console.warnCalled, 'warn not called')
t.notOk(console.errorCalled, 'error not called')
restore()
mock();
logger.warn('foo');
t.notOk(console.infoCalled, 'info not called');
t.ok(console.warnCalled, 'warn called');
t.notOk(console.errorCalled, 'error not called');
restore();
mock()
logger.warn('foo')
t.notOk(console.infoCalled, 'info not called')
t.ok(console.warnCalled, 'warn called')
t.notOk(console.errorCalled, 'error not called')
restore()
mock();
logger.error('foo');
t.notOk(console.infoCalled, 'info not called');
t.notOk(console.warnCalled, 'warn not called');
t.ok(console.errorCalled, 'error called');
restore();
mock()
logger.error('foo')
t.notOk(console.infoCalled, 'info not called')
t.notOk(console.warnCalled, 'warn not called')
t.ok(console.errorCalled, 'error called')
restore()
mock();
logger.fatal('foo');
t.notOk(console.infoCalled, 'info not called');
t.notOk(console.warnCalled, 'warn not called');
t.ok(console.errorCalled, 'error called');
restore();
mock()
logger.fatal('foo')
t.notOk(console.infoCalled, 'info not called')
t.notOk(console.warnCalled, 'warn not called')
t.ok(console.errorCalled, 'error called')
restore()
t.end();
});
t.end()
})
test('default level', function (t) {
var logger = Logger();
var logger = Logger()
mock();
logger.debug('foo');
t.notOk(console.infoCalled, 'info not called');
t.notOk(console.warnCalled, 'warn not called');
t.notOk(console.errorCalled, 'error not called');
restore();
mock()
logger.debug('foo')
t.notOk(console.infoCalled, 'info not called')
t.notOk(console.warnCalled, 'warn not called')
t.notOk(console.errorCalled, 'error not called')
restore()
mock();
logger.info('foo');
t.ok(console.infoCalled, 'info called');
t.notOk(console.warnCalled, 'warn not called');
t.notOk(console.errorCalled, 'error not called');
restore();
mock()
logger.info('foo')
t.ok(console.infoCalled, 'info called')
t.notOk(console.warnCalled, 'warn not called')
t.notOk(console.errorCalled, 'error not called')
restore()
t.end();
});
t.end()
})
test('set custom level', function (t) {
var logger = Logger({ level: 'warn' });
var logger = Logger({ level: 'warn' })
mock();
logger.info('foo');
t.notOk(console.infoCalled, 'info not called');
t.notOk(console.warnCalled, 'warn not called');
t.notOk(console.errorCalled, 'error not called');
restore();
mock()
logger.info('foo')
t.notOk(console.infoCalled, 'info not called')
t.notOk(console.warnCalled, 'warn not called')
t.notOk(console.errorCalled, 'error not called')
restore()
mock();
logger.warn('foo');
t.notOk(console.infoCalled, 'info not called');
t.ok(console.warnCalled, 'warn called');
t.notOk(console.errorCalled, 'error not called');
restore();
mock()
logger.warn('foo')
t.notOk(console.infoCalled, 'info not called')
t.ok(console.warnCalled, 'warn called')
t.notOk(console.errorCalled, 'error not called')
restore()
t.end();
});
t.end()
})
test('set prefix', function (t) {
var now = new Date().toISOString();
var logger = Logger({ prefix: now });
var msg = 'bar';
var now = new Date().toISOString()
var logger = Logger({ prefix: now })
var msg = 'bar'
spyOn('info', function () {
spyOff('info');
t.equal(arguments[0], now + ' foo %s', 'first arg ok');
t.equal(arguments[1], msg, 'second arg ok');
});
spyOff('info')
t.equal(arguments[0], now + ' foo %s', 'first arg ok')
t.equal(arguments[1], msg, 'second arg ok')
})
spyOn('warn', function () {
spyOff('warn');
t.equal(arguments[0], now + ' foo %s', 'first arg ok');
t.equal(arguments[1], msg, 'second arg ok');
});
spyOff('warn')
t.equal(arguments[0], now + ' foo %s', 'first arg ok')
t.equal(arguments[1], msg, 'second arg ok')
})
spyOn('error', function () {
spyOff('error');
t.equal(arguments[0], now + ' foo %s', 'first arg ok');
t.equal(arguments[1], msg, 'second arg ok');
t.end();
});
spyOff('error')
t.equal(arguments[0], now + ' foo %s', 'first arg ok')
t.equal(arguments[1], msg, 'second arg ok')
t.end()
})
logger.info('foo %s', msg);
logger.warn('foo %s', msg);
logger.error('foo %s', msg);
});
logger.info('foo %s', msg)
logger.warn('foo %s', msg)
logger.error('foo %s', msg)
})
test('set prefix with function', function (t) {
var now = new Date().toISOString();
var logger = Logger({ prefix: function () { return now; } });
var msg = 'bar';
var now = new Date().toISOString()
var logger = Logger({ prefix: function () { return now } })
var msg = 'bar'
spyOn('info', function () {
spyOff('info');
t.equal(arguments[0], now + ' foo %s', 'first arg ok');
t.equal(arguments[1], msg, 'second arg ok');
});
spyOff('info')
t.equal(arguments[0], now + ' foo %s', 'first arg ok')
t.equal(arguments[1], msg, 'second arg ok')
})
spyOn('warn', function () {
spyOff('warn');
t.equal(arguments[0], now + ' foo %s', 'first arg ok');
t.equal(arguments[1], msg, 'second arg ok');
});
spyOff('warn')
t.equal(arguments[0], now + ' foo %s', 'first arg ok')
t.equal(arguments[1], msg, 'second arg ok')
})
spyOn('error', function () {
spyOff('error');
t.equal(arguments[0], now + ' foo %s', 'first arg ok');
t.equal(arguments[1], msg, 'second arg ok');
t.end();
});
spyOff('error')
t.equal(arguments[0], now + ' foo %s', 'first arg ok')
t.equal(arguments[1], msg, 'second arg ok')
t.end()
})
logger.info('foo %s', msg);
logger.warn('foo %s', msg);
logger.error('foo %s', msg);
});
logger.info('foo %s', msg)
logger.warn('foo %s', msg)
logger.error('foo %s', msg)
})
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