-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathlogger.ts
More file actions
54 lines (45 loc) · 1.45 KB
/
logger.ts
File metadata and controls
54 lines (45 loc) · 1.45 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
47
48
49
50
51
52
53
54
export type LoggerBackend = {
info(msg: string, ...args: unknown[]): void
warn(msg: string, ...args: unknown[]): void
error(msg: string, ...args: unknown[]): void
debug?(msg: string, ...args: unknown[]): void
}
const NOOP_LOGGER: LoggerBackend = {
info() {},
warn() {},
error() {},
debug() {},
}
let _backend: LoggerBackend = NOOP_LOGGER
let _debug = false
export function initLogger(backend: LoggerBackend, debug: boolean): void {
_backend = backend
_debug = debug
}
export const log = {
info(msg: string, ...args: unknown[]): void {
_backend.info(`supermemory: ${msg}`, ...args)
},
warn(msg: string, ...args: unknown[]): void {
_backend.warn(`supermemory: ${msg}`, ...args)
},
error(msg: string, err?: unknown): void {
const detail = err instanceof Error ? err.message : err ? String(err) : ""
_backend.error(`supermemory: ${msg}${detail ? ` — ${detail}` : ""}`)
},
debug(msg: string, ...args: unknown[]): void {
if (!_debug) return
const fn = _backend.debug ?? _backend.info
fn(`supermemory [debug]: ${msg}`, ...args)
},
debugRequest(method: string, params: Record<string, unknown>): void {
if (!_debug) return
const fn = _backend.debug ?? _backend.info
fn(`supermemory [debug] → ${method}`, JSON.stringify(params, null, 2))
},
debugResponse(method: string, data: unknown): void {
if (!_debug) return
const fn = _backend.debug ?? _backend.info
fn(`supermemory [debug] ← ${method}`, JSON.stringify(data, null, 2))
},
}