@trifrost/core
Version:
Blazingly fast, runtime-agnostic server framework for modern edge and node environments
64 lines (63 loc) • 1.96 kB
JavaScript
;
/* eslint-disable no-console */
Object.defineProperty(exports, "__esModule", { value: true });
exports.JsonExporter = void 0;
const deep_1 = require("@valkyriestudios/utils/deep");
const Scrambler_1 = require("../../../utils/Scrambler");
class JsonExporter {
#global_attrs = null;
/**
* Scrambler based on omit pattern provided
*/
#scramble;
/**
* Sink for the json entries, if not defined will be console
*/
#sink = null;
constructor(options) {
/* Configure scrambler */
this.#scramble = (0, Scrambler_1.createScrambler)({
checks: Array.isArray(options?.omit) ? (0, deep_1.deepFreeze)([...options.omit]) : Scrambler_1.OMIT_PRESETS.default,
});
/* Configure sink if passed */
if (typeof options?.sink === 'function')
this.#sink = options.sink;
}
init(global_attrs) {
this.#global_attrs = this.#scramble(global_attrs);
}
async pushLog(log) {
const entry = this.#scramble({
message: log.message,
});
/* Add time */
entry.time = log.time.toISOString();
/* Add level */
entry.level = log.level;
/* Add trace id */
if (log.trace_id)
entry.trace_id = log.trace_id;
/* Add span id */
if (log.span_id)
entry.span_id = log.span_id;
/* Add context */
if (log.ctx)
entry.ctx = this.#scramble(log.ctx);
/* Add data */
if (log.data)
entry.data = this.#scramble(log.data);
/* Add global attributes */
if (this.#global_attrs)
entry.global = this.#global_attrs;
if (this.#sink) {
this.#sink(entry);
}
else {
console[log.level](JSON.stringify(entry));
}
}
async flush() {
/* No-Op for Json console exporter */
}
}
exports.JsonExporter = JsonExporter;