@@ -6,25 +6,32 @@ var api = require('./api'),
66
77 storage = [ ] ,
88
9- // flag that indicates if logs are being sent properly . if false then new batches of messages won't be sent.
9+ // flag that indicates if logs are being sent right now . if false then new batches of messages won't be sent.
1010 flag = false ,
1111 // deferred checking of logs
1212 timeout = 0 ,
1313 //number of failed attempts of sending logs
1414 fail_counter = 0 ,
15+ // hash that contains all the errors and their number logged during the current minute
16+ error_storage = { } ,
1517
18+ // automatically send all the messages from the queue after some timeout
1619 checkLogs = function ( ) {
1720 var length = storage . length ;
1821 if ( flag && length ) {
1922 sendLogs ( length ) ;
2023 }
2124 console . log ( 'logs checked' ) ;
2225 } ,
23-
26+ // handler for sending logs
2427 sendLogs = function ( len ) {
2528 var self = this ,
2629 length = len || CONFIG . MSG_LIMIT ,
2730 data = storage . slice ( 0 , length ) ,
31+
32+ /* if request is succesful remove messages from the queue, change the timeout for the next checkLogs call,
33+ switch the flag, send another batch (if there are enough messages in the queue)
34+ */
2835 success = function ( response ) {
2936 fail_counter = 0 ;
3037
@@ -43,6 +50,8 @@ var api = require('./api'),
4350 timeout = setTimeout ( checkLogs , CONFIG . SCAN_TIMER ) ;
4451 }
4552 } ,
53+
54+ // retry the attempt after some timeout
4655 fail = function ( ) {
4756 flag = false ;
4857 fail_counter += 1 ;
@@ -56,11 +65,37 @@ var api = require('./api'),
5665 api . methods . postLogs ( data , success , fail ) ;
5766 flag = false ;
5867 }
68+ } ,
69+ /**
70+ Check for duplicated error messages
71+ */
72+ checkErrorLimitMessage = function checkErrorLimitMessage ( ex ) {
73+ var min = new Date ( ) . getMinutes ( ) ,
74+ key = ex . Error . Message + ex . Error . ErrorType + ex . Error . SourceMethod ;
75+
76+ if ( ! ex ) {
77+ return true ;
78+ }
79+
80+ if ( error_storage [ min ] ) {
81+ if ( error_storage [ min ] [ key ] ) {
82+ error_storage [ min ] [ key ] += 1
83+ } else {
84+ error_storage [ min ] [ key ] = 1 ;
85+ }
86+ } else {
87+ errorStorage = { } ;
88+ error_storage [ min ] = { } ;
89+ error_storage [ min ] [ key ] = 1 ;
90+ }
91+
92+ return ( error_storage [ min ] [ key ] < CONFIG . MAX_DUP_ERROR_PER_MINUTE ) ? true : false ;
5993 } ;
6094
6195module . exports . storage = storage ;
6296
6397module . exports . methods = {
98+ // create the message object, push it to the queue and execute sendLogs call if messages cap is exceeded
6499 push : function push ( level , msg , meta ) {
65100 var err = new Error ( ) ,
66101 getStack = error . getStackTraceItem ( err ) ,
@@ -92,15 +127,23 @@ module.exports.methods = {
92127 rec . Ex = error . formatEx ( err , null , msg ) ;
93128 }
94129
95- storage . push ( rec ) ;
96- console . log ( 'logged' ) ;
97- console . log ( storage . length ) ;
130+ if ( ( level . toLowerCase ( ) === 'error' && checkErrorLimitMessage ( rec . Ex ) ) || level . toLowerCase ( ) !== 'error' ) {
131+ storage . push ( rec ) ;
132+ console . log ( 'logged' ) ;
133+
134+ if ( storage . length === CONFIG . MSG_CAP ) {
135+ storage . shift ( ) ;
136+ }
137+ }
98138
99139 if ( flag && storage . length >= CONFIG . MSG_LIMIT && CONFIG . APIKEY ) {
100140 sendLogs ( ) ;
101141 }
102142 } ,
103143
144+ /* check the queue after IdentifyApp call is done, switch the flag, set the timeout for checking the queue,
145+ set environment details for already logged messages
146+ */
104147 init : function init ( ) {
105148 console . log ( 'flag switched' ) ;
106149 flag = true ;
@@ -123,7 +166,7 @@ module.exports.methods = {
123166 sendLogs ( ) ;
124167 }
125168 } ,
126-
169+
127170 log : function log ( level , msg , meta ) {
128171 var levels = [ 'error' , 'debug' , 'warn' , 'info' , 'trace' ] ;
129172
0 commit comments