@@ -17,16 +17,15 @@ var api = require('./api'),
1717
1818 // automatically send all the messages from the queue after some timeout
1919 checkLogs = function ( ) {
20- var length = storage . length ;
20+ var length = Math . min ( storage . length , CONFIG . MSG_LIMIT ) ;
2121 if ( flag && length ) {
2222 sendLogs ( length ) ;
2323 }
2424 console . log ( 'logs checked' ) ;
2525 } ,
2626 // handler for sending logs
27- sendLogs = function ( len ) {
27+ sendLogs = function ( length ) {
2828 var self = this ,
29- length = len || CONFIG . MSG_LIMIT ,
3029 data = storage . slice ( 0 , length ) ,
3130
3231 /* if request is succesful remove messages from the queue, change the timeout for the next checkLogs call,
@@ -38,11 +37,14 @@ var api = require('./api'),
3837 storage = storage . slice ( length ) ;
3938 console . log ( 'logs sent' ) ;
4039
40+ // if there are enough messages for the batch, send another request
41+
4142 if ( storage . length >= CONFIG . MSG_LIMIT ) {
4243 sendLogs ( ) ;
4344 } else {
4445 flag = true ;
4546
47+ // reset previous timeout, define the new one
4648 if ( timeout ) {
4749 clearTimeout ( timeout ) ;
4850 }
@@ -67,9 +69,10 @@ var api = require('./api'),
6769 }
6870 } ,
6971 /**
70- Check for duplicated error messages
72+ Check for duplicate error messages. If the same error message logged more than configurated limit in one minute
73+ don't push it to the queue
7174 */
72- checkErrorLimitMessage = function checkErrorLimitMessage ( ex ) {
75+ checkErrorLimitMessage = function checkErrorLimitMessage ( ex ) {
7376 var min = new Date ( ) . getMinutes ( ) ,
7477 key = ex . Error . Message + ex . Error . ErrorType + ex . Error . SourceMethod ;
7578
@@ -79,12 +82,12 @@ var api = require('./api'),
7982
8083 if ( error_storage [ min ] ) {
8184 if ( error_storage [ min ] [ key ] ) {
82- error_storage [ min ] [ key ] += 1
85+ error_storage [ min ] [ key ] += 1 ;
8386 } else {
8487 error_storage [ min ] [ key ] = 1 ;
8588 }
8689 } else {
87- errorStorage = { } ;
90+ error_storage = { } ;
8891 error_storage [ min ] = { } ;
8992 error_storage [ min ] [ key ] = 1 ;
9093 }
@@ -108,6 +111,7 @@ module.exports.methods = {
108111 } ,
109112 data ;
110113
114+ // handle properly metadata object and create exception object if needed
111115 if ( meta ) {
112116 if ( level . toLowerCase ( ) === 'error' ) {
113117 data = helpers . parseMeta ( meta , true ) ;
@@ -131,55 +135,44 @@ module.exports.methods = {
131135 storage . push ( rec ) ;
132136 console . log ( 'logged' ) ;
133137
138+ // remove the earliest message from the queue if message cap is exceeded
134139 if ( storage . length === CONFIG . MSG_CAP ) {
135140 storage . shift ( ) ;
136141 }
137142 }
138-
143+
139144 if ( flag && storage . length >= CONFIG . MSG_LIMIT && CONFIG . APIKEY ) {
140145 sendLogs ( ) ;
141146 }
142147 } ,
143148
144- /* check the queue after IdentifyApp call is done, switch the flag, set the timeout for checking the queue,
149+ /* check the queue after IdentifyApp call is done, switch the flag, set the timeout for the next checking the queue,
145150 set environment details for already logged messages
146151 */
147152 init : function init ( ) {
148- console . log ( 'flag switched' ) ;
149153 flag = true ;
150154
151155 timeout = setTimeout ( checkLogs , CONFIG . SCAN_TIMER ) ;
152156
153- storage . forEach ( function ( elem , index , arr ) {
154- if ( elem . ex ) {
155- elem . ex . EnvironmentDetail = {
156- AppName : CONFIG . APP_DETAILS ? CONFIG . APP_DETAILS . AppName : CONFIG . APPNAME ,
157- AppNameID : CONFIG . APP_DETAILS ? CONFIG . APP_DETAILS . AppNameID : '' ,
158- EnvID : CONFIG . APP_DETAILS ? CONFIG . APP_DETAILS . EnvID : '' ,
159- AppEnvID : CONFIG . APP_DETAILS ? CONFIG . APP_DETAILS . AppEnvID : '' ,
160- AppLocation : process . env . PWD
161- } ;
162- }
163- } ) ;
164-
165157 if ( storage . length >= CONFIG . MSG_LIMIT ) {
166158 sendLogs ( ) ;
167159 }
168160 } ,
169-
161+
170162 log : function log ( level , msg , meta ) {
171163 var levels = [ 'error' , 'debug' , 'warn' , 'info' , 'trace' ] ;
172164
165+ // check the message level
173166 if ( levels . indexOf ( level . toLowerCase ( ) ) < 0 ) {
174- throw new TypeError ( level + 'doesn\'t exist' ) ;
167+ throw new TypeError ( level + ' level doesn\'t exist' ) ;
175168 }
176169
177170 this . push ( level , msg , meta ) ;
178171 } ,
179172
180- /*
181- Shortcut functions for logging message of certain level. Every function takes the same params as log function except the level.
182- */
173+ /*
174+ Shortcut functions for logging message of certain level. Every function takes the same params as log function except the level.
175+ */
183176
184177 trace : function trace ( msg , meta ) {
185178 this . log ( 'trace' , msg , meta ) ;
0 commit comments