Skip to content

Commit

Permalink
fix: only use package identifier for extra name
Browse files Browse the repository at this point in the history
  • Loading branch information
commenthol committed Oct 18, 2023
1 parent 0e9b2eb commit 7edaa34
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 39 deletions.
75 changes: 39 additions & 36 deletions src/ecs/LogEcs.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ export class LogEcs extends Log {
timestamp: 'iso'
})
this.serializers = { ...ecsSerializers, ...serializers }
this.toJson = toJson
const [extra] = name.split(':')
this._extraName = extra
this.toJson = this._toJson
}

/* c8 ignore next 18 */
_applySerializers (obj) {
const name = this.name
const ecsObj = {}
for (const key in obj) {
const value = obj[key]
Expand All @@ -39,48 +40,50 @@ export class LogEcs extends Log {
this.serializers[key](value, ecsObj)
} else {
// add all other unknown fields to extra
ecsObj.extra = ecsObj.extra || { [name]: {} }
ecsObj.extra[name][key] = value
const extra = this._extraName
ecsObj.extra = ecsObj.extra || { [extra]: {} }
ecsObj.extra[extra][key] = value
}
}
}
return ecsObj
}
}

LogEcs.serializers = ecsSerializers

function toJson (obj, serializers) {
const { level, time, name, msg, pid, hostname, diff, ...other } = obj
_toJson (obj, serializers) {
const { level, time, name, msg, pid, hostname, diff, ...other } = obj

const ecsObj = {
log: {
level,
logger: name,
diff_ms: diff
},
message: msg,
'@timestamp': time,
process: pid ? { pid } : undefined,
host: hostname ? { hostname } : undefined
}

for (const key in other) {
const value = other[key]
if (
value === undefined ||
!Object.prototype.hasOwnProperty.call(other, key)
) {
continue
const ecsObj = {
log: {
level,
logger: name,
diff_ms: diff
},
message: msg,
'@timestamp': time,
process: pid ? { pid } : undefined,
host: hostname ? { hostname } : undefined
}
if (serializers[key]) {
serializers[key](value, ecsObj)
} else {
// add all other unknown fields to extra
ecsObj.extra = ecsObj.extra || { [name]: {} }
ecsObj.extra[name][key] = value

for (const key in other) {
const value = other[key]
if (
value === undefined ||
!Object.prototype.hasOwnProperty.call(other, key)
) {
continue
}
if (serializers[key]) {
serializers[key](value, ecsObj)
} else {
// add all other unknown fields to extra
const extra = this._extraName
ecsObj.extra = ecsObj.extra || { [extra]: {} }
ecsObj.extra[extra][key] = value
}
}
}

return stringify(ecsObj)
return stringify(ecsObj)
}
}

LogEcs.serializers = ecsSerializers
6 changes: 3 additions & 3 deletions types/ecs/LogEcs.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ export class LogEcs extends Log {
*/
constructor(name: string, opts: LogOptions);
serializers: any;
toJson: typeof toJson;
_extraName: string;
toJson: (obj: any, serializers: any) => string;
_applySerializers(obj: any): {
extra: any;
};
_toJson(obj: any, serializers: any): string;
}
export namespace LogEcs {
export { ecsSerializers as serializers };
Expand All @@ -26,6 +28,4 @@ export type LogOptions = import('../node.js').LogOptions & {
serializers: Record<string, EcsSerializer>;
};
import { Log } from "../node.js";
declare function toJson(obj: any, serializers: any): string;
import { ecsSerializers } from "./serializers.js";
export {};

0 comments on commit 7edaa34

Please sign in to comment.