Skip to content

Commit 80e7983

Browse files
committed
feat: support multiple import styles from JS and TS; add morgan types
This updates the three @elastic/ecs-*-format packages to support the following import styles from JS and TS code: 1. `const { ecsFormat } = require('@elastic/ecs-pino-format);` in JS and TS. The preferred import style for JS code using CommonJS. 2. `import { ecsFormat } from '@elastic/ecs-pino-format';` in JS and TS. ES module (ESM) import style. This is the preferred style for TypeScript code and for JS developers using ESM. 3. `const ecsFormat = require('@elastic/ecs-pino-format');` in JS. The old, deprecated import method. Still supported for backward compat. 4. `import ecsFormat from '@elastic/ecs-pino-format';` in JS and TS. This works, but is deprecated. Prefer #2 style. 5. `import * as EcsPinoFormat from '@elastic/ecs-pino-format';` in TS. One must then use `EcsPinoFormat.ecsFormat()`. Note that this *excludes* support for this TS-only style: `import escFormat = require('@elastic/ecs-pino-format');` This also adds types for ecs-morgan-format, based on #119 and #90. I'd had an earlier start on this in #96. Replaces: #96 Closes: #90 Closes: #119
1 parent 7c82a8d commit 80e7983

40 files changed

+409
-121
lines changed

docs/morgan.asciidoc

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,17 @@ $ npm install @elastic/ecs-morgan-format
2828

2929
[source,js]
3030
----
31-
const app = require('express')()
32-
const morgan = require('morgan')
33-
const ecsFormat = require('@elastic/ecs-morgan-format')
31+
const app = require('express')();
32+
const morgan = require('morgan');
33+
const { ecsFormat } = require('@elastic/ecs-morgan-format');
3434
35-
app.use(morgan(ecsFormat(/* options */))) <1>
35+
app.use(morgan(ecsFormat(/* options */))); <1>
3636
3737
// ...
3838
app.get('/', function (req, res) {
39-
res.send('hello, world!')
39+
res.send('hello, world!');
4040
})
41-
app.listen(3000)
41+
app.listen(3000);
4242
----
4343
<1> Pass the ECS formatter to `morgan()`.
4444

@@ -58,17 +58,17 @@ include::{ecs-repo-dir}/setup.asciidoc[tag=configure-filebeat]
5858

5959
[source,js]
6060
----
61-
const app = require('express')()
62-
const morgan = require('morgan')
63-
const ecsFormat = require('@elastic/ecs-morgan-format')
61+
const app = require('express')();
62+
const morgan = require('morgan');
63+
const { ecsFormat } = require('@elastic/ecs-morgan-format');
6464
65-
app.use(morgan(ecsFormat(/* options */))) <1>
65+
app.use(morgan(ecsFormat(/* options */))); <1>
6666
6767
app.get('/', function (req, res) {
68-
res.send('hello, world!')
68+
res.send('hello, world!');
6969
})
7070
app.get('/error', function (req, res, next) {
71-
next(new Error('boom'))
71+
next(new Error('boom'));
7272
})
7373
7474
app.listen(3000)
@@ -129,11 +129,11 @@ specified format. The default is https://github.com/expressjs/morgan#combined[`c
129129

130130
[source,js]
131131
----
132-
const app = require('express')()
133-
const morgan = require('morgan')
134-
const ecsFormat = require('@elastic/ecs-morgan-format')
132+
const app = require('express')();
133+
const morgan = require('morgan');
134+
const { ecsFormat } = require('@elastic/ecs-morgan-format');
135135
136-
app.use(morgan(ecsFormat({ format: 'tiny' }))) <1>
136+
app.use(morgan(ecsFormat({ format: 'tiny' }))); <1>
137137
// ...
138138
----
139139
<1> If "format" is the only option you are using, you may pass it as `ecsFormat('tiny')`.
@@ -201,7 +201,7 @@ Integration with Elastic APM can be explicitly disabled via the
201201

202202
[source,js]
203203
----
204-
app.use(morgan(ecsFormat({ apmIntegration: false })))
204+
app.use(morgan(ecsFormat({ apmIntegration: false })));
205205
----
206206

207207

docs/pino.asciidoc

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ $ npm install @elastic/ecs-pino-format
2626

2727
[source,js]
2828
----
29-
const ecsFormat = require('@elastic/ecs-pino-format')
30-
const pino = require('pino')
29+
const { ecsFormat } = require('@elastic/ecs-pino-format');
30+
const pino = require('pino');
3131
32-
const log = pino(ecsFormat(/* options */)) <1>
33-
log.info('hi')
34-
log.error({ err: new Error('boom') }, 'oops there is a problem')
32+
const log = pino(ecsFormat(/* options */)); <1>
33+
log.info('hi');
34+
log.error({ err: new Error('boom') }, 'oops there is a problem');
3535
// ...
3636
----
3737
<1> This will https://getpino.io/#/docs/api?id=options[configure] Pino's `formatters`, `messageKey` and `timestamp` options.
@@ -53,14 +53,14 @@ include::{ecs-repo-dir}/setup.asciidoc[tag=configure-filebeat]
5353

5454
[source,js]
5555
----
56-
const ecsFormat = require('@elastic/ecs-pino-format')
57-
const pino = require('pino')
56+
const { ecsFormat } = require('@elastic/ecs-pino-format');
57+
const pino = require('pino');
5858
59-
const log = pino(ecsFormat(/* options */)) <1>
60-
log.info('Hello world')
59+
const log = pino(ecsFormat(/* options */)); <1>
60+
log.info('Hello world');
6161
62-
const child = log.child({ module: 'foo' })
63-
child.warn('From child')
62+
const child = log.child({ module: 'foo' });
63+
child.warn('From child');
6464
----
6565
<1> See available options <<pino-ref,below>>.
6666

@@ -82,12 +82,12 @@ For example:
8282

8383
[source,js]
8484
----
85-
const ecsFormat = require('@elastic/ecs-pino-format')
86-
const pino = require('pino')
87-
const log = pino(ecsFormat())
85+
const { ecsFormat } = require('@elastic/ecs-pino-format');
86+
const pino = require('pino');
87+
const log = pino(ecsFormat());
8888
89-
const myErr = new Error('boom')
90-
log.info({ err: myErr }, 'oops')
89+
const myErr = new Error('boom');
90+
log.info({ err: myErr }, 'oops');
9191
----
9292

9393
will yield (pretty-printed for readability):
@@ -114,7 +114,7 @@ Special handling of the `err` field can be disabled via the `convertErr: false`
114114

115115
[source,js]
116116
----
117-
const log = pino(ecsFormat({ convertErr: false }))
117+
const log = pino(ecsFormat({ convertErr: false }));
118118
----
119119

120120

@@ -130,20 +130,20 @@ objects when passed as the `req` and `res` fields, respectively.
130130

131131
[source,js]
132132
----
133-
const http = require('http')
134-
const ecsFormat = require('@elastic/ecs-pino-format')
135-
const pino = require('pino')
133+
const http = require('http');
134+
const { ecsFormat } = require('@elastic/ecs-pino-format');
135+
const pino = require('pino');
136136
137-
const log = pino(ecsFormat({ convertReqRes: true })) <1>
137+
const log = pino(ecsFormat({ convertReqRes: true })); <1>
138138
139139
const server = http.createServer(function handler (req, res) {
140-
res.setHeader('Foo', 'Bar')
141-
res.end('ok')
142-
log.info({ req, res }, 'handled request') <2>
143-
})
140+
res.setHeader('Foo', 'Bar');
141+
res.end('ok');
142+
log.info({ req, res }, 'handled request'); <2>
143+
});
144144
145145
server.listen(3000, () => {
146-
log.info('listening at http://localhost:3000')
146+
log.info('listening at http://localhost:3000');
147147
}
148148
----
149149
<1> use `convertReqRes` option
@@ -243,7 +243,7 @@ Integration with Elastic APM can be explicitly disabled via the
243243

244244
[source,js]
245245
----
246-
const log = pino(ecsFormat({ apmIntegration: false }))
246+
const log = pino(ecsFormat({ apmIntegration: false }));
247247
----
248248

249249

packages/ecs-morgan-format/CHANGELOG.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,26 @@
88

99
- Set `http.request.id` field (see [ecs-helpers CHANGELOG](../ecs-helpers/CHANGELOG.md#v210)).
1010

11+
- Changed to a named export. The preferred way to import is now:
12+
13+
```js
14+
const { ecsFormat } = require('@elastic/ecs-morgan-format'); // CommonJS
15+
import { ecsFormat } from '@elastic/ecs-morgan-format'; // ESM
16+
```
17+
18+
The old way will be deprecated and removed in the future:
19+
20+
```js
21+
const ecsFormat = require('@elastic/ecs-morgan-format'); // OLD
22+
```
23+
24+
Changes in this version add support for default import in TypeScript, with or
25+
without the `esModuleInterop` setting:
26+
27+
```ts
28+
import ecsFormat from '@elastic/ecs-morgan-format';
29+
```
30+
1131
## v1.4.0
1232

1333
- Add `service.version`, `service.environment`, and `service.node.name` log

packages/ecs-morgan-format/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ npm install @elastic/ecs-morgan-format
2727
```js
2828
const app = require('express')()
2929
const morgan = require('morgan')
30-
const ecsFormat = require('@elastic/ecs-morgan-format')
30+
const { ecsFormat } = require('@elastic/ecs-morgan-format')
3131

3232
app.use(morgan(ecsFormat(/* options */)))
3333

packages/ecs-morgan-format/examples/express-with-apm.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ const apm = require('elastic-apm-node').start({
3838

3939
const app = require('express')()
4040
const morgan = require('morgan')
41-
const ecsFormat = require('../') // @elastic/ecs-morgan-format
41+
const { ecsFormat } = require('../') // @elastic/ecs-morgan-format
4242

4343
app.use(morgan(ecsFormat()))
4444

packages/ecs-morgan-format/examples/express.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
const app = require('express')()
2121
const morgan = require('morgan')
22-
const ecsFormat = require('../') // @elastic/ecs-morgan-format
22+
const { ecsFormat } = require('../') // @elastic/ecs-morgan-format
2323

2424
app.use(morgan(ecsFormat()))
2525

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

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import type { FormatFn } from "morgan";
2+
3+
interface Config {
4+
/**
5+
* A format *name* (e.g. 'combined'), format function (e.g. `morgan.combined`),
6+
* or a format string (e.g. ':method :url :status').
7+
*
8+
* This is used to format the "message" field.
9+
* @default `morgan.combined`
10+
*/
11+
format: string;
12+
13+
/**
14+
* Whether to automatically integrate with
15+
* Elastic APM (https://github.com/elastic/apm-agent-nodejs). If a started
16+
* APM agent is detected, then log records will include the following
17+
* fields:
18+
*
19+
* - "trace.id", "transaction.id", and "span.id" - if there is a current
20+
* active trace when the log call is made
21+
*
22+
* and also the following fields, if not already specified in this config:
23+
*
24+
* - "service.name" - the configured `serviceName` in the agent
25+
* - "service.version" - the configured `serviceVersion` in the agent
26+
* - "service.environment" - the configured `environment` in the agent
27+
* - "service.node.name" - the configured `serviceNodeName` in the agent
28+
* - "event.dataset" - set to `${serviceName}` for correlation in Kibana
29+
*
30+
* @default true.
31+
*/
32+
apmIntegration?: boolean;
33+
34+
/** Specify "service.name" field. Defaults to a value from the APM agent, if available. */
35+
serviceName?: string;
36+
/** Specify "service.version" field. Defaults to a value from the APM agent, if available. */
37+
serviceVersion?: string;
38+
/** Specify "service.environment" field. Defaults to a value from the APM agent, if available. */
39+
serviceEnvironment?: string;
40+
/** Specify "service.node.name" field. Defaults to a value from the APM agent, if available. */
41+
serviceNodeName?: string;
42+
/** Specify "event.dataset" field. Defaults `${serviceName}`. */
43+
eventDataset?: string;
44+
45+
}
46+
47+
declare function ecsFormat(config?: Config): FormatFn;
48+
49+
export default ecsFormat;
50+
export { ecsFormat }

packages/ecs-morgan-format/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,4 +179,8 @@ function ecsFormat (opts) {
179179
}
180180
}
181181

182+
// For backwards compatibility with v1.0.0, the top-level export is `ecsFormat`,
183+
// though using the named export is preferred.
182184
module.exports = ecsFormat
185+
module.exports.ecsFormat = ecsFormat
186+
module.exports.default = ecsFormat

packages/ecs-morgan-format/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"version": "1.4.0",
44
"description": "A formatter for the morgan logger compatible with Elastic Common Schema.",
55
"main": "index.js",
6+
"types": "index.d.ts",
67
"files": [
78
"index.js"
89
],

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const morgan = require('morgan')
2626
const split = require('split2')
2727
const test = require('tap').test
2828

29-
const ecsFormat = require('../')
29+
const { ecsFormat } = require('../')
3030
const { ecsLoggingValidate } = require('../../../utils/lib/ecs-logging-validate')
3131

3232
const ajv = new Ajv({

packages/ecs-morgan-format/test/serve-one-http-req-with-apm.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ const apm = require('elastic-apm-node').start({
4444
const app = require('express')()
4545
const http = require('http')
4646
const morgan = require('morgan')
47-
const ecsFormat = require('../') // @elastic/ecs-morgan-format
47+
const { ecsFormat } = require('../') // @elastic/ecs-morgan-format
4848

4949
const ecsOpts = { serviceVersion: 'override-serviceVersion' }
5050
if (disableApmIntegration) {

packages/ecs-pino-format/CHANGELOG.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,26 @@
88

99
- Set `http.request.id` field (see [ecs-helpers CHANGELOG](../ecs-helpers/CHANGELOG.md#v210)).
1010

11+
- Changed to a named export. The preferred way to import is now:
12+
13+
```js
14+
const { ecsFormat } = require('@elastic/ecs-pino-format'); // CommonJS
15+
import { ecsFormat } from '@elastic/ecs-pino-format'; // ESM
16+
```
17+
18+
The old way will be deprecated and removed in the future:
19+
20+
```js
21+
const ecsFormat = require('@elastic/ecs-pino-format'); // OLD
22+
```
23+
24+
Changes in this version add support for default import in TypeScript,
25+
with or without the `esModuleInterop` setting:
26+
27+
```ts
28+
import ecsFormat from '@elastic/ecs-pino-format';
29+
```
30+
1131
## v1.4.0
1232

1333
- Add `service.version`, `service.environment`, and `service.node.name` log

packages/ecs-pino-format/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ npm install @elastic/ecs-pino-format
2828
This package will configure Pino's `formatters`, `messageKey` and `timestamp` options.
2929

3030
```js
31-
const ecsFormat = require('@elastic/ecs-pino-format')
31+
const { ecsFormat } = require('@elastic/ecs-pino-format')
3232
const pino = require('pino')
3333

3434
const log = pino(ecsFormat(/* options */))

packages/ecs-pino-format/examples/basic.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
'use strict'
1919

20-
const ecsFormat = require('../') // @elastic/ecs-pino-format
20+
const { ecsFormat } = require('../') // @elastic/ecs-pino-format
2121
const pino = require('pino')
2222

2323
const log = pino(ecsFormat())

packages/ecs-pino-format/examples/error.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18-
const ecsFormat = require('..') // @elastic/ecs-pino-format
18+
const { ecsFormat } = require('..') // @elastic/ecs-pino-format
1919
const pino = require('pino')
2020
const log = pino({ ...ecsFormat() })
2121

packages/ecs-pino-format/examples/express-simple.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
// This shows how one could use @elastic/ecs-pino-format with Express.
2121
// This implements simple Express middleware to do so.
2222

23-
const ecsFormat = require('../') // @elastic/ecs-pino-format
23+
const { ecsFormat } = require('../') // @elastic/ecs-pino-format
2424
const express = require('express')
2525
const pino = require('pino')
2626

packages/ecs-pino-format/examples/express-with-pino-http.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
// TODO: doc the log.child({req}) limitation
2424
// TODO: doc pino-http's 'req.id' and ECS translation to 'event.id'
2525

26-
const ecsFormat = require('../') // @elastic/ecs-pino-format
26+
const { ecsFormat } = require('../') // @elastic/ecs-pino-format
2727
const express = require('express')
2828
const pino = require('pino')
2929
const pinoHttp = require('pino-http')

packages/ecs-pino-format/examples/http-with-elastic-apm.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ const apm = require('elastic-apm-node').start({
3838

3939
const http = require('http')
4040
const https = require('https')
41-
const ecsFormat = require('../') // @elastic/ecs-pino-format
41+
const { ecsFormat } = require('../') // @elastic/ecs-pino-format
4242
const pino = require('pino')
4343

4444
const log = pino({ ...ecsFormat({ convertReqRes: true }) })

0 commit comments

Comments
 (0)