@visulima/pail
Version:
Highly configurable Logger for Node.js, Edge and Browser.
56 lines (52 loc) • 1.77 kB
JavaScript
;
const fmt = require('@visulima/fmt');
var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
class MessageFormatterProcessor {
static {
__name(this, "MessageFormatterProcessor");
}
// eslint-disable-next-line @typescript-eslint/no-redundant-type-constituents
#stringify;
#formatters;
constructor(options = {}) {
this.#formatters = options.formatters;
}
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-explicit-any
setStringify(function_) {
this.#stringify = function_;
}
process(meta) {
const formatter = fmt.build({
formatters: this.#formatters,
stringify: /* @__PURE__ */ __name((value) => {
const stringified = this.#stringify(value);
if (stringified === void 0) {
console.warn("Unable to stringify value of type " + typeof value, value);
return "undefined";
}
return stringified;
}, "stringify")
});
if (meta.message !== void 0) {
meta.message = this._format(formatter, meta.message, meta.context ?? []);
}
return meta;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
_format(formatter, data, arguments_ = []) {
if (typeof data === "string") {
return formatter(data, arguments_);
}
if (typeof data === "object" && data !== null) {
for (const index in data) {
const value = data[index];
if (typeof value === "string" || Array.isArray(value) || typeof value === "object") {
data[index] = this._format(formatter, value, arguments_);
}
}
}
return data;
}
}
module.exports = MessageFormatterProcessor;