-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1134 from appsignal/pino-support
Add Pino transport
- Loading branch information
Showing
5 changed files
with
117 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
}) | ||
) | ||
``` |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |