Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update logger to build logging function without Eval #2

Merged
merged 4 commits into from
Mar 20, 2011
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 14 additions & 11 deletions logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ logger.log = function (data) {

logger._init_loglevel = function () {
var _loglevel = config.get('loglevel', 'nolog', 'value');
if (_loglevel) {
if (_loglevel) {
var loglevel_num = parseInt(_loglevel);
if (!loglevel_num || loglevel_num === NaN) {
loglevel = logger[_loglevel.toUpperCase()];
Expand All @@ -35,19 +35,22 @@ logger._init_loglevel = function () {
loglevel = logger.LOGWARN;
}
}
// logger.log("Set log level to: " + loglevel);
};

logger._init_loglevel();

for (var key in logger) {
if (key.match(/^LOG\w/)) {
var level = key.slice(3);
var key_copy = key.slice(0); // copy
// For some reason I have to use eval here or this doesn't work...
eval("logger." + key.toLowerCase() +
" = function (data) { if (loglevel >= " + logger[key_copy] +
") { logger.log(\"" + level + ": \" + data); } }");
var level, key;
for (key in logger) {
if(logger.hasOwnProperty(key)) {
if (key.match(/^LOG\w/)) {
level = key.slice(3);
logger[key.toLowerCase()] = (function(level, key) {
return function(data) {
if (loglevel >= logger[key]) {
logger.log("[" + level + "] " + data);
}
}
})(level, key);
}
}
}