Skip to content

Commit b3a1b92

Browse files
akapagergaczd
andcommitted
fix(readme): add details about new ecs format
AUT-2249 Co-authored-by: Daniel Gergacz <[email protected]>
1 parent 82b499d commit b3a1b92

File tree

1 file changed

+28
-9
lines changed

1 file changed

+28
-9
lines changed

README.md

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,21 @@ It has the same namespace based enabling/disabling mechanism as [debug].
99
npm install @emartech/json-logger
1010
```
1111

12+
### Usage
1213

14+
Since 8.0.0, by default ECS fields will be used when logging.
1315

14-
### Usage
16+
If for reason you still need the old format, you need to override the `outputFormat` config.
17+
`configure` will apply this setting globally, for all instances of the logger.
18+
19+
```javascript
20+
const { createLogger } = require('@emartech/json-logger');
21+
22+
createLogger.configure({
23+
outputFormat: 'legacy'
24+
});
25+
26+
```
1527

1628
#### Script
1729

@@ -22,13 +34,15 @@ const mongoLogger = createLogger('mongo');
2234
const redisLogger = createLogger('redis');
2335

2436
redisLogger.info('connected', { domain: 'yahoo' });
25-
// {"name":"redis","action":"connected","level":30,"time":"2016-08-15T08:50:23.566Z","domain":"yahoo"}
37+
// ECS format: {"event":{"action":"connected"},"log":{"logger":"redis","level":30},"@timestamp":"2016-08-15T08:50:23.566Z","domain":"yahoo"}
38+
// Legacy format: {"name":"redis","action":"connected","level":30,"time":"2016-08-15T08:50:23.566Z","domain":"yahoo"}
2639

2740
mongoLogger.info('connected', { domain: 'google' });
2841
// no output, because 'mongo' is not within namespaces (process.env.DEBUG)
2942

3043
redisLogger.fromError('query', new Error('Unauthorized'), { problem: 'missmatch' });
31-
// {"name":"redis","action":"query","level":50,"time":"2016-08-15T08:50:23.569Z","error_name":"Error","error_stack":"Error: Unauthorized\n at Object.<anonymous> (/home/blacksonic/workspace/bunyan-debug/example.js:15:32)\n at Module._compile (module.js:541:32)\n at Object.Module._extensions..js (module.js:550:10)\n at Module.load (module.js:458:32)\n at tryModuleLoad (module.js:417:12)\n at Function.Module._load (module.js:409:3)\n at Module.runMain (module.js:575:10)\n at run (bootstrap_node.js:352:7)\n at startup (bootstrap_node.js:144:9)\n at bootstrap_node.js:467:3","error_message":"Unauthorized","problem":"missmatch"}
44+
// ECS format: {"event":{"action":"query"},"log":{"logger":"redis","level":50},"@timestamp":"2016-08-15T08:50:23.569Z","error":{"type":"Error","message":"Unauthorized","stack_trace":"..."},"problem":"mismatch"}
45+
// Legacy format: {"name":"redis","action":"query","level":50,"time":"2016-08-15T08:50:23.569Z","error_name":"Error","error_stack":"Error: Unauthorized\n at Object.<anonymous> (/home/blacksonic/workspace/bunyan-debug/example.js:15:32)\n at Module._compile (module.js:541:32)\n at Object.Module._extensions..js (module.js:550:10)\n at Module.load (module.js:458:32)\n at tryModuleLoad (module.js:417:12)\n at Function.Module._load (module.js:409:3)\n at Module.runMain (module.js:575:10)\n at run (bootstrap_node.js:352:7)\n at startup (bootstrap_node.js:144:9)\n at bootstrap_node.js:467:3","error_message":"Unauthorized","problem":"missmatch"}
3246
```
3347

3448
#### Class
@@ -103,10 +117,12 @@ const { createLogger } = require('@emartech/json-logger');
103117
const redisLogger = createLogger('redis');
104118

105119
redisLogger.info('connected', { domain: 'yahoo' });
106-
// {"name":"redis","action":"connected","level":30,"time":"2016-08-15T08:50:23.566Z","domain":"yahoo"}
120+
// ECS format: {"event":{"action":"connected"},"log":{"logger":"redis","level":30},"@timestamp":"2016-08-15T08:50:23.566Z","domain":"yahoo"}
121+
// Legacy format: {"name":"redis","action":"connected","level":30,"time":"2016-08-15T08:50:23.566Z","domain":"yahoo"}
107122

108123
redisLogger.info('connected');
109-
// {"name":"redis","action":"connected","level":30,"time":"2016-08-15T08:50:23.566Z"}
124+
// ECS format: {"event":{"action":"connected"},"log":{"logger":"redis","level":30},"@timestamp":"2016-08-15T08:50:23.566Z"}
125+
// Legacy format: {"name":"redis","action":"connected","level":30,"time":"2016-08-15T08:50:23.566Z"}
110126
```
111127

112128
By default displays the namespace of the instance (`name`), the current time in ISO8601 format (`time`),
@@ -144,7 +160,8 @@ const { createLogger } = require('@emartech/json-logger');
144160
const redisLogger = createLogger('redis');
145161

146162
redisLogger.fromError('query', new Error('Unauthorized'), { problem: 'missmatch' });
147-
// {"name":"redis","action":"query","level":50,"time":"2016-08-15T08:50:23.569Z","error_name":"Error","error_stack":"Error: Unauthorized\n at Object.<anonymous> (/home/blacksonic/workspace/bunyan-debug/example.js:15:32)\n at Module._compile (module.js:541:32)\n at Object.Module._extensions..js (module.js:550:10)\n at Module.load (module.js:458:32)\n at tryModuleLoad (module.js:417:12)\n at Function.Module._load (module.js:409:3)\n at Module.runMain (module.js:575:10)\n at run (bootstrap_node.js:352:7)\n at startup (bootstrap_node.js:144:9)\n at bootstrap_node.js:467:3","error_message":"Unauthorized","problem":"missmatch"}
163+
// ECS format: {"event":{"action":"query"},"log":{"logger":"redis","level":50},"@timestamp":"2016-08-15T08:50:23.569Z","error":{"type":"Error","message":"Unauthorized","stack_trace":"..."},"problem":"mismatch"}
164+
// Legacy format: {"name":"redis","action":"query","level":50,"time":"2016-08-15T08:50:23.569Z","error_name":"Error","error_stack":"Error: Unauthorized\n at Object.<anonymous> (/home/blacksonic/workspace/bunyan-debug/example.js:15:32)\n at Module._compile (module.js:541:32)\n at Object.Module._extensions..js (module.js:550:10)\n at Module.load (module.js:458:32)\n at tryModuleLoad (module.js:417:12)\n at Function.Module._load (module.js:409:3)\n at Module.runMain (module.js:575:10)\n at run (bootstrap_node.js:352:7)\n at startup (bootstrap_node.js:144:9)\n at bootstrap_node.js:467:3","error_message":"Unauthorized","problem":"missmatch"}
148165
```
149166

150167
##### JsonLogger.prototype.warnFromError(action, data)
@@ -166,7 +183,8 @@ const timer = redisLogger.timer();
166183
// heavy task
167184

168185
timer.info('completed');
169-
// {"name":"redis","action":"completed","level":30,"time":"2016-08-15T08:50:23.566Z","duration": 1500}
186+
// Legacy format: {"name":"redis","action":"completed","level":30,"time":"2016-08-15T08:50:23.566Z","duration": 1500}
187+
// ECS format: {"event":{"action":"completed","duration":"1500"},"log":{"logger":"redis","level":30},"@timestamp":"2016-08-15T08:50:23.566Z"}
170188
```
171189

172190
##### JsonLogger.configure(options)
@@ -182,7 +200,8 @@ const { createLogger } = require('@emartech/json-logger');
182200
createLogger.configure({
183201
formatter: JSON.stringify,
184202
output: console.log,
185-
transformers: []
203+
transformers: [],
204+
outputFormat: 'ecs'
186205
});
187206

188207
```
@@ -225,7 +244,7 @@ app.use(clsAdapter.getKoaMiddleware());
225244

226245
app.use(async () => {
227246
logger.info('connected');
228-
// {"name":"redis","action":"connected","level":30,"time":"2016-08-15T08:50:23.566Z","request_id":"d5caaa0e-b04e-4d94-bc88-3ed3b62dc94a"}
247+
// Legacy format: {"name":"redis","action":"connected","level":30,"time":"2016-08-15T08:50:23.566Z","request_id":"d5caaa0e-b04e-4d94-bc88-3ed3b62dc94a"}
229248
})
230249
```
231250

0 commit comments

Comments
 (0)