@storm-software/config-tools
Version:
⚡The Storm-Ops monorepo contains utility applications, tools, and various libraries to create modern and scalable web applications.
142 lines (139 loc) • 6.2 kB
JavaScript
import {
DEFAULT_COLOR_CONFIG
} from "./chunk-2V4WR4HU.js";
import {
getChalk
} from "./chunk-7DEX73IB.js";
import {
CONSOLE_ICONS
} from "./chunk-FVYBJYYR.js";
import {
formatTimestamp
} from "./chunk-TKQCRNGC.js";
import {
getLogLevel
} from "./chunk-NKHVLY3K.js";
import {
LogLevel,
LogLevelLabel
} from "./chunk-3QAWRU2B.js";
import {
__name
} from "./chunk-SHUYVCID.js";
// src/logger/console.ts
var getLogFn = /* @__PURE__ */ __name((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 ?? "#7d1a1a")(`[${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 ?? "#f85149")(`[${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 ?? "#e3b341")(`[${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 ?? "#56d364")(`[${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 ?? "#58a6ff")(`[${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.brand ?? "#1fb2a6")(`[${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.brand ?? "#1fb2a6")(`[${CONSOLE_ICONS[LogLevelLabel.TRACE]} Trace] `)}${_chalk.bold.whiteBright(formatLogMessage(message))}
`);
};
}
return (message) => {
console.log(`
${_chalk.gray(formatTimestamp())} ${_chalk.hex(colors.brand ?? "#1fb2a6")(`[${CONSOLE_ICONS[LogLevelLabel.ALL]} System] `)}${_chalk.bold.whiteBright(formatLogMessage(message))}
`);
};
}, "getLogFn");
var writeFatal = /* @__PURE__ */ __name((message, config) => getLogFn(LogLevel.FATAL, config)(message), "writeFatal");
var writeError = /* @__PURE__ */ __name((message, config) => getLogFn(LogLevel.ERROR, config)(message), "writeError");
var writeWarning = /* @__PURE__ */ __name((message, config) => getLogFn(LogLevel.WARN, config)(message), "writeWarning");
var writeInfo = /* @__PURE__ */ __name((message, config) => getLogFn(LogLevel.INFO, config)(message), "writeInfo");
var writeSuccess = /* @__PURE__ */ __name((message, config) => getLogFn(LogLevel.SUCCESS, config)(message), "writeSuccess");
var writeDebug = /* @__PURE__ */ __name((message, config) => getLogFn(LogLevel.DEBUG, config)(message), "writeDebug");
var writeTrace = /* @__PURE__ */ __name((message, config) => getLogFn(LogLevel.TRACE, config)(message), "writeTrace");
var writeSystem = /* @__PURE__ */ __name((message, config) => getLogFn(LogLevel.ALL, config)(message), "writeSystem");
var getStopwatch = /* @__PURE__ */ __name((name) => {
const start = process.hrtime();
return () => {
const end = process.hrtime(start);
console.info(`
> \u23F1\uFE0F The${name ? ` ${name}` : ""} process took ${Math.round(end[0] * 1e3 + end[1] / 1e6)}ms to complete
`);
};
}, "getStopwatch");
var MAX_DEPTH = 4;
var formatLogMessage = /* @__PURE__ */ __name((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;
}, "formatLogMessage");
var _isFunction = /* @__PURE__ */ __name((value) => {
try {
return value instanceof Function || typeof value === "function" || !!(value?.constructor && value?.call && value?.apply);
} catch (e) {
return false;
}
}, "_isFunction");
export {
getLogFn,
writeFatal,
writeError,
writeWarning,
writeInfo,
writeSuccess,
writeDebug,
writeTrace,
writeSystem,
getStopwatch,
formatLogMessage
};