-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bfedcaf
commit 2c14d64
Showing
18 changed files
with
723 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import { Log, stringify } from '../node.js' | ||
import { ecsSerializers } from './serializers.js' | ||
|
||
/** | ||
* @typedef {(val: any, escFields: object) => void} EcsSerializer | ||
* @typedef {import('../node.js').LogOptions & {serializers: Record<string, EcsSerializer>}} LogOptions | ||
*/ | ||
|
||
/** | ||
* Elastic Common Schema (ECS) compatible logger; | ||
* See [field reference](https://www.elastic.co/guide/en/ecs/current/ecs-field-reference.html) | ||
*/ | ||
export class LogEcs extends Log { | ||
/** | ||
* @param {string} name logger namespace | ||
* @param {LogOptions} opts | ||
*/ | ||
constructor (name, opts) { | ||
const { serializers, ..._opts } = opts || {} | ||
super(name, { | ||
..._opts, | ||
timestamp: 'iso' | ||
}) | ||
this.serializers = { ...ecsSerializers, ...serializers } | ||
this.toJson = toJson | ||
} | ||
|
||
/* c8 ignore next 18 */ | ||
_applySerializers (obj) { | ||
const ecsObj = {} | ||
for (const key in obj) { | ||
const value = obj[key] | ||
if ( | ||
Object.prototype.hasOwnProperty.call(obj, key) && | ||
value !== undefined | ||
) { | ||
if (this.serializers && this.serializers[key]) { | ||
this.serializers[key](value, ecsObj) | ||
} else { | ||
ecsObj.extra = ecsObj.extra || {} | ||
ecsObj.extra[key] = value | ||
} | ||
} | ||
} | ||
return ecsObj | ||
} | ||
} | ||
|
||
LogEcs.serializers = ecsSerializers | ||
|
||
function 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 | ||
} | ||
if (serializers[key]) { | ||
serializers[key](value, ecsObj) | ||
} else { | ||
// add all other unknown fields to extra | ||
ecsObj.extra = ecsObj.extra || {} | ||
ecsObj.extra[key] = normToString(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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { LogEcs } from './LogEcs.js' | ||
import { ecsSerializers } from './serializers.js' | ||
|
||
export { LogEcs, ecsSerializers } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import { startTimeKey } from '../serializers/res.js' | ||
|
||
const isNotObject = (any) => !any || typeof any !== 'object' | ||
|
||
const ecsError = (err, ecsObj) => { | ||
if (!(err instanceof Error)) { | ||
return | ||
} | ||
|
||
const type = | ||
Object.prototype.toString.call(err.constructor) === '[object Function]' | ||
? err.constructor.name | ||
: err.name | ||
|
||
ecsObj.error = { | ||
type, | ||
message: err.message, | ||
stack_trace: err.stack | ||
} | ||
} | ||
|
||
const ecsClient = (req, ecsObj) => { | ||
const ip = req.ip ? req.ip : req.socket?.remoteAddress | ||
const port = req.socket?.remotePort | ||
if (ip) { | ||
ecsObj.client = { | ||
ip, | ||
port | ||
} | ||
} | ||
} | ||
|
||
const ecsUrl = (req, ecsObj) => { | ||
const { originalUrl, url, headers } = req | ||
const _url = originalUrl || url | ||
if (!_url) return | ||
|
||
const [path, query] = _url.split('?') | ||
ecsObj.url = { path, query } | ||
|
||
if (headers?.host) { | ||
const [domain, port] = (headers.host || '').split(':') | ||
ecsObj.url.domain = domain | ||
if (port) { | ||
ecsObj.url.port = Number(port) | ||
} | ||
} | ||
} | ||
|
||
const ecsReq = (req, ecsObj) => { | ||
if (isNotObject(req)) { | ||
return | ||
} | ||
ecsClient(req, ecsObj) | ||
|
||
ecsUrl(req, ecsObj) | ||
|
||
const { method, httpVersion } = req | ||
if (!method) return | ||
|
||
const { | ||
cookie, | ||
authorization, | ||
'user-agent': userAgent, | ||
...headers | ||
} = req.headers | ||
|
||
ecsObj.http = ecsObj.http || {} | ||
ecsObj.http.request = { | ||
id: typeof req.id === 'function' ? req.id() : req.id, | ||
method, | ||
version: httpVersion, | ||
headers | ||
} | ||
ecsObj.user_agent = { | ||
original: userAgent | ||
} | ||
} | ||
|
||
const ecsRes = (res, ecsObj) => { | ||
if (isNotObject(res)) { | ||
return | ||
} | ||
const { statusCode } = res | ||
if (!statusCode) return | ||
|
||
const { | ||
'proxy-authenticate': _1, | ||
'set-cookie': _2, | ||
'content-type': mimeType, | ||
cookie, | ||
...headers | ||
} = res._headers || {} | ||
|
||
ecsObj.http = ecsObj.http || {} | ||
ecsObj.http.response = { | ||
status_code: statusCode, | ||
mime_type: mimeType, | ||
headers | ||
} | ||
if (res[startTimeKey]) { | ||
ecsObj.http.response.latency = Date.now() - res[startTimeKey] | ||
} | ||
} | ||
|
||
export const ecsSerializers = { | ||
err: ecsError, | ||
req: ecsReq, | ||
res: ecsRes | ||
} |
Oops, something went wrong.