mp-lens
Version:
微信小程序分析工具 (Unused Code, Dependencies, Visualization)
160 lines • 5.36 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.logger = exports.LogLevel = void 0;
/**
* Verbosity levels for logging:
* 0 = ESSENTIAL: Only critical information (errors, warnings, final results)
* 1 = NORMAL: Basic debug information (DEFAULT with --verbose flag)
* 2 = VERBOSE: Detailed debug information (configuration, processing steps)
* 3 = TRACE: Extremely detailed information (path resolution, all file operations)
*/
var LogLevel;
(function (LogLevel) {
LogLevel[LogLevel["ESSENTIAL"] = 0] = "ESSENTIAL";
LogLevel[LogLevel["NORMAL"] = 1] = "NORMAL";
LogLevel[LogLevel["VERBOSE"] = 2] = "VERBOSE";
LogLevel[LogLevel["TRACE"] = 3] = "TRACE";
})(LogLevel || (exports.LogLevel = LogLevel = {}));
/**
* Debug logger utility for the analyzer
*/
class DebugLogger {
/**
* Create a new debug logger
*/
constructor(options = {}) {
var _a, _b, _c, _d;
this.options = {
level: (_a = options.level) !== null && _a !== void 0 ? _a : (options.level === 0 ? 0 : LogLevel.ESSENTIAL),
projectRoot: options.projectRoot,
useRelativePaths: (_b = options.useRelativePaths) !== null && _b !== void 0 ? _b : true,
useColors: (_c = options.useColors) !== null && _c !== void 0 ? _c : true,
language: (_d = options.language) !== null && _d !== void 0 ? _d : 'en',
};
}
/**
* Update logger options
*/
setOptions(options) {
this.options = { ...this.options, ...options };
}
/**
* Set the verbosity level
*/
setLevel(level) {
if (typeof level === 'boolean') {
// Convert boolean verbose flag to appropriate level
this.options.level = level ? LogLevel.NORMAL : LogLevel.ESSENTIAL;
}
else {
this.options.level = level;
}
}
/**
* Get the current verbosity level
*/
getLevel() {
return this.options.level;
}
/**
* Set the project root for path normalization
*/
setProjectRoot(projectRoot) {
this.options.projectRoot = projectRoot;
}
/**
* Log a message at ESSENTIAL level (always shown)
*/
info(message, ...args) {
this._log('INFO', LogLevel.ESSENTIAL, message, ...args);
}
/**
* Log a warning message (always shown)
*/
warn(message, ...args) {
this._log('WARN', LogLevel.ESSENTIAL, message, ...args);
}
/**
* Log an error message (always shown)
*/
error(message, ...args) {
this._log('ERROR', LogLevel.ESSENTIAL, message, ...args);
}
/**
* Log a message at NORMAL level
* Only shown when verbose flag is enabled
*/
debug(message, ...args) {
this._log('DEBUG', LogLevel.NORMAL, message, ...args);
}
/**
* Log a message at VERBOSE level
* Requires verbosity level of 2 or higher
*/
verbose(message, ...args) {
this._log('VERBOSE', LogLevel.VERBOSE, message, ...args);
}
/**
* Log a message at TRACE level
* Requires verbosity level of 3
*/
trace(message, ...args) {
this._log('TRACE', LogLevel.TRACE, message, ...args);
}
/**
* Normalize paths in log messages if needed
*/
normalizePath(input) {
if (!this.options.useRelativePaths || !this.options.projectRoot) {
return input;
}
// Try to find and convert absolute paths to relative ones
return input.replace(new RegExp(this.options.projectRoot.replace(/\\/g, '\\\\'), 'g'), '.');
}
/**
* Internal logging implementation
*/
_log(prefix, level, message, ...args) {
if (this.options.level < level) {
return;
}
// Normalize paths in the message
if (this.options.projectRoot) {
message = this.normalizePath(message);
// Also try to normalize paths in objects
args = args.map((arg) => {
if (typeof arg === 'string') {
return this.normalizePath(arg);
}
else if (typeof arg === 'object' && arg !== null) {
// For objects, we need to stringify and normalize all strings
try {
const str = JSON.stringify(arg);
return JSON.parse(this.normalizePath(str));
}
catch (e) {
return arg;
}
}
return arg;
});
}
// Format the message with arguments
let formatted = message;
if (args.length > 0) {
if (args.length === 1 && typeof args[0] === 'object') {
// Special case for logging objects - format them nicely
formatted = `${message} ${JSON.stringify(args[0], null, 2)}`;
}
else {
formatted = `${message} ${args
.map((a) => (typeof a === 'object' ? JSON.stringify(a) : String(a)))
.join(' ')}`;
}
}
console.log(`${prefix} - ${formatted}`);
}
}
// Create a default instance for convenient import
exports.logger = new DebugLogger();
//# sourceMappingURL=debug-logger.js.map