Skip to content

Commit 197a6b3

Browse files
author
pooya parsa
committedJun 18, 2019
feat: count spam log
1 parent f1f5705 commit 197a6b3

File tree

1 file changed

+23
-9
lines changed

1 file changed

+23
-9
lines changed
 

‎src/consola.js

+23-9
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,11 @@ class Consola {
3030
this.mockTypes()
3131
}
3232

33-
// Keep serialized version of last message
34-
this._lastMessage = null
35-
this._lastMessageTime = null
33+
// Keep serialized version of last log
34+
this._lastLogSerialized = null
35+
this._lastLog = null
36+
this._lastLogTime = null
37+
this._lastLogCount = 0
3638
}
3739

3840
get level () {
@@ -257,20 +259,32 @@ class Consola {
257259
}
258260

259261
// Throttle
260-
const diffTime = this._lastMessageTime ? logObj.date - this._lastMessageTime : 0
261-
this._lastMessageTime = logObj.date
262+
const diffTime = this._lastLogTime ? logObj.date - this._lastLogTime : 0
263+
this._lastLogTime = logObj.date
262264
if (diffTime < this._throttle) {
263265
try {
264-
const serializedMessage = JSON.stringify([logObj.type, logObj.tag, logObj.args])
265-
const isSameMessage = this._lastMessage === serializedMessage
266-
this._lastMessage = serializedMessage
267-
if (isSameMessage) {
266+
const serializedLog = JSON.stringify([logObj.type, logObj.tag, logObj.args])
267+
const isSameLog = this._lastLogSerialized === serializedLog
268+
this._lastLogSerialized = serializedLog
269+
if (isSameLog) {
270+
this._lastLogCount++
268271
return // SPAM!
269272
}
270273
} catch (_) {
271274
// Circular References
272275
}
273276
}
277+
if (this._lastLogCount) {
278+
this._log({
279+
...this._lastLog,
280+
args: [
281+
...this._lastLog.args,
282+
`(repeated ${this._lastLogCount} times)`
283+
]
284+
})
285+
this._lastLogCount = 0
286+
}
287+
this._lastLog = logObj
274288

275289
// Log
276290
if (this._async) {

0 commit comments

Comments
 (0)
Please sign in to comment.