From 3216b5c39054a8ffe115f35fc9d8d366f76fc2bc Mon Sep 17 00:00:00 2001 From: hlj Date: Fri, 20 May 2011 09:31:28 +0800 Subject: [PATCH 1/2] added stack track support --- logger.js | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/logger.js b/logger.js index 33e5690..bcd32bd 100644 --- a/logger.js +++ b/logger.js @@ -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 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) { @@ -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) { + 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 false; + }).join('\n') + '\n'; + } + this.write(message + "\n" +stackstr); return message; } return false; From 6d9949a3950e298b6cb028c94a67c7b0bed599c0 Mon Sep 17 00:00:00 2001 From: hlj Date: Wed, 15 Jun 2011 04:38:42 -0700 Subject: [PATCH 2/2] fix stacktrace level --- logger.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/logger.js b/logger.js index bcd32bd..75eac49 100644 --- a/logger.js +++ b/logger.js @@ -45,7 +45,7 @@ var Logger = function(log_file_path) { //default output stack trace this.stackTrace = true; //reduce unnecessary trace by the filter string. - //for example,set the filter equal your root path name to output the caller at your project only. + //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 @@ -101,7 +101,7 @@ Logger.prototype.log = function() { }); message = this.format(Logger.levels[log_index], new Date(), message); var stackstr = ''; - if (this.stackTrace) { + if (this.stackTrace && log_index < 2) { var stack = new Error().stack.split('\n'); var self = this; stackstr = stack.filter(function(v){ @@ -109,7 +109,7 @@ Logger.prototype.log = function() { if (self.stackTraceFilter) { return v.indexOf(self.stackTraceFilter)>-1; } - return false; + return true; }).join('\n') + '\n'; } this.write(message + "\n" +stackstr);