UNPKG

@kadena/kadena-cli

Version:

Kadena CLI tool to interact with the Kadena blockchain (manage keys, transactions, etc.)

126 lines 4.28 kB
import { Chalk } from 'chalk'; import jsYaml from 'js-yaml'; import { formatWithOptions } from 'node:util'; import z from 'zod'; import { maskSensitiveInfo } from './logger-utils.js'; const ENV_LEVEL = process.env.KADENA_LOG; // If in CI or other non-interactive environment, disable colors const stderrColorsEnabled = process.stderr.isTTY !== true || process.env.COLORS === 'false' || process.env.COLORS === '0' || process.env.NODE_DISABLE_COLORS === 'true' || process.env.NODE_DISABLE_COLORS === '1' || process.env.NO_COLOR === 'true' || process.env.NO_COLOR === '1' ? false : true; // Be more strict for stdout, only enable colors if stdout is a TTY const stdoutColorsEnabled = stderrColorsEnabled === false || process.stdout.isTTY !== true ? false : true; export const LEVELS = { error: 0, warning: 1, output: 2, header: 3, info: 4, debug: 5, verbose: 6, }; /** Accepts levels as strings "info" or "3" and output as numbers */ const levelSchema = z.union([ z .enum(Object.keys(LEVELS)) .transform((level) => LEVELS[level]), z .enum(Object.values(LEVELS).map(String)) .transform((level) => Number(level)), ]); const stdOutChalk = new Chalk({ level: stdoutColorsEnabled ? 2 : 0 }); const stdErrChalk = new Chalk({ level: stderrColorsEnabled ? 2 : 0 }); export const defaultTransport = (record) => { var _a; // Give color to logs const NO_COLOR = (line) => line; const LEVEL_COLORS = { [LEVELS.error]: stdErrChalk.red, [LEVELS.warning]: stdErrChalk.yellow, [LEVELS.output]: NO_COLOR, [LEVELS.info]: NO_COLOR, [LEVELS.debug]: stdErrChalk.gray, [LEVELS.verbose]: stdErrChalk.gray, }; const COLOR = (_a = LEVEL_COLORS[record.level]) !== null && _a !== void 0 ? _a : NO_COLOR; // If level "output", write to stdout if (record.level === LEVELS.output) { const formatted = formatWithOptions({ colors: stdoutColorsEnabled }, ...record.args); process.stdout.write(`${stdoutColorsEnabled ? COLOR(formatted) : formatted}\n`); } else { // Otherwise write to stderr const formatted = formatWithOptions({ colors: stderrColorsEnabled }, ...record.args); process.stderr.write(`${COLOR(formatted)}\n`); } }; class Logger { constructor(data) { this._transport = defaultTransport; // chalk takes the more strict version of stdout because we can't be sure which log level it is used this._chalk = stdOutChalk; this._outputMode = 'plain'; this.LEVELS = LEVELS; this.level = LEVELS.info; if ((data === null || data === void 0 ? void 0 : data.level) !== undefined) { this.level = data.level; } else if (ENV_LEVEL !== undefined) { const parsed = levelSchema.safeParse(ENV_LEVEL); if (parsed.success) this.level = parsed.data; } } setTransport(transport) { this._transport = transport; } setOutputMode(outputMode) { this._outputMode = outputMode; } setLevel(level) { this.level = level; } get color() { return this._chalk; } _log(level, args) { if (this.level >= level) { const processedArgs = args.map((arg) => maskSensitiveInfo(arg)); this._transport({ date: new Date(), level, args: processedArgs }, this); } } error(...args) { this._log(LEVELS.error, args); } warning(...args) { this._log(LEVELS.warning, args); } output(plain, formatted) { if (this._outputMode === 'json') { this._log(LEVELS.output, [JSON.stringify(formatted, null, 2)]); } else if (this._outputMode === 'yaml') { this._log(LEVELS.output, [jsYaml.dump(formatted, { lineWidth: -1 })]); } else if (plain !== null) { this._log(LEVELS.output, [plain]); } } info(...args) { this._log(LEVELS.info, args); } debug(...args) { this._log(LEVELS.debug, args); } verbose(...args) { this._log(LEVELS.verbose, args); } } export const log = new Logger(); //# sourceMappingURL=logger.js.map