Skip to content

Commit 5632bcb

Browse files
committed
Pull request suggestions
- Add docs - Change signature of customization function - Change test to use old JavaScript syntax
1 parent 6ef1c8a commit 5632bcb

File tree

4 files changed

+35
-5
lines changed

4 files changed

+35
-5
lines changed

docs/morgan.asciidoc

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,35 @@ Integration with Elastic APM can be explicitly disabled via the
204204
app.use(morgan(ecsFormat({ apmIntegration: false })));
205205
----
206206

207+
[float]
208+
[[morgan-logHook]]
209+
=== Customizing Log Records
210+
211+
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:
212+
213+
[source,js]
214+
----
215+
app.use(morgan(ecsFormat({
216+
logHook: ({ record, req, res }) => {
217+
// Omit sensitive headers
218+
const { authorization, cookie, ...headers } = record.http.request.headers;
219+
return {
220+
...record,
221+
labels: {
222+
...record.labels,
223+
customLabel: res.locals.someValue
224+
},
225+
http: {
226+
...record.http,
227+
request: {
228+
...record.http.request,
229+
headers
230+
}
231+
}
232+
};
233+
},
234+
})));
235+
----
207236

208237
[float]
209238
[[morgan-ref]]
@@ -222,5 +251,6 @@ app.use(morgan(ecsFormat({ apmIntegration: false })));
222251
** `serviceEnvironment` +{type-string}+ A "service.environment" value. If specified this overrides any value from an active APM agent.
223252
** `serviceNodeName` +{type-string}+ A "service.node.name" value. If specified this overrides any value from an active APM agent.
224253
** `eventDataset` +{type-string}+ A "event.dataset" value. If specified this overrides the default of using `${serviceVersion}`.
254+
** `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`.
225255

226256
Create a formatter for morgan that emits in ECS Logging format.

packages/ecs-morgan-format/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ interface Config {
4444
eventDataset?: string;
4545

4646
/** Callback for custom modification of the fields */
47-
customize: (fields: any, req: IncomingMessage, res: ServerResponse) => void;
47+
logHook: (event: { record: any, req: IncomingMessage, res: ServerResponse }) => void;
4848
}
4949

5050
declare function ecsFormat(config?: Config): FormatFn;

packages/ecs-morgan-format/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ function ecsFormat (opts) {
175175
formatHttpRequest(ecsFields, req)
176176
formatHttpResponse(ecsFields, res)
177177

178-
opts.customize && opts.customize(ecsFields, req, res);
178+
opts.logHook && opts.logHook({ record: ecsFields, req, res })
179179

180180
return stringify(ecsFields)
181181
}

packages/ecs-morgan-format/test/basic.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -245,9 +245,9 @@ test('can provide custom fields', t => {
245245
})
246246
const logger = morgan(
247247
ecsFormat({
248-
customize(fields, req, res) {
249-
fields.labels ??= {};
250-
fields.labels.custom = 'customValue';
248+
logHook ({ record, req, res }) {
249+
record.labels = record.labels || {}
250+
record.labels.custom = 'customValue'
251251
}
252252
}),
253253
{ stream }

0 commit comments

Comments
 (0)