-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog.ts
More file actions
274 lines (249 loc) · 7.39 KB
/
log.ts
File metadata and controls
274 lines (249 loc) · 7.39 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/**
* Add support for sending logs to our devtools.
* Our Logger also help shapping logs and recognize our context module.
*
* @module
*/
import {
blue,
brightBlue,
brightCyan,
brightGreen,
brightMagenta,
brightRed,
brightYellow,
cyan,
gray,
green,
magenta,
red,
yellow,
} from '@std/fmt/colors'
import { now, startTime } from '@01edu/time'
import { getContext } from './context.ts'
import {
APP_ENV,
CI_COMMIT_SHA,
DEVTOOL_REPORT_TOKEN,
DEVTOOL_URL,
} from './env.ts'
// Types
type LogLevel = 'info' | 'error' | 'warn' | 'debug'
type LoggerOptions = {
/** The URL of the devtool service to send logs to (prod only). Defaults to `DEVTOOL_URL` env var. */
logUrl?: string
/** The authentication token for the devtool service (prod only). Defaults to `DEVTOOL_REPORT_TOKEN` env var. */
logToken?: string
/** The version of the application, typically a git commit SHA. Defaults to `CI_COMMIT_SHA` env var or `git rev-parse HEAD`. */
version?: string
/** The interval in milliseconds to batch and send logs (prod only). */
batchInterval?: number
/** The maximum number of logs to batch before sending (prod only). */
maxBatchSize?: number
/** A set of event names to filter out and not log. */
filters?: Set<string>
}
type LogFunction = (
level: LogLevel,
event: string,
props?: Record<string, unknown>,
) => void
type BoundLogFunction = (
event: string,
props?: Record<string, unknown>,
) => void
/**
* Represents the logger interface, with methods for each log level.
*/
export interface Log extends LogFunction {
/** Logs an error event. */
error: BoundLogFunction
/** Logs a debug event. */
debug: BoundLogFunction
/** Logs a warning event. */
warn: BoundLogFunction
/** Logs an info event. */
info: BoundLogFunction
}
type LogLevelDetails = {
ico: string
color: (text: string) => string
level: number
}
// Constants
const levels: Record<LogLevel, LogLevelDetails> = {
debug: { ico: '🐛', color: green, level: 5 },
info: { ico: 'ℹ️', color: cyan, level: 9 },
warn: { ico: '⚠️', color: yellow, level: 13 },
error: { ico: '💥', color: red, level: 17 },
} as const
const colors = [
green,
yellow,
blue,
magenta,
cyan,
brightRed,
brightGreen,
brightYellow,
brightBlue,
brightMagenta,
brightCyan,
]
const colored: Record<string, string> = { 'Object.fetch': cyan('serve') }
const makePrettyTimestamp = (level: LogLevel, event: string) => {
const at = new Date()
const hh = String(at.getHours()).padStart(2, '0')
const mm = String(at.getMinutes()).padStart(2, '0')
const ss = String(at.getSeconds()).padStart(2, '0')
const ms = String(at.getMilliseconds()).padStart(2, '0').slice(0, 2)
const lvl = levels[level]
return `${gray(`${hh}h${mm}:${ss}.${ms}`)} ${lvl.ico} ${lvl.color(event)}`
}
const redactLongString = (_: string, value: unknown) => {
if (typeof value !== 'string') return value
return value.length > 100 ? 'long_string' : value
}
const bind = (log: LogFunction) =>
Object.assign(log, {
error: log.bind(null, 'error'),
debug: log.bind(null, 'debug'),
warn: log.bind(null, 'warn'),
info: log.bind(null, 'info'),
}) as Log
/**
* Initializes and returns a logger instance.
* The logger's behavior depends on the application environment (`APP_ENV`).
* In 'prod', it batches logs and sends them to a remote devtool service.
* In 'dev', it logs to the console with pretty colors and call chain information.
* In 'test', it logs to the console with timestamps.
*
* @param options - Configuration options for the logger.
* @returns A promise that resolves to a logger instance.
*
* @example
* ```ts
* import { logger } from '@01edu/log';
*
* const log = await logger({
* filters: new Set(['noisy_event']),
* });
*
* log.info('Application started');
* log.error('Something went wrong', { error: new Error('details') });
* ```
*/
export const logger = async ({
filters,
batchInterval = 5000,
maxBatchSize = 50,
logUrl = DEVTOOL_URL,
logToken = DEVTOOL_REPORT_TOKEN,
version = CI_COMMIT_SHA,
}: LoggerOptions): Promise<Log> => {
let logBatch: unknown[] = []
if (APP_ENV === 'prod' && (!logToken || !logUrl)) {
throw Error('DEVTOOLS configuration is required in production')
}
if (!version) {
try {
const p = new Deno.Command('git', {
args: ['rev-parse', 'HEAD'],
stdout: 'piped',
stderr: 'null',
})
const output = await p.output()
if (output.success) {
version = new TextDecoder().decode(output.stdout).trim()
}
} catch (err) {
if (err && APP_ENV === 'prod') {
throw Error('CI_COMMIT_SHA env needed for production')
}
version = 'unknown'
}
}
// DEVTOOLS Batch Logic
async function flushLogs() {
if (logBatch.length === 0) return
const batchToSend = logBatch
logBatch = []
try {
const response = await fetch(logUrl!, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${logToken}`,
},
body: JSON.stringify(batchToSend, redactLongString),
})
if (!response.ok) {
throw new Error(`HTTP ${response.status}: ${await response.text()}`)
}
} catch (err) {
console.error('DEVTOOLS batch send failed:', err)
logBatch = [...batchToSend, ...logBatch] // Requeue failed logs
}
}
// Loggers Implementation
const rootDir =
import.meta.dirname?.slice(0, -'/lib'.length).replaceAll('\\', '/') || ''
const f = filters || new Set()
if (APP_ENV === 'prod') {
// Initialize batch interval
const interval = setInterval(flushLogs, batchInterval)
// Cleanup on exit
const cleanup = async () => {
clearInterval(interval)
await flushLogs()
Deno.exit()
}
Deno.addSignalListener('SIGINT', cleanup)
Deno.addSignalListener('SIGTERM', cleanup)
return bind((level, event, props) => {
if (f.has(event)) return
const { trace, span } = getContext()
const logData = {
severity_number: levels[level].level,
trace_id: trace,
span_id: span,
event_name: event,
attributes: props,
timestamp: now() * 1000,
service_version: version,
service_instance_id: startTime.toString(),
}
// Local logging
console.log(event, props)
logBatch.push(logData)
logBatch.length >= maxBatchSize && flushLogs()
})
}
if (APP_ENV === 'test') {
return bind((level, event, props) => {
if (f.has(event)) return
const ev = makePrettyTimestamp(level, event)
props ? console[level](ev, props) : console[level](ev)
})
}
if (APP_ENV === 'dev') {
return bind((level, event, props) => {
if (f.has(event)) return
let callChain = ''
for (const s of Error('').stack!.split('\n').slice(2).reverse()) {
if (!s.includes(rootDir)) continue
const fnName = s.split(' ').at(-2)
if (!fnName || fnName === 'async' || fnName === 'at') continue
const coloredName = colored[fnName] ||
(colored[fnName] = colors
[Object.keys(colored).length % colors.length](
fnName,
))
callChain = callChain ? `${callChain}/${coloredName}` : coloredName
}
const ev = `${makePrettyTimestamp(level, event)} ${callChain}`.trim()
props ? console[level](ev, props) : console[level](ev)
})
}
throw Error('unknown APP_ENV')
}