loglevel
Advanced tools
Comparing version 0.2.0 to 0.3.0
@@ -1,2 +0,2 @@ | ||
/*! loglevel - v0.1.0 - 2013-04-02 | ||
/*! loglevel - v0.2.0 - 2013-06-19 | ||
* https://github.com/pimterry/loglevel | ||
@@ -10,11 +10,8 @@ * Copyright (c) 2013 Tim Perry; Licensed MIT */ | ||
} else { | ||
this.name = definition(); | ||
this[name] = definition(); | ||
} | ||
}('log', function () { | ||
var self = {}, | ||
noop = function() { }; | ||
var self = {}; | ||
var noop = function() {}; | ||
self.levels = { "TRACE": 0, "DEBUG": 1, "INFO": 2, "WARN": 3, | ||
"ERROR": 4, "SILENT": 5}; | ||
function realMethod(methodName) { | ||
@@ -24,3 +21,3 @@ if (typeof console === "undefined") { | ||
} else if (typeof console[methodName] === "undefined") { | ||
return console.log || noop; | ||
return boundToConsole(console, 'log') || noop; | ||
} else { | ||
@@ -60,4 +57,53 @@ return boundToConsole(console, methodName); | ||
function cookiesAvailable() { | ||
return (typeof window !== "undefined" && | ||
typeof window.document !== "undefined" && | ||
typeof window.document.cookie !== "undefined"); | ||
} | ||
function setLevelInCookie(levelNum) { | ||
if (!cookiesAvailable()) { | ||
return; | ||
} | ||
var levelName; | ||
for (var key in self.levels) { | ||
if (self.levels.hasOwnProperty(key) && self.levels[key] === levelNum) { | ||
levelName = key; | ||
break; | ||
} | ||
} | ||
if (levelName !== undefined) { | ||
window.document.cookie = "loglevel=" + levelName + ";"; | ||
} | ||
} | ||
var cookieRegex = /loglevel=([^;]+)/; | ||
function loadLevelFromCookie() { | ||
var cookieLevel; | ||
if (cookiesAvailable()) { | ||
var cookieMatch = cookieRegex.exec(window.document.cookie) || []; | ||
cookieLevel = cookieMatch[1]; | ||
} | ||
self.setLevel(self.levels[cookieLevel] || self.levels.WARN); | ||
} | ||
/* | ||
* | ||
* Public API | ||
* | ||
*/ | ||
self.levels = { "TRACE": 0, "DEBUG": 1, "INFO": 2, "WARN": 3, | ||
"ERROR": 4, "SILENT": 5}; | ||
self.setLevel = function (level) { | ||
if (typeof level === "number" && level >= 0 && level <= self.levels.SILENT) { | ||
setLevelInCookie(level); | ||
if (level === self.levels.SILENT) { | ||
@@ -83,3 +129,3 @@ clearMethods(); | ||
} else { | ||
throw "log.setLevel called with invalid level: " + level; | ||
throw "log.setLevel() called with invalid level: " + level; | ||
} | ||
@@ -97,3 +143,3 @@ }; | ||
try { | ||
self.setLevel(self.levels.WARN); | ||
loadLevelFromCookie(); | ||
} catch (e) { | ||
@@ -103,2 +149,2 @@ self.setLevel(self.levels.SILENT); | ||
return self; | ||
})); | ||
})); |
@@ -1,4 +0,4 @@ | ||
/*! loglevel - v0.1.0 - 2013-04-02 | ||
/*! loglevel - v0.2.0 - 2013-06-19 | ||
* https://github.com/pimterry/loglevel | ||
* Copyright (c) 2013 Tim Perry; Licensed MIT */ | ||
(function(e,n){"undefined"!=typeof module?module.exports=n():"function"==typeof define&&"object"==typeof define.amd?define(n):this.name=n()})("log",function(){function e(e){return"undefined"==typeof console?t:console[e]===void 0?console.log||t:n(console,e)}function n(e,n){var o=e[n];return o.bind===void 0?Function.prototype.bind===void 0?function(){o.apply(e,arguments)}:Function.prototype.bind.call(e[n],e):e[n].bind(e)}function o(){for(var e=0;i.length>e;e++)l[i[e]]=t}var l={},t=function(){};l.levels={TRACE:0,DEBUG:1,INFO:2,WARN:3,ERROR:4,SILENT:5};var i=["trace","debug","info","warn","error"];l.setLevel=function(n){if("number"==typeof n&&n>=0&&l.levels.SILENT>=n){if(n===l.levels.SILENT)return o(),void 0;if("undefined"==typeof console)throw o(),"No console available for logging";for(var r=0;i.length>r;r++){var f=i[r];l[f]=l.levels[f.toUpperCase()]>=n?e(f):t}}else{if("string"!=typeof n)throw"log.setLevel called with invalid level: "+n;l.setLevel(l.levels[n.toUpperCase()])}},l.enableAll=function(){l.setLevel(l.levels.TRACE)},l.disableAll=function(){l.setLevel(l.levels.SILENT)};try{l.setLevel(l.levels.WARN)}catch(r){l.setLevel(l.levels.SILENT)}return l}); | ||
(function(e,o){"undefined"!=typeof module?module.exports=o():"function"==typeof define&&"object"==typeof define.amd?define(o):this[e]=o()})("log",function(){function e(e){return"undefined"==typeof console?f:console[e]===void 0?o(console,"log")||f:o(console,e)}function o(e,o){var n=e[o];return n.bind===void 0?Function.prototype.bind===void 0?function(){n.apply(e,arguments)}:Function.prototype.bind.call(e[o],e):e[o].bind(e)}function n(){for(var e=0;v.length>e;e++)r[v[e]]=f}function l(){return"undefined"!=typeof window&&window.document!==void 0&&window.document.cookie!==void 0}function t(e){if(l()){var o;for(var n in r.levels)if(r.levels.hasOwnProperty(n)&&r.levels[n]===e){o=n;break}void 0!==o&&(window.document.cookie="loglevel="+o+";")}}function i(){var e;if(l()){var o=d.exec(window.document.cookie)||[];e=o[1]}r.setLevel(r.levels[e]||r.levels.WARN)}var r={},f=function(){},v=["trace","debug","info","warn","error"],d=/loglevel=([^;]+)/;r.levels={TRACE:0,DEBUG:1,INFO:2,WARN:3,ERROR:4,SILENT:5},r.setLevel=function(o){if("number"==typeof o&&o>=0&&r.levels.SILENT>=o){if(t(o),o===r.levels.SILENT)return n(),void 0;if("undefined"==typeof console)throw n(),"No console available for logging";for(var l=0;v.length>l;l++){var i=v[l];r[i]=r.levels[i.toUpperCase()]>=o?e(i):f}}else{if("string"!=typeof o)throw"log.setLevel() called with invalid level: "+o;r.setLevel(r.levels[o.toUpperCase()])}},r.enableAll=function(){r.setLevel(r.levels.TRACE)},r.disableAll=function(){r.setLevel(r.levels.SILENT)};try{i()}catch(c){r.setLevel(r.levels.SILENT)}return r}); |
@@ -32,3 +32,3 @@ 'use strict'; | ||
dest: 'dist/<%= pkg.name %>.min.js' | ||
}, | ||
} | ||
}, | ||
@@ -40,2 +40,3 @@ jasmine: { | ||
vendor: 'test/vendor/*.js', | ||
helpers: 'test/*-helper.js', | ||
template: require('grunt-template-jasmine-requirejs') | ||
@@ -42,0 +43,0 @@ } |
@@ -14,11 +14,8 @@ /* | ||
} else { | ||
this.name = definition(); | ||
this[name] = definition(); | ||
} | ||
}('log', function () { | ||
var self = {}, | ||
noop = function() { }; | ||
var self = {}; | ||
var noop = function() {}; | ||
self.levels = { "TRACE": 0, "DEBUG": 1, "INFO": 2, "WARN": 3, | ||
"ERROR": 4, "SILENT": 5}; | ||
function realMethod(methodName) { | ||
@@ -28,3 +25,3 @@ if (typeof console === "undefined") { | ||
} else if (typeof console[methodName] === "undefined") { | ||
return console.log || noop; | ||
return boundToConsole(console, 'log') || noop; | ||
} else { | ||
@@ -64,4 +61,53 @@ return boundToConsole(console, methodName); | ||
function cookiesAvailable() { | ||
return (typeof window !== "undefined" && | ||
typeof window.document !== "undefined" && | ||
typeof window.document.cookie !== "undefined"); | ||
} | ||
function setLevelInCookie(levelNum) { | ||
if (!cookiesAvailable()) { | ||
return; | ||
} | ||
var levelName; | ||
for (var key in self.levels) { | ||
if (self.levels.hasOwnProperty(key) && self.levels[key] === levelNum) { | ||
levelName = key; | ||
break; | ||
} | ||
} | ||
if (levelName !== undefined) { | ||
window.document.cookie = "loglevel=" + levelName + ";"; | ||
} | ||
} | ||
var cookieRegex = /loglevel=([^;]+)/; | ||
function loadLevelFromCookie() { | ||
var cookieLevel; | ||
if (cookiesAvailable()) { | ||
var cookieMatch = cookieRegex.exec(window.document.cookie) || []; | ||
cookieLevel = cookieMatch[1]; | ||
} | ||
self.setLevel(self.levels[cookieLevel] || self.levels.WARN); | ||
} | ||
/* | ||
* | ||
* Public API | ||
* | ||
*/ | ||
self.levels = { "TRACE": 0, "DEBUG": 1, "INFO": 2, "WARN": 3, | ||
"ERROR": 4, "SILENT": 5}; | ||
self.setLevel = function (level) { | ||
if (typeof level === "number" && level >= 0 && level <= self.levels.SILENT) { | ||
setLevelInCookie(level); | ||
if (level === self.levels.SILENT) { | ||
@@ -87,3 +133,3 @@ clearMethods(); | ||
} else { | ||
throw "log.setLevel called with invalid level: " + level; | ||
throw "log.setLevel() called with invalid level: " + level; | ||
} | ||
@@ -101,3 +147,3 @@ }; | ||
try { | ||
self.setLevel(self.levels.WARN); | ||
loadLevelFromCookie(); | ||
} catch (e) { | ||
@@ -107,2 +153,2 @@ self.setLevel(self.levels.SILENT); | ||
return self; | ||
})); | ||
})); |
{ | ||
"name": "loglevel", | ||
"description": "Minimal lightweight logging for JavaScript, adding reliable log level methods to any available console.log methods", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"homepage": "https://github.com/pimterry/loglevel", | ||
@@ -46,3 +46,7 @@ "author": { | ||
}, | ||
"jam": { | ||
"dependencies": {}, | ||
"main": "lib/loglevel.js" | ||
}, | ||
"keywords": ["log", "logger", "logging", "browser"] | ||
} |
@@ -28,3 +28,3 @@ # loglevel [![Build Status](https://travis-ci.org/pimterry/loglevel.png)](https://travis-ci.org/pimterry/loglevel) | ||
If you're using node, you can run `npm install loglevel`. | ||
If you're using node, you can run `npm install loglevel`. loglevel is also available via [Bower](https://github.com/bower/bower) (run `bower install loglevel`) or [JamJS](http://jamjs.org/packages/#/details/loglevel) (run `jam install loglevel`) | ||
@@ -84,7 +84,9 @@ Alternatively if you want to grab the file directly, you can download either the [production version][min] or the [development version][max] directly. | ||
This can take either a log level name or 'silent' (which disables everything) in one of a few forms: | ||
* As a string, like 'error' (case-insensitive) | ||
* As a log level from the internal levels list, e.g. log.levels.SILENT | ||
* As a numeric index from 0 (trace) to 5 (silent). | ||
* As a log level from the internal levels list, e.g. log.levels.SILENT <- for type safety | ||
* As a string, like 'error' (case-insensitive) <- for a reasonable practical balance | ||
* As a numeric index from 0 (trace) to 5 (silent) <- deliciously terse, and more easily programmable (...although, why?) | ||
Where possible the loglevel will be persisted as a session cookie on the current domain. If cookies are not available in the current environment (i.e. in Node) this step will be skipped. | ||
**log.setLevel() will throw an error if you attempt to set the level to a non-silent level and there is no console available.** If you do want to explicitly change the default log level from warn in your codebase you should do so in a try/catch. Failing setLevel calls due to a missing console are equivalent to log.setLevel("silent"), which never fails. | ||
It is expected that log.setLevel() will be manually called during debugging and similar, and as such you should note that **log.setLevel() will throw an error if you attempt to set the level to a non-silent level and there is no console available.** If you do want to explicitly change the default log level from warn in your codebase you should do so in a try/catch. Failing setLevel calls due to a missing console are equivalent to log.setLevel("silent"), which never fails. | ||
@@ -105,4 +107,8 @@ * `log.enableAll()` and `log.disableAll()` methods. | ||
v0.3.0 - Some bugfixes (#12, #14), cookie-based log level persistence, doc tweaks, support for Bower and JamJS | ||
## License | ||
Copyright (c) 2013 Tim Perry | ||
Licensed under the MIT license. | ||
[![githalytics.com alpha](https://cruel-carlota.pagodabox.com/3393271900ddc6808ae1901f3760a59e "githalytics.com")](http://githalytics.com/pimterry/loglevel) |
@@ -41,4 +41,24 @@ "use strict"; | ||
}); | ||
it("are saved in cookies", function() { | ||
log.setLevel(log.levels.TRACE); | ||
expect(window.document.cookie).toContain("loglevel=TRACE"); | ||
log.setLevel(log.levels.DEBUG); | ||
expect(window.document.cookie).toContain("loglevel=DEBUG"); | ||
log.setLevel(log.levels.INFO); | ||
expect(window.document.cookie).toContain("loglevel=INFO"); | ||
log.setLevel(log.levels.WARN); | ||
expect(window.document.cookie).toContain("loglevel=WARN"); | ||
log.setLevel(log.levels.ERROR); | ||
expect(window.document.cookie).toContain("loglevel=ERROR"); | ||
log.setLevel(log.levels.SILENT); | ||
expect(window.document.cookie).toContain("loglevel=SILENT"); | ||
}); | ||
}); | ||
}); | ||
}); |
@@ -0,0 +0,0 @@ "use strict"; |
@@ -0,0 +0,0 @@ "use strict"; |
@@ -0,0 +0,0 @@ /* |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Uses eval
Supply chain riskPackage uses eval() which is a dangerous function. This prevents the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 2 instances in 1 package
SPDX disjunction
LicenseSPDX disjunction for an artifact's license information
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
SPDX disjunction
LicenseSPDX disjunction for an artifact's license information
Found 1 instance in 1 package
337738
41
4293
112
9
2