-
Notifications
You must be signed in to change notification settings - Fork 9
/
pino_transport.ts
78 lines (67 loc) · 1.75 KB
/
pino_transport.ts
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
import { LOGGER_LEVEL_SEVERITY } from "./logger"
import build from "pino-abstract-transport"
import { Client } from "./client"
type PinoTransportOptions = {
client: Client
group: string
}
const appsignalPinoTransport = ({
client,
group = "app"
}: PinoTransportOptions) => {
return build(async (source: any) => {
for await (const obj of source) {
sendLogs(parseInfo(obj, group), client)
}
})
async function sendLogs(data: Record<string, any>, client: Client) {
client.extension.log(
data.group || "app",
data.severity,
0,
data.msg,
data.attributes
)
}
}
function parseInfo(
obj: Record<string, any>,
group: string
): Record<string, any> {
const { hostname, level, msg, ...attributes } = obj
return {
severity: getSeverity(level),
hostname,
group,
msg,
attributes: flattenAttributes(attributes)
}
}
function flattenAttributes(
attributes: Record<string, any>,
prefix = ""
): Record<string, any> {
let result: Record<string, any> = {}
for (const key in attributes) {
const newKey = prefix ? `${prefix}.${key}` : key
if (
typeof attributes[key] === "object" &&
attributes[key] !== null &&
!Array.isArray(attributes[key])
) {
const flattened = flattenAttributes(attributes[key], newKey)
result = { ...result, ...flattened }
} else {
result[newKey] = attributes[key]
}
}
return result
}
function getSeverity(level: number): number {
if (level >= 50) return LOGGER_LEVEL_SEVERITY.error
if (level >= 40) return LOGGER_LEVEL_SEVERITY.warn
if (level >= 30) return LOGGER_LEVEL_SEVERITY.info
if (level >= 20) return LOGGER_LEVEL_SEVERITY.debug
return LOGGER_LEVEL_SEVERITY.trace
}
export = appsignalPinoTransport