UNPKG

@storm-stack/log-stream

Version:

A package containing a Storm Stack log adapter to write logs to a stream

127 lines (122 loc) 5.65 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.defaultTextFormatter = exports.ansiColorFormatter = void 0; exports.getAnsiColorFormatter = getAnsiColorFormatter; exports.getTextFormatter = getTextFormatter; var _stormJson = require("@stryke/json/storm-json"); var _nodeUtil = _interopRequireDefault(require("node:util")); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } function l(t, r) { return t.__type = r, t; } const v = { debug: "DBG", info: "INF", warning: "WRN", error: "ERR", fatal: "FTL" }, S = "Deno" in globalThis && "inspect" in globalThis.Deno && typeof globalThis.Deno.inspect == "function" ? l((t, r) => globalThis.Deno.inspect(t, { strAbbreviateSize: 1 / 0, iterableLimit: 1 / 0, ...r }), ["v", "opts", "", 'P"2!"2""/#']) : _nodeUtil.default != null && "inspect" in _nodeUtil.default && typeof _nodeUtil.default.inspect == "function" ? l((t, r) => _nodeUtil.default.inspect(t, { maxArrayLength: 1 / 0, maxStringLength: 1 / 0, ...r }), ["v", "opts", "", 'P"2!"2""/#']) : l(t => _stormJson.StormJSON.stringify(t), ["v", "", 'P"2!"/"']); function getTextFormatter(t = {}) { const r = t.timestamp == null || t.timestamp === "date-time-timezone" ? l(e => new Date(e).toISOString().replace("T", " ").replace("Z", " +00:00"), ["ts", "", `P'2!&/"`]) : t.timestamp === "date-time-tz" ? l(e => new Date(e).toISOString().replace("T", " ").replace("Z", " +00"), ["ts", "", `P'2!&/"`]) : t.timestamp === "date-time" ? l(e => new Date(e).toISOString().replace("T", " ").replace("Z", ""), ["ts", "", `P'2!&/"`]) : t.timestamp === "time-timezone" ? l(e => new Date(e).toISOString().replace(/.*T/, "").replace("Z", " +00:00"), ["ts", "", `P'2!&/"`]) : t.timestamp === "time-tz" ? l(e => new Date(e).toISOString().replace(/.*T/, "").replace("Z", " +00"), ["ts", "", `P'2!&/"`]) : t.timestamp === "time" ? l(e => new Date(e).toISOString().replace(/.*T/, "").replace("Z", ""), ["ts", "", `P'2!&/"`]) : t.timestamp === "date" ? l(e => new Date(e).toISOString().replace(/T.*/, ""), ["ts", "", `P'2!&/"`]) : t.timestamp === "rfc3339" ? l(e => new Date(e).toISOString(), ["ts", "", `P'2!&/"`]) : t.timestamp, m = t.value ?? S, i = t.level == null || t.level === "ABBR" ? l(e => v[e] || "", ["LogLevel", "level", "", 'P"w!2"&/#']) : t.level === "abbr" ? l(e => (v[e] || "").toLowerCase(), ["LogLevel", "level", "", 'P"w!2"&/#']) : t.level === "FULL" ? l(e => e.toUpperCase(), ["LogLevel", "level", "", 'P"w!2"&/#']) : t.level === "full" ? l(e => e, ["LogLevel", "level", "", 'P"w!2"&/#']) : t.level === "L" ? l(e => e.charAt(0).toUpperCase(), ["LogLevel", "level", "", 'P"w!2"&/#']) : t.level === "l" ? l(e => e.charAt(0), ["LogLevel", "level", "", 'P"w!2"&/#']) : t.level, f = t.format ?? l(({ timestamp: e, level: n, message: s }) => `${e} [${n}] ${s}`, ["FormattedValues", "param0", "", 'P"w!2""/#']); return l(e => { let n = ""; for (let o = 0; o < e.message.length; o++) o % 2 === 0 && typeof e.message[o] == "string" ? n += e.message[o] : n += m(e.message[o]); const s = r(e.timestamp), a = i(e.level); return `${f({ timestamp: s, level: a, message: n, record: e })} `; }, ["LogRecord", "record", "", 'P"w!2"&/#']); } getTextFormatter.__type = ["options", () => ({}), "getTextFormatter", `Get a text formatter with the specified options. Although it's flexible enough to create a custom formatter, if you want more control, you can create a custom formatter that satisfies the {@link TextFormatter} type instead. For more information on the options, see {@link TextFormatterOptions}. By default, the formatter formats log records as follows: \`\`\` 2023-11-14 22:13:20.000 +00:00 [INFO] Hello, world! \`\`\``, 'P!2!>"!/#?$']; const defaultTextFormatter = exports.defaultTextFormatter = getTextFormatter(); const x = "\x1B[0m", y = { black: "\x1B[30m", red: "\x1B[31m", green: "\x1B[32m", yellow: "\x1B[33m", blue: "\x1B[34m", magenta: "\x1B[35m", cyan: "\x1B[36m", white: "\x1B[37m" }, w = { bold: "\x1B[1m", dim: "\x1B[2m", italic: "\x1B[3m", underline: "\x1B[4m", strikethrough: "\x1B[9m" }, h = { debug: "blue", info: "green", warning: "yellow", error: "red", fatal: "magenta" }; function getAnsiColorFormatter(t = {}) { const r = t.format, m = typeof t.timestampStyle > "u" ? "dim" : t.timestampStyle, i = t.timestampColor ?? null, f = `${m == null ? "" : w[m]}${i == null ? "" : y[i]}`, e = m == null && i == null ? "" : x, n = typeof t.levelStyle > "u" ? "bold" : t.levelStyle, s = t.levelColors ?? h; return getTextFormatter({ timestamp: "date-time-tz", value: l(function (a) { return S(a, { colors: !0 }); }, ["value", "P#2!&/!"]), ...t, format: l(({ timestamp: a, level: c, message: o, record: p }) => { const g = s[p.level]; return a = `${f}${a}${e}`, c = `${n == null ? "" : w[n]}${g == null ? "" : y[g]}${c}${n == null && g == null ? "" : x}`, r == null ? `${a} ${c} ${o}` : r({ timestamp: a, level: c, message: o, record: p }); }, ["param0", "", 'P"2!&/"']) }); } getAnsiColorFormatter.__type = ["options", () => ({}), "getAnsiColorFormatter", `Get an ANSI color formatter with the specified options. ![A preview of an ANSI color formatter.](https://i.imgur.com/I8LlBUf.png)`, 'P!2!>"!/#?$']; const ansiColorFormatter = exports.ansiColorFormatter = getAnsiColorFormatter();