@ace-sdk/cli
Version:
ACE CLI - Command-line tool for intelligent pattern learning and playbook management
229 lines • 6.47 kB
JavaScript
/**
* CLI Logger - Unified Logging Layer for ACE CLI
*
* Implements ILogger from @ace-sdk/core with CLI-specific features:
* - Respect for --json, --quiet, --verbose flags
* - Proper stderr/stdout separation
* - Spinner management for long-running operations
* - Colored output via chalk
*
* @package @ace-sdk/cli
*/
import chalk from 'chalk';
import ora from 'ora';
/**
* CLI Logger implementing ILogger interface
*
* Provides logging with CLI-specific enhancements like spinners
* and colored output while being compatible with @ace-sdk/core
*/
export class CliLogger {
options;
activeSpinner = null;
constructor(options) {
this.options = options;
}
/**
* Debug message (only shown with --verbose, to stderr)
*/
debug(message, data) {
if (!this.options.verbose)
return;
if (this.options.quiet)
return;
if (this.options.json) {
console.error(JSON.stringify({ level: 'debug', message, data }));
}
else {
console.error(chalk.dim(`[DEBUG] ${message}`));
if (data) {
console.error(chalk.dim(JSON.stringify(data, null, 2)));
}
}
}
/**
* Informational message (to stderr in non-JSON mode)
*/
info(message, data) {
if (this.options.quiet)
return;
if (this.options.json) {
if (data) {
console.error(JSON.stringify({ level: 'info', message, data }));
}
}
else {
console.error(chalk.blue('ℹ'), message);
if (data && this.options.verbose) {
console.error(chalk.dim(JSON.stringify(data, null, 2)));
}
}
}
/**
* Success message (to stderr in non-JSON mode)
*/
success(message, data) {
if (this.options.quiet)
return;
if (this.options.json) {
if (data) {
console.error(JSON.stringify({ level: 'success', message, data }));
}
}
else {
console.error(chalk.green('✓'), message);
if (data && this.options.verbose) {
console.error(chalk.dim(JSON.stringify(data, null, 2)));
}
}
}
/**
* Warning message (to stderr)
*/
warn(message, data) {
if (this.options.quiet)
return;
if (this.options.json) {
console.error(JSON.stringify({ level: 'warn', message, data }));
}
else {
console.error(chalk.yellow('⚠'), message);
if (data && this.options.verbose) {
console.error(chalk.dim(JSON.stringify(data, null, 2)));
}
}
}
/**
* Error message (to stderr)
*/
error(message, error) {
if (this.options.json) {
const errorData = { level: 'error', message };
if (error) {
errorData.error = error instanceof Error ? {
message: error.message,
stack: error.stack
} : error;
}
console.error(JSON.stringify(errorData));
}
else {
console.error(chalk.red('✗'), message);
if (error && this.options.verbose) {
const errorMsg = error instanceof Error ? error.stack : error;
console.error(chalk.dim(errorMsg));
}
}
}
/**
* Trace message (only shown with --trace, to stderr)
*/
trace(message, data) {
if (!this.options.trace)
return;
if (this.options.quiet)
return;
if (this.options.json) {
console.error(JSON.stringify({ level: 'trace', message, data }));
}
else {
console.error(chalk.gray(`[TRACE] ${message}`));
if (data) {
console.error(chalk.gray(JSON.stringify(data, null, 2)));
}
}
}
/**
* Check if we're in JSON mode
*/
isJson() {
return this.options.json;
}
/**
* Check if we're in quiet mode
*/
isQuiet() {
return this.options.quiet;
}
/**
* Check if we're in verbose mode
*/
isVerbose() {
return this.options.verbose;
}
/**
* Check if we're in trace mode
*/
isTrace() {
return this.options.trace || false;
}
// =========================================================================
// CLI-Specific Methods (not part of ILogger interface)
// =========================================================================
/**
* Create a spinner for long-running operations
* Returns null if in JSON or quiet mode
*/
spinner(message) {
// Stop existing spinner if any
if (this.activeSpinner) {
this.activeSpinner.stop();
}
// Don't show spinners in JSON or quiet mode
if (this.options.json || this.options.quiet) {
return null;
}
this.activeSpinner = ora(message).start();
return this.activeSpinner;
}
/**
* Stop active spinner
*/
stopSpinner() {
if (this.activeSpinner) {
this.activeSpinner.stop();
this.activeSpinner = null;
}
}
/**
* Output data to stdout (for command results)
* This is the ONLY method that writes to stdout
*/
output(data) {
if (this.options.json) {
console.log(JSON.stringify(data, null, 2));
}
else {
// For non-JSON mode, data is already formatted by formatters
console.log(data);
}
}
/**
* Direct stderr write (for diagnostics that should always appear)
*/
stderr(message) {
if (!this.options.quiet) {
console.error(message);
}
}
}
// Global logger instance and options
export let globalOptions = {
json: false,
verbose: false,
quiet: false,
trace: false
};
export let logger = new CliLogger(globalOptions);
/**
* Initialize global logger with options
* Called from CLI entry point after parsing arguments
*/
export function initializeLogger(options) {
globalOptions = options;
logger = new CliLogger(options);
return logger;
}
// Backwards-compatible alias
export { CliLogger as Logger };
//# sourceMappingURL=logger.js.map