File tree 3 files changed +35
-0
lines changed
3 files changed +35
-0
lines changed Original file line number Diff line number Diff line change 20
20
- Browser support
21
21
- Pause/Resume support
22
22
- Mocking support
23
+ - Spam preventation by throttling logs
23
24
24
25
## Installation
25
26
Original file line number Diff line number Diff line change
1
+ #!/usr/bin/env node -r esm
2
+
3
+ import { consola } from './utils'
4
+
5
+ function spam ( msg , level = 'warn' , count = 10 ) {
6
+ for ( let i = 0 ; i < 10 ; i ++ ) {
7
+ consola [ level ] ( msg )
8
+ }
9
+ }
10
+
11
+ spam ( 'FOOOOOOO FOOOOOOOOO' )
12
+ consola . log ( 'bar' )
13
+ spam ( 'FOOOOOOO FOOOOOOOOO' )
Original file line number Diff line number Diff line change @@ -14,6 +14,7 @@ class Consola {
14
14
this . _stdout = options . stdout
15
15
this . _stderr = options . stdout
16
16
this . _mockFn = options . mockFn
17
+ this . _throttle = options . throttle || 2000
17
18
18
19
// Create logger functions for current instance
19
20
for ( const type in this . _types ) {
@@ -28,6 +29,10 @@ class Consola {
28
29
if ( this . _mockFn ) {
29
30
this . mockTypes ( )
30
31
}
32
+
33
+ // Keep serialized version of last message
34
+ this . _lastMessage = null
35
+ this . _lastMessageTime = null
31
36
}
32
37
33
38
get level ( ) {
@@ -251,6 +256,22 @@ class Consola {
251
256
logObj . tag = logObj . tag . toLowerCase ( )
252
257
}
253
258
259
+ // Throttle
260
+ const diffTime = this . _lastMessageTime ? logObj . date - this . _lastMessageTime : 0
261
+ this . _lastMessageTime = logObj . date
262
+ if ( diffTime < this . _throttle ) {
263
+ 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 ) {
268
+ return // SPAM!
269
+ }
270
+ } catch ( _ ) {
271
+ // Circular References
272
+ }
273
+ }
274
+
254
275
// Log
255
276
if ( this . _async ) {
256
277
return this . _logAsync ( logObj )
You can’t perform that action at this time.
0 commit comments