-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconsoleLogs.ts
More file actions
36 lines (31 loc) · 774 Bytes
/
consoleLogs.ts
File metadata and controls
36 lines (31 loc) · 774 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import type { Collector } from './collector.js'
const consoleMethods = ['log', 'info', 'warn', 'error'] as const
export interface ConsoleLogs {
type: 'log' | 'info' | 'warn' | 'error'
args: any[]
timestamp: number
source?: 'browser' | 'test'
}
export class ConsoleLogCollector implements Collector<ConsoleLogs> {
#logs: ConsoleLogs[] = []
constructor() {
consoleMethods.forEach(this.#consolePatch.bind(this))
}
getArtifacts() {
return this.#logs
}
clear(): void {
this.#logs = []
}
#consolePatch(type: (typeof consoleMethods)[number]) {
const orig = console[type]
console[type] = (...args) => {
this.#logs.push({
timestamp: Date.now(),
type,
args
})
return orig(...args)
}
}
}