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

I added the stack trace support to logger. #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
19 changes: 18 additions & 1 deletion logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ var Logger = function(log_file_path) {
// default write is STDOUT
this.write = sys.print;
this.log_level_index = 3;
//default output stack trace
this.stackTrace = true;
//reduce unnecessary trace by the filter string.
//for example,set the filter equal your application's root path name to output the caller at your project only.
this.stackTraceFilter = null;

// if a path is given, try to write to it
if (log_file_path) {
Expand Down Expand Up @@ -95,7 +100,19 @@ Logger.prototype.log = function() {
}
});
message = this.format(Logger.levels[log_index], new Date(), message);
this.write(message + "\n");
var stackstr = '';
if (this.stackTrace && log_index < 2) {
var stack = new Error().stack.split('\n');
var self = this;
stackstr = stack.filter(function(v){
if (v.indexOf('].log') > -1) return false;
if (self.stackTraceFilter) {
return v.indexOf(self.stackTraceFilter)>-1;
}
return true;
}).join('\n') + '\n';
}
this.write(message + "\n" +stackstr);
return message;
}
return false;
Expand Down