Skip to content

Commit

Permalink
Merge pull request #1134 from appsignal/pino-support
Browse files Browse the repository at this point in the history
Add Pino transport
  • Loading branch information
luismiramirez authored Oct 15, 2024
2 parents 6b0b4c6 + 46a6ad1 commit 87853d7
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 0 deletions.
18 changes: 18 additions & 0 deletions .changesets/add-pino-transport.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
bump: patch
type: add
---

A Pino transport is now available. If Pino is your main logger, you can now use the AppSignal pino transport to send those logs to AppSignal.

```js
import pino from "pino"
import { Appsignal, AppsignalPinoTransport } from "@appsignal/nodejs"

const logger = pino(
AppsignalPinoTransport({
client: Appsignal.client,
group: "application",
})
)
```
17 changes: 17 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"@prisma/instrumentation": ">= 5.11.0 < 5.14.0",
"node-addon-api": "^3.1.0",
"node-gyp": "^10.0.0",
"pino-abstract-transport": "^2.0.0",
"tslib": "^2.0.3",
"winston": "^3.6.0"
},
Expand Down
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@ export { WinstonTransport } from "./winston_transport"
export * from "./helpers"
export * as checkIn from "./check_in"
export { Heartbeat, heartbeat } from "./heartbeat"

import AppsignalPinoTransport from "./pino_transport"
export { AppsignalPinoTransport }
78 changes: 78 additions & 0 deletions src/pino_transport.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,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

0 comments on commit 87853d7

Please sign in to comment.