1 |
|
2 | import { LogEntry } from '../logger.js'
|
3 | import { Transform } from '../transform.js'
|
4 | import { inspect } from 'util'
|
5 | import * as ansi from '@bevry/ansi'
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | function ansiNoop(a: string): string {
|
12 | return a
|
13 | }
|
14 |
|
15 |
|
16 | interface LevelsToColorsMap {
|
17 | [logLevelNumber: string]: ansi.ANSIApplier
|
18 | }
|
19 |
|
20 |
|
21 | export interface HumanOptions {
|
22 |
|
23 | color?: boolean
|
24 |
|
25 |
|
26 | colors?: LevelsToColorsMap
|
27 | }
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 | export class Human extends Transform {
|
42 |
|
43 | public color: boolean = true
|
44 |
|
45 |
|
46 | public colors: LevelsToColorsMap = {
|
47 | '0': 'red',
|
48 | '1': 'red',
|
49 | '2': 'red',
|
50 | '3': 'red',
|
51 | '4': 'yellow',
|
52 | '5': 'yellow',
|
53 | '6': 'green',
|
54 | '7': 'green',
|
55 | }
|
56 |
|
57 |
|
58 | constructor(opts?: HumanOptions) {
|
59 | super()
|
60 |
|
61 |
|
62 | if (opts?.color != null) this.color = opts.color
|
63 | if (opts?.colors != null) this.colors = opts.colors
|
64 | }
|
65 |
|
66 |
|
67 | getColor(levelNumber: number): ansi.ANSIApplier | false {
|
68 |
|
69 | const color = this.colors[levelNumber] || false
|
70 |
|
71 |
|
72 | return color
|
73 | }
|
74 |
|
75 |
|
76 | padLeft(padding: string, size: number, content: string | number): string {
|
77 |
|
78 | padding = String(padding)
|
79 | content = String(content)
|
80 |
|
81 |
|
82 | if (content.length < size) {
|
83 | for (let i = 0, n = size - content.length; i < n; ++i) {
|
84 | content = padding + content
|
85 | }
|
86 | }
|
87 |
|
88 |
|
89 | return content
|
90 | }
|
91 |
|
92 |
|
93 | formatArguments(args: any[]): string {
|
94 | return args
|
95 | .map((value) =>
|
96 | typeof value === 'string'
|
97 | ? value
|
98 | : inspect(value, {
|
99 | showHidden: false,
|
100 | depth: 10,
|
101 | colors: this.color,
|
102 | })
|
103 | )
|
104 | .join(' ')
|
105 | }
|
106 |
|
107 |
|
108 | formatDate(datetime: Date | number | string): string {
|
109 |
|
110 | const now = new Date(datetime)
|
111 | const year = now.getFullYear()
|
112 | const month = this.padLeft('0', 2, now.getMonth() + 1)
|
113 | const date = this.padLeft('0', 2, now.getDate())
|
114 | const hours = this.padLeft('0', 2, now.getHours())
|
115 | const minutes = this.padLeft('0', 2, now.getMinutes())
|
116 | const seconds = this.padLeft('0', 2, now.getSeconds())
|
117 | const ms = this.padLeft('0', 3, now.getMilliseconds())
|
118 |
|
119 |
|
120 | const result = `${year}-${month}-${date} ${hours}:${minutes}:${seconds}.${ms}`
|
121 |
|
122 |
|
123 | return result
|
124 | }
|
125 |
|
126 |
|
127 | format(entry: LogEntry): string {
|
128 |
|
129 | const { color } = this
|
130 | const useLine = entry.line !== -1
|
131 | let result: string
|
132 |
|
133 |
|
134 | const format = {
|
135 | color: this.getColor(entry.levelNumber),
|
136 | timestamp: this.formatDate(entry.date),
|
137 | text: this.formatArguments(entry.args),
|
138 | }
|
139 |
|
140 |
|
141 | if (format.text) {
|
142 |
|
143 | const levelFormatter =
|
144 | (color && format.color && ansi[format.color]) || ansiNoop
|
145 | const lineFormatter = (useLine && color && ansi.dim) || ansiNoop
|
146 |
|
147 |
|
148 |
|
149 | const levelString = levelFormatter(`${entry.levelName}:`)
|
150 | const entryString = format.text
|
151 | const messageString = `${levelString} ${entryString}`
|
152 |
|
153 |
|
154 | if (useLine) {
|
155 |
|
156 | const seperator = '\n '
|
157 | const debugString = lineFormatter(
|
158 | `→ [${format.timestamp}] [${entry.file}:${entry.line}:${entry.char}] [${entry.method}]`
|
159 | )
|
160 |
|
161 |
|
162 | result = `${messageString}${seperator}${debugString}\n`
|
163 | } else {
|
164 |
|
165 | result = `${messageString}\n`
|
166 | }
|
167 | } else {
|
168 | result = format.text
|
169 | }
|
170 |
|
171 |
|
172 | return result
|
173 | }
|
174 | }
|
175 |
|
176 | export default Human
|