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

Support field customization #179

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
30 changes: 30 additions & 0 deletions docs/morgan.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,35 @@ Integration with Elastic APM can be explicitly disabled via the
app.use(morgan(ecsFormat({ apmIntegration: false })));
----

[float]
[[morgan-logHook]]
=== Customizing Log Records

To add custom fields to log records or remove sensitive data, you can use the `logHook` option. This option is a function that takes the formatted log record and returns a new log record. Here's an example that adds a label to the log record and removes the `authorization` and `cookie` headers:

[source,js]
----
app.use(morgan(ecsFormat({
logHook: ({ record, req, res }) => {
// Omit sensitive headers
const { authorization, cookie, ...headers } = record.http.request.headers;
return {
...record,
labels: {
...record.labels,
customLabel: res.locals.someValue
},
http: {
...record.http,
request: {
...record.http.request,
headers
}
}
};
},
})));
----

[float]
[[morgan-ref]]
Expand All @@ -222,5 +251,6 @@ app.use(morgan(ecsFormat({ apmIntegration: false })));
** `serviceEnvironment` +{type-string}+ A "service.environment" value. If specified this overrides any value from an active APM agent.
** `serviceNodeName` +{type-string}+ A "service.node.name" value. If specified this overrides any value from an active APM agent.
** `eventDataset` +{type-string}+ A "event.dataset" value. If specified this overrides the default of using `${serviceVersion}`.
** `logHook` +{type-function}+ A function that takes the formatted log record and returns a new log record. This can be used to add custom fields to the log record or remove sensitive data. It is passed an object with three properties: `record`, `req`, and `res`.

Create a formatter for morgan that emits in ECS Logging format.
3 changes: 3 additions & 0 deletions packages/ecs-morgan-format/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { IncomingMessage, ServerResponse } from 'http';
import type { FormatFn } from "morgan";

interface Config {
Expand Down Expand Up @@ -42,6 +43,8 @@ interface Config {
/** Specify "event.dataset" field. Defaults `${serviceName}`. */
eventDataset?: string;

/** Callback for custom modification of the fields */
logHook: (event: { record: any, req: IncomingMessage, res: ServerResponse }) => void;
}

declare function ecsFormat(config?: Config): FormatFn;
Expand Down
2 changes: 2 additions & 0 deletions packages/ecs-morgan-format/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,8 @@ function ecsFormat (opts) {
formatHttpRequest(ecsFields, req)
formatHttpResponse(ecsFields, res)

opts.logHook && opts.logHook({ record: ecsFields, req, res })

return stringify(ecsFields)
}
}
Expand Down
23 changes: 23 additions & 0 deletions packages/ecs-morgan-format/test/basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,3 +235,26 @@ test('can configure correlation fields', t => {
t.end()
})
})

test('can provide custom fields', t => {
t.plan(2)

const stream = split().on('data', line => {
const rec = JSON.parse(line)
t.equal(rec.labels.custom, 'customValue')
})
const logger = morgan(
ecsFormat({
logHook ({ record, req, res }) {
record.labels = record.labels || {}
record.labels.custom = 'customValue'
}
}),
{ stream }
)

makeExpressServerAndRequest(logger, '/', {}, null, function (err) {
t.error(err)
t.end()
})
})