forked from rocicorp/replicache
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger.ts
More file actions
46 lines (42 loc) · 1.57 KB
/
Copy pathlogger.ts
File metadata and controls
46 lines (42 loc) · 1.57 KB
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
44
45
46
/**
* A logger interface exposing optional [[error]], [[info]] and [[debug]]
* methods. The idea is that these gets filled in based on the [[LogLevel]]. If
* [[LogLevel]] is 'debug' then all three should be present, if it is 'info',
* then [[error]] and [[info]] should be present and if [[LogLevel]] is 'error'
* only [[error]] will be present.
*/
export interface Logger {
error?(...args: unknown[]): void;
info?(...args: unknown[]): void;
debug?(...args: unknown[]): void;
}
/**
* The different log levels. This is used to determine how much logging to do.
* `'error'` > `'info'` > `'debug'`... meaning `'error'` has highest priority
* and `'debug'` lowest.
*/
export type LogLevel = 'error' | 'info' | 'debug';
export function getLogger(prefix: string[], level: LogLevel): Logger {
const logger: Logger = {};
const impl = (name: 'debug' | 'log' | 'error') => (...args: unknown[]) =>
console[name](...prefix, ...args);
switch (level) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
// eslint-disable-next-line no-fallthrough
case 'debug':
logger.debug = impl('debug');
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
// eslint-disable-next-line no-fallthrough
case 'info':
// Use log instead of info because that is what repc uses.
logger.info = impl('log');
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
// eslint-disable-next-line no-fallthrough
case 'error':
logger.error = impl('error');
}
return logger;
}