@storm-software/untyped
Version:
A package containing `untyped` utilities for building Storm Software libraries and applications
260 lines (250 loc) • 8.82 kB
JavaScript
// ../config-tools/src/logger/console.ts
import { formatDistanceToNow } from "date-fns/formatDistanceToNow";
// ../config-tools/src/types.ts
var LogLevel = {
SILENT: 0,
FATAL: 10,
ERROR: 20,
WARN: 30,
SUCCESS: 35,
INFO: 40,
DEBUG: 60,
TRACE: 70,
ALL: 100
};
var LogLevelLabel = {
SILENT: "silent",
FATAL: "fatal",
ERROR: "error",
WARN: "warn",
SUCCESS: "success",
INFO: "info",
DEBUG: "debug",
TRACE: "trace",
ALL: "all"
};
// ../config-tools/src/utilities/colors.ts
var DEFAULT_COLOR_CONFIG = {
light: {
background: "#fafafa",
foreground: "#1d1e22",
brand: "#1fb2a6",
alternate: "#db2777",
help: "#5C4EE5",
success: "#087f5b",
info: "#0550ae",
warning: "#e3b341",
danger: "#D8314A",
fatal: "#51070f",
link: "#3fa6ff",
positive: "#22c55e",
negative: "#dc2626",
gradient: ["#1fb2a6", "#db2777", "#5C4EE5"]
},
dark: {
background: "#1d1e22",
foreground: "#cbd5e1",
brand: "#2dd4bf",
alternate: "#db2777",
help: "#818cf8",
success: "#10b981",
info: "#58a6ff",
warning: "#f3d371",
danger: "#D8314A",
fatal: "#a40e26",
link: "#3fa6ff",
positive: "#22c55e",
negative: "#dc2626",
gradient: ["#1fb2a6", "#db2777", "#818cf8"]
}
};
// ../config-tools/src/logger/chalk.ts
import chalk from "chalk";
var chalkDefault = {
hex: (_) => (message) => message,
bgHex: (_) => ({
whiteBright: (message) => message,
white: (message) => message
}),
white: (message) => message,
whiteBright: (message) => message,
gray: (message) => message,
bold: {
hex: (_) => (message) => message,
bgHex: (_) => ({
whiteBright: (message) => message,
white: (message) => message
}),
whiteBright: (message) => message,
white: (message) => message
},
dim: {
hex: (_) => (message) => message,
gray: (message) => message
}
};
var getChalk = () => {
let _chalk = chalk;
if (!_chalk?.hex || !_chalk?.bold?.hex || !_chalk?.bgHex || !_chalk?.whiteBright || !_chalk?.white) {
_chalk = chalkDefault;
}
return _chalk;
};
// ../config-tools/src/logger/is-unicode-supported.ts
function isUnicodeSupported() {
if (process.platform !== "win32") {
return process.env.TERM !== "linux";
}
return Boolean(process.env.WT_SESSION) || // Windows Terminal
Boolean(process.env.TERMINUS_SUBLIME) || // Terminus (<0.2.27)
process.env.ConEmuTask === "{cmd::Cmder}" || // ConEmu and cmder
process.env.TERM_PROGRAM === "Terminus-Sublime" || process.env.TERM_PROGRAM === "vscode" || process.env.TERM === "xterm-256color" || process.env.TERM === "alacritty" || process.env.TERM === "rxvt-unicode" || process.env.TERM === "rxvt-unicode-256color" || process.env.TERMINAL_EMULATOR === "JetBrains-JediTerm";
}
// ../config-tools/src/logger/console-icons.ts
var useIcon = (c, fallback) => isUnicodeSupported() ? c : fallback;
var CONSOLE_ICONS = {
[LogLevelLabel.ERROR]: useIcon("\u2718", "\xD7"),
[LogLevelLabel.FATAL]: useIcon("\u{1F480}", "\xD7"),
[LogLevelLabel.WARN]: useIcon("\u26A0", "\u203C"),
[LogLevelLabel.INFO]: useIcon("\u2139", "i"),
[LogLevelLabel.SUCCESS]: useIcon("\u2714", "\u221A"),
[LogLevelLabel.DEBUG]: useIcon("\u{1F6E0}", "D"),
[LogLevelLabel.TRACE]: useIcon("\u{1F6E0}", "T"),
[LogLevelLabel.ALL]: useIcon("\u2709", "\u2192")
};
// ../config-tools/src/logger/format-timestamp.ts
var formatTimestamp = (date = /* @__PURE__ */ new Date()) => {
return `${date.toLocaleDateString()} ${date.toLocaleTimeString()}`;
};
// ../config-tools/src/logger/get-log-level.ts
var getLogLevel = (label) => {
switch (label) {
case "all":
return LogLevel.ALL;
case "trace":
return LogLevel.TRACE;
case "debug":
return LogLevel.DEBUG;
case "info":
return LogLevel.INFO;
case "warn":
return LogLevel.WARN;
case "error":
return LogLevel.ERROR;
case "fatal":
return LogLevel.FATAL;
case "silent":
return LogLevel.SILENT;
default:
return LogLevel.INFO;
}
};
// ../config-tools/src/logger/console.ts
var getLogFn = (logLevel = LogLevel.INFO, config = {}, _chalk = getChalk()) => {
const colors = !config.colors?.dark && !config.colors?.["base"] && !config.colors?.["base"]?.dark ? DEFAULT_COLOR_CONFIG : config.colors?.dark && typeof config.colors.dark === "string" ? config.colors : config.colors?.["base"]?.dark && typeof config.colors["base"].dark === "string" ? config.colors["base"].dark : config.colors?.["base"] ? config.colors?.["base"] : DEFAULT_COLOR_CONFIG;
const configLogLevel = config.logLevel || process.env.STORM_LOG_LEVEL || LogLevelLabel.INFO;
if (logLevel > getLogLevel(configLogLevel) || logLevel <= LogLevel.SILENT || getLogLevel(configLogLevel) <= LogLevel.SILENT) {
return (_) => {
};
}
if (typeof logLevel === "number" && LogLevel.FATAL >= logLevel) {
return (message) => {
console.error(
`
${_chalk.gray(formatTimestamp())} ${_chalk.hex(colors.fatal ?? DEFAULT_COLOR_CONFIG.dark.fatal)(`[${CONSOLE_ICONS[LogLevelLabel.FATAL]} Fatal] `)}${_chalk.bold.whiteBright(formatLogMessage(message))}
`
);
};
}
if (typeof logLevel === "number" && LogLevel.ERROR >= logLevel) {
return (message) => {
console.error(
`
${_chalk.gray(formatTimestamp())} ${_chalk.hex(colors.danger ?? DEFAULT_COLOR_CONFIG.dark.danger)(`[${CONSOLE_ICONS[LogLevelLabel.ERROR]} Error] `)}${_chalk.bold.whiteBright(formatLogMessage(message))}
`
);
};
}
if (typeof logLevel === "number" && LogLevel.WARN >= logLevel) {
return (message) => {
console.warn(
`
${_chalk.gray(formatTimestamp())} ${_chalk.hex(colors.warning ?? DEFAULT_COLOR_CONFIG.dark.warning)(`[${CONSOLE_ICONS[LogLevelLabel.WARN]} Warn] `)}${_chalk.bold.whiteBright(formatLogMessage(message))}
`
);
};
}
if (typeof logLevel === "number" && LogLevel.SUCCESS >= logLevel) {
return (message) => {
console.info(
`
${_chalk.gray(formatTimestamp())} ${_chalk.hex(colors.success ?? DEFAULT_COLOR_CONFIG.dark.success)(`[${CONSOLE_ICONS[LogLevelLabel.SUCCESS]} Success] `)}${_chalk.bold.whiteBright(formatLogMessage(message))}
`
);
};
}
if (typeof logLevel === "number" && LogLevel.INFO >= logLevel) {
return (message) => {
console.info(
`
${_chalk.gray(formatTimestamp())} ${_chalk.hex(colors.info ?? DEFAULT_COLOR_CONFIG.dark.info)(`[${CONSOLE_ICONS[LogLevelLabel.INFO]} Info] `)}${_chalk.bold.whiteBright(formatLogMessage(message))}
`
);
};
}
if (typeof logLevel === "number" && LogLevel.DEBUG >= logLevel) {
return (message) => {
console.debug(
`
${_chalk.gray(formatTimestamp())} ${_chalk.hex(colors.info ?? DEFAULT_COLOR_CONFIG.dark.info)(`[${CONSOLE_ICONS[LogLevelLabel.DEBUG]} Debug] `)}${_chalk.bold.whiteBright(formatLogMessage(message))}
`
);
};
}
if (typeof logLevel === "number" && LogLevel.TRACE >= logLevel) {
return (message) => {
console.debug(
`
${_chalk.gray(formatTimestamp())} ${_chalk.hex(colors.info ?? DEFAULT_COLOR_CONFIG.dark.info)(`[${CONSOLE_ICONS[LogLevelLabel.TRACE]} Trace] `)}${_chalk.bold.whiteBright(formatLogMessage(message))}
`
);
};
}
return (message) => {
console.log(
`
${_chalk.gray(formatTimestamp())} ${_chalk.hex(colors.brand ?? DEFAULT_COLOR_CONFIG.dark.brand)(`[${CONSOLE_ICONS[LogLevelLabel.ALL]} System] `)}${_chalk.bold.whiteBright(formatLogMessage(message))}
`
);
};
};
var writeError = (message, config) => getLogFn(LogLevel.ERROR, config)(message);
var writeTrace = (message, config) => getLogFn(LogLevel.TRACE, config)(message);
var MAX_DEPTH = 4;
var formatLogMessage = (message, options = {}, depth = 0) => {
if (depth > MAX_DEPTH) {
return "<max depth>";
}
const prefix = options.prefix ?? "-";
const skip = options.skip ?? [];
return typeof message === "undefined" || message === null || !message && typeof message !== "boolean" ? "<none>" : typeof message === "string" ? message : Array.isArray(message) ? `
${message.map((item, index) => ` ${prefix}> #${index} = ${formatLogMessage(item, { prefix: `${prefix}-`, skip }, depth + 1)}`).join("\n")}` : typeof message === "object" ? `
${Object.keys(message).filter((key) => !skip.includes(key)).map(
(key) => ` ${prefix}> ${key} = ${_isFunction(message[key]) ? "<function>" : typeof message[key] === "object" ? formatLogMessage(
message[key],
{ prefix: `${prefix}-`, skip },
depth + 1
) : message[key]}`
).join("\n")}` : message;
};
var _isFunction = (value) => {
try {
return value instanceof Function || typeof value === "function" || !!(value?.constructor && value?.call && value?.apply);
} catch {
return false;
}
};
export {
writeError,
writeTrace
};