forked from ipfs/public-gateway-checker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLog.ts
43 lines (34 loc) · 1.16 KB
/
Log.ts
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
37
38
39
40
41
42
43
type Console = typeof console
/**
* This class' sole purpose is to avoid cluttering the codebase with `eslint-disable-line no-console` comments
*
* When using this class to log errors or messages, one can assume it's intentional and principled.
*/
class Log {
constructor (private readonly namespace?: string) {}
/**
* The log method's generic typing allows it to only accept
*
* @param method - log
* @param args
*/
private log<M extends Extract<keyof Console, keyof Omit<Log, 'log'>>>(method: M, ...args: Parameters<Console[M]>): void {
const [msg, ...optionalParams] = args
const prefix = this.namespace != null ? `${this.namespace}.${method}: ` : ''
// eslint-disable-next-line no-console
console[method](`${prefix}${msg as string}`, ...optionalParams)
}
debug (...args: Parameters<Console['debug']>): void {
this.log('debug', ...args)
}
info (...args: Parameters<Console['info']>): void {
this.log('info', ...args)
}
warn (...args: Parameters<Console['warn']>): void {
this.log('warn', ...args)
}
error (...args: Parameters<Console['error']>): void {
this.log('error', ...args)
}
}
export { Log }