@storm-software/config-tools
Version:
A package containing various utilities to support custom workspace configurations and environment management for Storm Software projects, including configuration file handling, environment variable management, and logging utilities.
159 lines (156 loc) • 5.84 kB
JavaScript
import {
DEFAULT_COLOR_CONFIG
} from "./chunk-SHD7ZUSQ.js";
import {
getChalk
} from "./chunk-P2KJ6EZO.js";
import {
CONSOLE_ICONS
} from "./chunk-LM2UMGYA.js";
import {
formatTimestamp
} from "./chunk-CZ4IE2QN.js";
import {
getLogLevel
} from "./chunk-K4CDYUQR.js";
import {
LogLevel,
LogLevelLabel
} from "./chunk-POXTJ6GF.js";
// 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 ?? "#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))}
`
);
};
};
var writeFatal = (message, config) => getLogFn(LogLevel.FATAL, config)(message);
var writeError = (message, config) => getLogFn(LogLevel.ERROR, config)(message);
var writeWarning = (message, config) => getLogFn(LogLevel.WARN, config)(message);
var writeInfo = (message, config) => getLogFn(LogLevel.INFO, config)(message);
var writeSuccess = (message, config) => getLogFn(LogLevel.SUCCESS, config)(message);
var writeDebug = (message, config) => getLogFn(LogLevel.DEBUG, config)(message);
var writeTrace = (message, config) => getLogFn(LogLevel.TRACE, config)(message);
var writeSystem = (message, config) => getLogFn(LogLevel.ALL, config)(message);
var getStopwatch = (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
`
);
};
};
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 (e) {
return false;
}
};
export {
getLogFn,
writeFatal,
writeError,
writeWarning,
writeInfo,
writeSuccess,
writeDebug,
writeTrace,
writeSystem,
getStopwatch,
formatLogMessage
};