@@ -5,7 +5,7 @@ var api = require('./api'),
55
66 storage = [ ] ,
77
8- // current delay
8+ // current delay, starting with one sec
99 delay = CONFIG . DELAY . ONE_SECOND_DELAY ,
1010 sendingLogsInProgress = false ,
1111 // delayed sendLogs call
@@ -48,10 +48,10 @@ var api = require('./api'),
4848 }
4949 }
5050 } ;
51- // count the delay
51+ // re- count the delay
5252 if ( length >= CONFIG . MSG . MAX_BATCH_SIZE ) {
5353 delay = Math . max ( Math . round ( delay / 2.0 ) , CONFIG . DELAY . ONE_SECOND_DELAY ) ;
54- } else {
54+ } else if ( length < CONFIG . MSG . MIN_BATCH_SIZE ) {
5555 delay = Math . min ( Math . round ( 1.25 * delay ) , CONFIG . DELAY . FIVE_SECONDS_DELAY ) ;
5656 }
5757
@@ -86,7 +86,8 @@ module.exports.methods = {
8686 if ( meta . length ) {
8787 if ( level . toLowerCase ( ) === 'error' ) {
8888 data = helpers . parseMeta ( meta , true ) ;
89- if ( data . ex ) {
89+ // if duplicate errors cap per minute isn't exceeded and error object is passed, create an exception
90+ if ( data . ex && error . checkErrorLimitMessage ( data . ex ) ) {
9091 messageObject . Ex = error . formatEx ( data . ex , req ) ;
9192 }
9293 } else {
@@ -98,26 +99,27 @@ module.exports.methods = {
9899 }
99100 }
100101
101- if ( level . toLowerCase ( ) === 'error' && ! messageObject . Ex ) {
102+ // if error object isn't passed with message & duplicate errors cap per minute isn't exceeded, create a string exception
103+ if ( level . toLowerCase ( ) === 'error' && ! messageObject . Ex && error . checkErrorLimitMessage ( data . ex ) ) {
102104 messageObject . Ex = error . formatEx ( err , null , msg ) ;
103105 }
104106
105- if ( ( level . toLowerCase ( ) === 'error' && error . checkErrorLimitMessage ( messageObject . Ex ) ) || level . toLowerCase ( ) !== 'error' ) {
106- storage . push ( messageObject ) ;
107- // remove the earliest message from the queue if message cap is exceeded
108- if ( storage . length === CONFIG . MSG . QUEUE_CAP ) {
109- storage . shift ( ) ;
110- }
107+ storage . push ( messageObject ) ;
108+
109+ // remove the earliest message from the queue if message cap is exceeded
110+ if ( storage . length === CONFIG . MSG . QUEUE_CAP ) {
111+ storage . shift ( ) ;
111112 }
112113 } ,
113114
114- /* start sending logs after IdentifyApp call is done
115- */
115+ // start sending logs after IdentifyApp call is done
116116 start : function start ( ) {
117117 timeout = setTimeout ( sendLogs , delay ) ;
118118 } ,
119+
119120 // reset current delay schedule, push exception to the queue, send data and run the callback
120121 sendException : function sendException ( err , req , cb ) {
122+ // check if messages are being sent right now
121123 var check = function check ( ) {
122124
123125 if ( sendingLogsInProgress ) {
@@ -131,7 +133,14 @@ module.exports.methods = {
131133 this . push ( 'error' , err . message , [ err ] , req ) ;
132134 check ( ) ;
133135 } ,
136+ // drain the queue and send the messages before server closes
137+ drain : function drain ( ) {
138+ if ( storage . length ) {
139+ api . methods . postLogsSync ( storage ) ;
140+ }
141+ } ,
134142
143+ // basic logging method
135144 log : function log ( level , msg ) {
136145 var meta = Array . prototype . slice . call ( arguments , 2 ) ;
137146 var levels = [ 'error' , 'debug' , 'warn' , 'info' , 'trace' ] ;
@@ -140,11 +149,10 @@ module.exports.methods = {
140149 if ( levels . indexOf ( level . toLowerCase ( ) ) < 0 ) {
141150 throw new TypeError ( level + ' level doesn\'t exist' ) ;
142151 }
143- // check the message
152+ // check the message itself
144153 if ( typeof msg !== 'string' ) {
145154 throw new TypeError ( 'Message must be a string' ) ;
146155 }
147-
148156 this . push ( level , msg , meta ) ;
149157 } ,
150158
0 commit comments