-
Notifications
You must be signed in to change notification settings - Fork 1
/
format.mjs
44 lines (40 loc) · 952 Bytes
/
format.mjs
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
import YAML from 'yaml'
const dtf = new Intl.DateTimeFormat('sv-SE', {
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
fractionalSecondDigits: 3,
})
export function formatTime(time) {
const date = time ? new Date(time) : null
if (!date || isNaN(date)) {
return '<invalid date>'
}
return dtf.format(date)
}
export function formatLevel(level) {
if (level == null || !Number.isFinite(level)) {
return '?'
}
if (level === 100) {
return 'INVALID'
} else if (level >= 60) {
return 'FATAL'
} else if (level >= 50) {
return 'ERROR'
} else if (level >= 40) {
return 'WARN'
} else if (level >= 30) {
return 'INFO'
} else if (level >= 20) {
return 'DEBUG'
} else {
return 'TRACE'
}
}
export function formatObject(obj, opts) {
return YAML.stringify(obj, { blockQuote: 'literal', aliasDuplicateObjects: false, ...opts })
}