@@ -26,18 +26,18 @@ $ npm install @elastic/ecs-winston-format
2626
2727[source,js]
2828----
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');
3131
3232const logger = winston.createLogger({
3333 format: ecsFormat(/* options */), <1>
3434 transports: [
3535 new winston.transports.Console()
3636 ]
37- })
37+ });
3838
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') });
4141----
4242<1> Pass the ECS formatter to winston here.
4343
@@ -58,19 +58,19 @@ NOTE: You might like to try out our tutorial using Node.js ECS logging with wins
5858
5959[source,js]
6060----
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');
6363
6464const logger = winston.createLogger({
6565 level: 'info',
6666 format: ecsFormat(/* options */), <1>
6767 transports: [
6868 new winston.transports.Console()
6969 ]
70- })
70+ });
7171
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' });
7474----
7575<1> See available options <<winston-ref,below>>.
7676
@@ -99,17 +99,17 @@ For https://github.com/elastic/ecs-logging-nodejs/blob/main/packages/ecs-winston
9999
100100[source,js]
101101----
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');
104104const logger = winston.createLogger({
105105 format: ecsFormat(), <1>
106106 transports: [
107107 new winston.transports.Console()
108108 ]
109- })
109+ });
110110
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>
113113----
114114
115115will yield (pretty-printed for readability):
@@ -153,27 +153,27 @@ objects when passed as the `req` and `res` meta fields, respectively.
153153
154154[source,js]
155155----
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');
159159
160160const logger = winston.createLogger({
161161 level: 'info',
162162 format: ecsFormat({ convertReqRes: true }), <1>
163163 transports: [
164164 new winston.transports.Console()
165165 ]
166- })
166+ });
167167
168- const server = http.createServer(handler)
168+ const server = http.createServer(handler);
169169server.listen(3000, () => {
170170 logger.info('listening at http://localhost:3000')
171- })
171+ });
172172
173173function 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>
177177}
178178----
179179<1> use `convertReqRes` option
@@ -271,6 +271,18 @@ const logger = winston.createLogger({
271271})
272272----
273273
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+
274286[float]
275287[[winston-ref]]
276288=== Reference
@@ -289,4 +301,49 @@ const logger = winston.createLogger({
289301** `serviceNodeName` +{type-string}+ A "service.node.name" value. If specified this overrides any value from an active APM agent.
290302** `eventDataset` +{type-string}+ A "event.dataset" value. If specified this overrides the default of using `${serviceVersion}`.
291303
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