Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: expose default prepare payload functions #71

Merged
merged 2 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,17 @@ const writeStream = createWriteStream({
To customize the payload, use the the `onPreparePayload` option:

```js
import { defaultPreparePayload } from "pino-logflare"

const writeStream = createWriteStream({
...,
// optional callback, by default, the received object will be nested under the `metadata` key
onPreparePayload: (payload, meta)=> {
// the `meta` arg contains cleaned information of raw payload
// You can add in top-level keys via this callback, or completely disable `metadata` key nesting by passing the payload as is, as shown below.
return payload
const item = defaultPreparePayload(payload, meta)
item["my_custom_key"] = "some value'
return item
}
});
```
Expand Down
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import createHttpWriteStream from "./httpStream"
import createConsoleWriteStream from "./consoleStream"
import { defaultPreparePayload, extractPayloadMeta } from "./utils"
import {
Level,
LogEvent,
Expand Down Expand Up @@ -47,4 +48,6 @@ export {
createPinoBrowserSend,
createConsoleWriteStream,
createHttpWriteStream,
defaultPreparePayload,
extractPayloadMeta,
}
21 changes: 20 additions & 1 deletion src/utils.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import {
formatPinoBrowserLogEvent,
addLogflareTransformDirectives,
handlePreparePayload
handlePreparePayload,
defaultPreparePayload,
extractPayloadMeta,
} from "./utils"

describe("utils", () => {
Expand Down Expand Up @@ -127,6 +129,23 @@ describe("utils", () => {
message: item.msg,
timestamp: item.time,
})

const meta = extractPayloadMeta(item)
expect(defaultPreparePayload(item, meta)).toMatchObject({
metadata: {
v: 1,
context: {
host: item.hostname,
service: item.service,
pid: item.pid,
stack: item.stack,
type: item.type,
},
level: "error",
},
message: item.msg,
timestamp: item.time,
})
})

it("correctly removes falsy items before adding them to context", () => {
Expand Down
12 changes: 6 additions & 6 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ function addLogflareTransformDirectives(
}
}

const extractMeta = (item: Record<string, any>) => {
export const extractPayloadMeta = (item: Record<string, any>) => {
const status = levelToStatus(item.level)
const message = item.msg || status
const host = item.hostname
Expand Down Expand Up @@ -153,7 +153,7 @@ const extractMeta = (item: Record<string, any>) => {
} as PayloadMeta
}

const defaultPreparePayload: PreparePayloadCallback = (_item, meta) => {
export const defaultPreparePayload: PreparePayloadCallback = (_item, meta) => {
return {
metadata: {
...meta.cleanedPayload,
Expand All @@ -166,11 +166,11 @@ const defaultPreparePayload: PreparePayloadCallback = (_item, meta) => {
}

const handlePreparePayload = (item: Record<string, any>, options: Options) => {
const meta = extractMeta(item)
const meta = extractPayloadMeta(item)
const callback =
options && options.onPreparePayload
? options.onPreparePayload
: defaultPreparePayload
options && options.onPreparePayload
? options.onPreparePayload
: defaultPreparePayload
const result = callback(item, meta)
return result
}
Expand Down
Loading