Skip to content

Instantly share code, notes, and snippets.

@strongant
Created May 20, 2016 06:23
Show Gist options
  • Save strongant/68f3919f7f461ae31cba96c68df6a7a2 to your computer and use it in GitHub Desktop.
Save strongant/68f3919f7f461ae31cba96c68df6a7a2 to your computer and use it in GitHub Desktop.
// logger = require('logger');
// logger.log_to_console(true);
// logger.log_to_folder('log','_app.log');
// logger.set_log_level('DEBUG');
// logger.info('Happy to be here.');
exports.logger = (function() {
var that = {};
that.levels = {
"TRACE": 0,
"DEBUG": 1,
"INFO": 2,
"WARN": 3,
"ERROR": 4,
"SILENT": 5
};
var log_file_path = null;
var log_file_stream = null;
var log_level = that.levels.ERROR;
var log_to_console = true;
var logfunc = null;
that.trace = function(msg) {
var my_level = that.levels.TRACE;
if (my_level >= log_level) {
logfunc(msg);
}
};
that.debug = function(msg) {
var my_level = that.levels.DEBUG;
if (my_level >= log_level) {
logfunc(msg);
}
};
that.info = function(msg) {
var my_level = that.levels.INFO;
if (my_level >= log_level) {
logfunc(msg);
}
};
that.warn = function(msg) {
var my_level = that.levels.WARN;
if (my_level >= log_level) {
logfunc(msg);
}
};
that.error = function(msg) {
var my_level = that.levels.ERROR;
if (my_level >= log_level) {
logfunc(msg);
}
};
that.log_to_console = function(true_false) {
if (true_false) {
log_to_console = true;
} else {
log_to_console = false;
}
};
that.log_to_folder = function(log_folder,basename) {
var date_string = '';
var filename = '';
var now = new Date();
var pad = null;
var re = new RegExp(fs.separator + '$');
pad = function(number) {
if (number.toString().length < 2) {
return ('0' + number.toString());
} else {
return (number.toString());
}
};
if (!fs.isDirectory(log_folder)) {
fs.makeTree(log_folder);
if (!fs.isDirectory(log_folder)) {
throw(new Error(log_folder + ' is not a directory'));
}
}
if (re.exec(log_folder) === null) {
log_folder += fs.separator;
}
date_string += pad(now.getFullYear());
date_string += pad(now.getMonth());
date_string += pad(now.getDate());
date_string += pad(now.getHours());
date_string += pad(now.getMinutes());
date_string += pad(now.getSeconds());
filename = date_string + basename;
log_file_path = log_folder + filename;
log_file_stream = fs.open(log_file_path,'w');
console.log('logging to ' + log_file_path);
};
that.get_log_file_path = function() {
return log_file_path;
};
that.get_log_level = function() {
var ii = 0;
var keys = Object.keys(that.levels);
var length = keys.length;
var value = null;
for (ii=0; ii<length; ++ii) {
value = that.levels[keys[ii]];
if (value === log_level) {
return keys[ii];
}
}
return null;
};
that.set_log_level = function(level) {
log_level = that.levels[level.toUpperCase()];
if (log_level === undefined) {
log_level = that.levels.SILENT;
}
};
logfunc = function(msg) {
if (log_level < that.levels.SILENT) {
if (log_file_stream) {
log_file_stream.writeLine(msg);
log_file_stream.flush();
}
if (log_to_console) {
console.log(msg);
}
}
};
return that;
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment