Skip to content

Commit

Permalink
Merge pull request #22 from commenthol/fix-ecs
Browse files Browse the repository at this point in the history
fix(ECS): serialize error like objects
  • Loading branch information
commenthol authored Oct 18, 2023
2 parents 5a9742b + 9dea4c4 commit bbc09c6
Show file tree
Hide file tree
Showing 14 changed files with 447 additions and 168 deletions.
2 changes: 1 addition & 1 deletion benchmarks/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"bunyan": "^1.8.15",
"debug": "^4.3.4",
"fastbench": "^1.0.1",
"pino": "^8.8.0"
"pino": "^8.16.0"
},
"devDependencies": {}
}
84 changes: 42 additions & 42 deletions docs/benchmarks.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
`log.debug('hello world')`

```
benchDebug*10000: 23.423ms
benchBunyan*10000: 25.634ms
benchPino*10000: 9.619ms
benchDebugLevel*10000: 19.821ms
benchPinoAsync*10000: 8.892ms
benchDebugLevelAsync*10000: 10.998ms
benchDebug*10000: 18.546ms
benchBunyan*10000: 22.96ms
benchPino*10000: 9.484ms
benchDebugLevel*10000: 16.34ms
benchPinoAsync*10000: 6.189ms
benchDebugLevelAsync*10000: 9.902ms
```

Expand All @@ -18,12 +18,12 @@ benchDebugLevelAsync*10000: 10.998ms
`log.debug('...2000chars...')`

```
benchDebug*10000: 76.483ms
benchBunyan*10000: 83.973ms
benchPino*10000: 75.299ms
benchDebugLevel*10000: 95.44ms
benchPinoAsync*10000: 81.941ms
benchDebugLevelAsync*10000: 63.656ms
benchDebug*10000: 57.241ms
benchBunyan*10000: 75.729ms
benchPino*10000: 65.504ms
benchDebugLevel*10000: 67.711ms
benchPinoAsync*10000: 56.107ms
benchDebugLevelAsync*10000: 59.471ms
```

Expand All @@ -32,12 +32,12 @@ benchDebugLevelAsync*10000: 63.656ms
`log.debug('hello %s', 'world')`

```
benchDebug*10000: 22.46ms
benchBunyan*10000: 25.477ms
benchPino*10000: 10.787ms
benchDebugLevel*10000: 29.386ms
benchPinoAsync*10000: 10.147ms
benchDebugLevelAsync*10000: 14.039ms
benchDebug*10000: 20.458ms
benchBunyan*10000: 24.222ms
benchPino*10000: 10.218ms
benchDebugLevel*10000: 17.011ms
benchPinoAsync*10000: 5.769ms
benchDebugLevelAsync*10000: 10.59ms
```

Expand All @@ -46,12 +46,12 @@ benchDebugLevelAsync*10000: 14.039ms
`log.debug('hello %s %j %d', 'world', { obj: true }, 4, { another: 'obj' })`

```
benchDebug*10000: 24.282ms
benchBunyan*10000: 37.851ms
benchPino*10000: 16.485ms
benchDebugLevel*10000: 24.695ms
benchPinoAsync*10000: 14.521ms
benchDebugLevelAsync*10000: 20.715ms
benchDebug*10000: 23.713ms
benchBunyan*10000: 38.137ms
benchPino*10000: 13.356ms
benchDebugLevel*10000: 25.32ms
benchPinoAsync*10000: 10.579ms
benchDebugLevelAsync*10000: 18.082ms
```

Expand All @@ -60,12 +60,12 @@ benchDebugLevelAsync*10000: 20.715ms
`log.debug({ msg: 'the message',string: 'string',number: 42,bool: true })`

```
benchDebug*10000: 39.002ms
benchBunyan*10000: 27.534ms
benchPino*10000: 14.266ms
benchDebugLevel*10000: 20.081ms
benchPinoAsync*10000: 11.325ms
benchDebugLevelAsync*10000: 16.31ms
benchDebug*10000: 38.19ms
benchBunyan*10000: 25.403ms
benchPino*10000: 15.406ms
benchDebugLevel*10000: 17.856ms
benchPinoAsync*10000: 8.526ms
benchDebugLevelAsync*10000: 14.703ms
```

Expand All @@ -74,12 +74,12 @@ benchDebugLevelAsync*10000: 16.31ms
`log.debug(deep)`

```
benchDebug*10000: 1.340s
benchBunyan*10000: 474.926ms
benchPino*10000: 455.004ms
benchDebugLevel*10000: 476.143ms
benchPinoAsync*10000: 281.14ms
benchDebugLevelAsync*10000: 299.743ms
benchDebug*10000: 1.046s
benchBunyan*10000: 269.327ms
benchPino*10000: 263.777ms
benchDebugLevel*10000: 278.875ms
benchPinoAsync*10000: 231.434ms
benchDebugLevelAsync*10000: 242.807ms
```

Expand All @@ -88,11 +88,11 @@ benchDebugLevelAsync*10000: 299.743ms
`log.debug('deep %j', deep)`

```
benchDebug*10000: 24.003ms
benchBunyan*10000: 738.39ms
benchPino*10000: 717.07ms
benchDebugLevel*10000: 791.504ms
benchPinoAsync*10000: 521.031ms
benchDebugLevelAsync*10000: 573.552ms
benchDebug*10000: 20.346ms
benchBunyan*10000: 535.546ms
benchPino*10000: 526.309ms
benchDebugLevel*10000: 585.732ms
benchPinoAsync*10000: 492.425ms
benchDebugLevelAsync*10000: 555.237ms
```
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "debug-level",
"version": "3.1.1",
"version": "3.1.2-0",
"description": "debug with levels",
"keywords": [
"debug",
Expand Down
14 changes: 9 additions & 5 deletions src/Format.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ import { format as quickFormat } from './quick-format.js'

export class Format {
/**
* @param {FormatOption} opts
* @param {FormatOption} [opts]
*/
constructor (opts = {}) {
this.opts = { opts }
constructor (opts) {
const { spaces } = opts || {}
this.opts = { spaces }
this._formatOpts()
}

Expand All @@ -25,8 +26,11 @@ export class Format {
}

_formatOpts () {
// @ts-expect-error
this.formatOpts = { stringify: (o) => fastStringify(o, null, this.opts.spaces) }
this.formatOpts = {
// @ts-expect-error
stringify: (o) => fastStringify(o, null, this.opts.spaces),
spaces: this.opts.spaces
}
}

stringify (...args) {
Expand Down
27 changes: 6 additions & 21 deletions src/ecs/LogEcs.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export class LogEcs extends Log {

/* c8 ignore next 18 */
_applySerializers (obj) {
const name = this.name
const ecsObj = {}
for (const key in obj) {
const value = obj[key]
Expand All @@ -37,8 +38,9 @@ export class LogEcs extends Log {
if (this.serializers && this.serializers[key]) {
this.serializers[key](value, ecsObj)
} else {
ecsObj.extra = ecsObj.extra || {}
ecsObj.extra[key] = value
// add all other unknown fields to extra
ecsObj.extra = ecsObj.extra || { [name]: {} }
ecsObj.extra[name][key] = value
}
}
}
Expand Down Expand Up @@ -75,27 +77,10 @@ function toJson (obj, serializers) {
serializers[key](value, ecsObj)
} else {
// add all other unknown fields to extra
ecsObj.extra = ecsObj.extra || {}
ecsObj.extra[key] = normToString(value)
ecsObj.extra = ecsObj.extra || { [name]: {} }
ecsObj.extra[name][key] = value
}
}

return stringify(ecsObj)
}

/**
* elastic is picky on indexing types; for this all entries in e.g. extra are
* set to string to avoid any type collisions
* @param {any} val
* @returns {string}
*/
const normToString = (val) => {
switch (typeof val) {
case 'string':
return val
case 'number':
return String(val)
default:
return stringify(val)
}
}
4 changes: 2 additions & 2 deletions src/ecs/serializers.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { startTimeKey } from '../serializers/res.js'
const isNotObject = (any) => !any || typeof any !== 'object'

const ecsError = (err, ecsObj) => {
if (!(err instanceof Error)) {
if (!(err?.message)) {
return
}

Expand Down Expand Up @@ -63,7 +63,7 @@ const ecsReq = (req, ecsObj) => {
authorization,
'user-agent': userAgent,
...headers
} = req.headers
} = req.headers || {}

ecsObj.http = ecsObj.http || {}
ecsObj.http.request = {
Expand Down
5 changes: 4 additions & 1 deletion src/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { wrapConsole } from './wrapConsole.js'
import { wrapDebug } from './wrapDebug.js'
import { Sonic } from './Sonic.js'
import { errSerializer } from './serializers/err.js'
import { Format } from './Format.js'

const env = process.env.NODE_ENV || 'development'
const isDevEnv = /^dev/.test(env) // anything which starts with dev is seen as development env
Expand Down Expand Up @@ -81,6 +82,8 @@ export class Log extends LogBase {
: this.opts.stream || process.stderr

if (!this.opts.json) {
this.opts.spaces = this.opts.spaces ?? 2
this.formatter = new Format(this.opts)
this._log = this._logDebugLike
} else if (this.opts.colors) {
this._log = this._logJsonColor
Expand Down Expand Up @@ -231,7 +234,7 @@ export class Log extends LogBase {
this._color(this.name, this.color, true)

const strOther = Object.keys(other).length
? stringify(this._applySerializers(other), this.opts.splitLine ? 2 : undefined)
? stringify(this._applySerializers(other), this.opts.splitLine ? (this.opts.spaces ?? 2) : undefined)
: ''

const str = (this.opts.splitLine)
Expand Down
9 changes: 5 additions & 4 deletions src/quick-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
* @copyright Copyright (c) 2016-2019 David Mark Clements
*/

function tryStringify (o) {
try { return JSON.stringify(o) } catch (e) { return '"[Circular]"' }
function tryStringify (value, replacer, spaces) {
try { return JSON.stringify(value, replacer, spaces) } catch (e) { return '"[Circular]"' }
}

export function format (f, args, opts, obj = {}) {
const ss = (opts && opts.stringify) || tryStringify
const stringify = opts?.stringify || tryStringify
const spaces = opts?.spaces
const argLen = args.length
let a = 0
let str = ''
Expand Down Expand Up @@ -78,7 +79,7 @@ export function format (f, args, opts, obj = {}) {
i++
break
}
str += ss(args[a])
str += stringify(args[a], null, spaces)
lastPos = i + 2
i++
break
Expand Down
Loading

0 comments on commit bbc09c6

Please sign in to comment.