Repository navigation
Expand file tree
/
Copy pathDebug.ts
More file actions
60 lines (50 loc) · 1.83 KB
/
Copy pathDebug.ts
File metadata and controls
60 lines (50 loc) · 1.83 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
55
56
57
58
59
60
import { Lincoln, LincolnEnvelope, LincolnLog, LincolnMessageType, Debug } from '../deps.ts'
interface DebugLogger {
[key: string]: any
}
const DEBUG_LOGGERS: DebugLogger = {}
export class LincolnLogDebug extends LincolnLog {
static observe(lincoln: Lincoln): LincolnLogDebug {
return new LincolnLogDebug(lincoln)
}
protected initialize(): Promise<void> {
return Promise.resolve()
}
protected render(envelope: LincolnEnvelope): Promise<void> {
const scope = [envelope.scope, this.messageTypeString(envelope.message.type)].join(':')
const logger: any = DEBUG_LOGGERS[scope] ? DEBUG_LOGGERS[scope] : (DEBUG_LOGGERS[scope] = Debug.default(scope))
const messageHasString = typeof envelope.message.body === 'string'
const messageParamsIsArray = Array.isArray(envelope.message.parameters)
if (messageHasString && messageParamsIsArray) {
logger(envelope.message.body, ...envelope.message.parameters)
} else if (messageHasString && messageParamsIsArray === false) {
logger(envelope.message.body)
} else {
logger(JSON.stringify({ body: envelope.message.body, params: envelope.message.parameters }, null, 2))
}
return Promise.resolve()
}
protected renderError(envelope: LincolnEnvelope): Promise<void> {
return this.render(envelope)
}
protected messageTypeString(type: LincolnMessageType): string {
switch (type) {
case LincolnMessageType.debug:
return 'debug'
case LincolnMessageType.error:
return 'error'
case LincolnMessageType.fatal:
return 'fatal'
case LincolnMessageType.info:
return 'info'
case LincolnMessageType.silly:
return 'silly'
case LincolnMessageType.trace:
return 'trace'
case LincolnMessageType.warn:
return 'watcn'
default:
return 'unknown'
}
}
}