@@ -26,18 +26,18 @@ $ npm install @elastic/ecs-winston-format
26
26
27
27
[source,js]
28
28
----
29
- const winston = require('winston')
30
- const ecsFormat = require('@elastic/ecs-winston-format')
29
+ const winston = require('winston');
30
+ const { ecsFormat } = require('@elastic/ecs-winston-format');
31
31
32
32
const logger = winston.createLogger({
33
33
format: ecsFormat(/* options */), <1>
34
34
transports: [
35
35
new winston.transports.Console()
36
36
]
37
- })
37
+ });
38
38
39
- logger.info('hi')
40
- logger.error('oops there is a problem', { err: new Error('boom') })
39
+ logger.info('hi');
40
+ logger.error('oops there is a problem', { err: new Error('boom') });
41
41
----
42
42
<1> Pass the ECS formatter to winston here.
43
43
@@ -58,19 +58,19 @@ NOTE: You might like to try out our tutorial using Node.js ECS logging with wins
58
58
59
59
[source,js]
60
60
----
61
- const winston = require('winston')
62
- const ecsFormat = require('@elastic/ecs-winston-format')
61
+ const winston = require('winston');
62
+ const { ecsFormat } = require('@elastic/ecs-winston-format');
63
63
64
64
const logger = winston.createLogger({
65
65
level: 'info',
66
66
format: ecsFormat(/* options */), <1>
67
67
transports: [
68
68
new winston.transports.Console()
69
69
]
70
- })
70
+ });
71
71
72
- logger.info('hi')
73
- logger.error('oops there is a problem', { foo: 'bar' })
72
+ logger.info('hi');
73
+ logger.error('oops there is a problem', { foo: 'bar' });
74
74
----
75
75
<1> See available options <<winston-ref,below>>.
76
76
@@ -99,17 +99,17 @@ For https://github.com/elastic/ecs-logging-nodejs/blob/main/packages/ecs-winston
99
99
100
100
[source,js]
101
101
----
102
- const winston = require('winston')
103
- const ecsFormat = require('@elastic/ecs-winston-format')
102
+ const winston = require('winston');
103
+ const { ecsFormat } = require('@elastic/ecs-winston-format');
104
104
const logger = winston.createLogger({
105
105
format: ecsFormat(), <1>
106
106
transports: [
107
107
new winston.transports.Console()
108
108
]
109
- })
109
+ });
110
110
111
- const myErr = new Error('boom')
112
- logger.info('oops', { err: myErr }) <2>
111
+ const myErr = new Error('boom');
112
+ logger.info('oops', { err: myErr }); <2>
113
113
----
114
114
115
115
will yield (pretty-printed for readability):
@@ -153,27 +153,27 @@ objects when passed as the `req` and `res` meta fields, respectively.
153
153
154
154
[source,js]
155
155
----
156
- const http = require('http')
157
- const winston = require('winston')
158
- const ecsFormat = require('@elastic/ecs-winston-format')
156
+ const http = require('http');
157
+ const winston = require('winston');
158
+ const { ecsFormat } = require('@elastic/ecs-winston-format');
159
159
160
160
const logger = winston.createLogger({
161
161
level: 'info',
162
162
format: ecsFormat({ convertReqRes: true }), <1>
163
163
transports: [
164
164
new winston.transports.Console()
165
165
]
166
- })
166
+ });
167
167
168
- const server = http.createServer(handler)
168
+ const server = http.createServer(handler);
169
169
server.listen(3000, () => {
170
170
logger.info('listening at http://localhost:3000')
171
- })
171
+ });
172
172
173
173
function handler (req, res) {
174
- res.setHeader('Foo', 'Bar')
175
- res.end('ok')
176
- logger.info('handled request', { req, res }) <2>
174
+ res.setHeader('Foo', 'Bar');
175
+ res.end('ok');
176
+ logger.info('handled request', { req, res }); <2>
177
177
}
178
178
----
179
179
<1> use `convertReqRes` option
@@ -271,6 +271,18 @@ const logger = winston.createLogger({
271
271
})
272
272
----
273
273
274
+ [float]
275
+ [[winston-limitations]]
276
+ === Limitations and Considerations
277
+
278
+ The https://github.com/elastic/ecs-logging/tree/main/spec[ecs-logging spec]
279
+ suggests that the first three fields in log records should be `@timestamp`,
280
+ `log.level`, and `message`. As of version 1.5.0, this formatter does *not*
281
+ follow this suggestion. It would be possible but would require creating a new
282
+ Object in `ecsFields` for each log record. Given that ordering of ecs-logging
283
+ fields is for *human readability* and does not affect interoperability, the
284
+ decision was made to prefer performance.
285
+
274
286
[float]
275
287
[[winston-ref]]
276
288
=== Reference
@@ -289,4 +301,49 @@ const logger = winston.createLogger({
289
301
** `serviceNodeName` +{type-string}+ A "service.node.name" value. If specified this overrides any value from an active APM agent.
290
302
** `eventDataset` +{type-string}+ A "event.dataset" value. If specified this overrides the default of using `${serviceVersion}`.
291
303
292
- Create a formatter for winston that emits in ECS Logging format.
304
+ Create a formatter for winston that emits in ECS Logging format. This is a
305
+ single format that handles both <<winston-ref-ecsFields>> and <<winston-ref-ecsStringify>>.
306
+ The following two are equivalent:
307
+
308
+ [source,js]
309
+ ----
310
+ const { ecsFormat, ecsFields, ecsStringify } = require('@elastic/ecs-winston-format');
311
+ const winston = require('winston');
312
+
313
+ const logger = winston.createLogger({
314
+ format: ecsFormat(/* options */),
315
+ // ...
316
+ });
317
+
318
+ const logger = winston.createLogger({
319
+ format: winston.format.combine(
320
+ ecsFields(/* options */),
321
+ ecsStringify()
322
+ ),
323
+ // ...
324
+ });
325
+ ----
326
+
327
+ [float]
328
+ [[winston-ref-ecsFields]]
329
+ ==== `ecsFields([options])`
330
+
331
+ * `options` +{type-object}+ The following options are supported:
332
+ ** `convertErr` +{type-boolean}+ Whether to convert a logged `err` field to ECS error fields. *Default:* `true`.
333
+ ** `convertReqRes` +{type-boolean}+ Whether to logged `req` and `res` HTTP request and response fields to ECS HTTP, User agent, and URL fields. *Default:* `false`.
334
+ ** `apmIntegration` +{type-boolean}+ Whether to enable APM agent integration. *Default:* `true`.
335
+ ** `serviceName` +{type-string}+ A "service.name" value. If specified this overrides any value from an active APM agent.
336
+ ** `serviceVersion` +{type-string}+ A "service.version" value. If specified this overrides any value from an active APM agent.
337
+ ** `serviceEnvironment` +{type-string}+ A "service.environment" value. If specified this overrides any value from an active APM agent.
338
+ ** `serviceNodeName` +{type-string}+ A "service.node.name" value. If specified this overrides any value from an active APM agent.
339
+ ** `eventDataset` +{type-string}+ A "event.dataset" value. If specified this overrides the default of using `${serviceVersion}`.
340
+
341
+ Create a formatter for winston that converts fields on the log record info
342
+ objecct to ECS Logging format.
343
+
344
+ [float]
345
+ [[winston-ref-ecsStringify]]
346
+ ==== `ecsStringify([options])`
347
+
348
+ Create a formatter for winston that stringifies/serializes the log record to
349
+ JSON. (This is very similar to `logform.json()`.)
0 commit comments