UNPKG

@szzbmy/lowcode-cli

Version:

🐇 lowcode-cli is an efficient cli tool for Rabbitpre plugin component secondary development. ❤️

76 lines (75 loc) 2.31 kB
"use strict"; /* * CLI Logger * @Author: Jiyu Shao * @Date: 2019-11-29 14:46:00 * @Last Modified by: Jiyu Shao * @Last Modified time: 2022-01-11 15:42:30 */ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.injectLogLevel = void 0; const chalk_1 = __importDefault(require("chalk")); /** 自定义各个 level 的配置 */ const logLevelOptionsMapping = { trace: { priority: 0, color: 'grey', // 这里不要直接使用 console.trace, 后面 ink 会使用 patch-console 来覆盖默认 console 的表现 func: (...args) => console.trace(...args), }, debug: { priority: 1, color: 'grey', func: (...args) => console.debug(...args), }, info: { priority: 2, color: 'cyan', func: (...args) => console.info(...args), }, warn: { priority: 3, color: 'yellow', func: (...args) => console.warn(...args), }, error: { priority: 4, color: 'red', func: (...args) => console.error(...args), }, }; /** 日志级别 */ let logLevel = 'info'; const logger = {}; Object.keys(logLevelOptionsMapping).forEach(currentLevel => { // 设置当前 logger 的方法 logger[currentLevel] = (...args) => { const currentLevelOption = logLevelOptionsMapping[currentLevel]; const logLevelOption = logLevelOptionsMapping[logLevel]; // 小于日志级别则不需要输出 if (currentLevelOption.priority < logLevelOption.priority) { return; } currentLevelOption.func( // @ts-ignore chalk_1.default[currentLevelOption.color](`[${currentLevel.toLocaleUpperCase()}]`.padEnd(9, ' '), ...args)); }; }); /** * 设置日志级别 * @param {string | undefined} level * @returns {boolean} 是否设置成功 */ const injectLogLevel = (level) => { if (level && logLevelOptionsMapping[level]) { logLevel = level; logger.debug('设置日志级别成功: ', level); return; } logger.debug('设置日志级别失败: ', level); }; exports.injectLogLevel = injectLogLevel; exports.default = logger;