Skip to content

Commit

Permalink
feat: count spam log
Browse files Browse the repository at this point in the history
  • Loading branch information
pooya parsa committed Jun 18, 2019
1 parent f1f5705 commit 197a6b3
Showing 1 changed file with 23 additions and 9 deletions.
32 changes: 23 additions & 9 deletions src/consola.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@ class Consola {
this.mockTypes()
}

// Keep serialized version of last message
this._lastMessage = null
this._lastMessageTime = null
// Keep serialized version of last log
this._lastLogSerialized = null
this._lastLog = null
this._lastLogTime = null
this._lastLogCount = 0
}

get level () {
Expand Down Expand Up @@ -257,20 +259,32 @@ class Consola {
}

// Throttle
const diffTime = this._lastMessageTime ? logObj.date - this._lastMessageTime : 0
this._lastMessageTime = logObj.date
const diffTime = this._lastLogTime ? logObj.date - this._lastLogTime : 0
this._lastLogTime = logObj.date
if (diffTime < this._throttle) {
try {
const serializedMessage = JSON.stringify([logObj.type, logObj.tag, logObj.args])
const isSameMessage = this._lastMessage === serializedMessage
this._lastMessage = serializedMessage
if (isSameMessage) {
const serializedLog = JSON.stringify([logObj.type, logObj.tag, logObj.args])
const isSameLog = this._lastLogSerialized === serializedLog
this._lastLogSerialized = serializedLog
if (isSameLog) {
this._lastLogCount++
return // SPAM!
}
} catch (_) {
// Circular References
}
}
if (this._lastLogCount) {
this._log({
...this._lastLog,
args: [
...this._lastLog.args,
`(repeated ${this._lastLogCount} times)`
]
})
this._lastLogCount = 0
}
this._lastLog = logObj

// Log
if (this._async) {
Expand Down

0 comments on commit 197a6b3

Please sign in to comment.