capsule-ai-cli
Version:
The AI Model Orchestrator - Intelligent multi-model workflows with device-locked licensing
87 lines • 2.62 kB
JavaScript
import winston from 'winston';
import chalk from 'chalk';
import path from 'path';
import os from 'os';
const logLevels = {
error: 0,
warn: 1,
info: 2,
debug: 3,
trace: 4
};
const logColors = {
error: 'red',
warn: 'yellow',
info: 'blue',
debug: 'gray',
trace: 'magenta'
};
const consoleFormat = winston.format.printf(({ level, message, timestamp, ...metadata }) => {
const coloredLevel = chalk[logColors[level] || 'white'](level.toUpperCase());
const formattedTime = chalk.gray(new Date(timestamp).toLocaleTimeString());
let output = `${formattedTime} ${coloredLevel} ${message}`;
if (Object.keys(metadata).length > 0) {
output += '\n' + chalk.gray(JSON.stringify(metadata, null, 2));
}
return output;
});
export const logger = winston.createLogger({
levels: logLevels,
level: process.env.LOG_LEVEL || 'info',
format: winston.format.combine(winston.format.timestamp(), winston.format.errors({ stack: true }), winston.format.splat(), winston.format.json()),
transports: [
new winston.transports.Console({
format: winston.format.combine(winston.format.colorize({ all: false }), consoleFormat)
}),
new winston.transports.File({
filename: path.join(os.homedir(), '.capsule', 'logs', 'error.log'),
level: 'error',
maxsize: 5242880,
maxFiles: 5
}),
new winston.transports.File({
filename: path.join(os.homedir(), '.capsule', 'logs', 'combined.log'),
maxsize: 10485760,
maxFiles: 10
})
]
});
export const loggers = {
apiCall: (provider, model, tokens) => {
logger.debug('API call', { provider, model, tokens });
},
apiError: (provider, error) => {
logger.error(`API error from ${provider}`, {
error: error.message,
stack: error.stack
});
},
cost: (amount, provider) => {
logger.info(`Cost: $${amount.toFixed(4)}`, { provider });
},
performance: (operation, duration) => {
logger.debug(`${operation} took ${duration}ms`);
}
};
export class LogCapture {
logs = [];
stream;
start() {
this.stream = new winston.transports.Stream({
stream: {
write: (message) => {
this.logs.push(message);
}
}
});
logger.add(this.stream);
}
stop() {
logger.remove(this.stream);
return this.logs;
}
clear() {
this.logs = [];
}
}
//# sourceMappingURL=logger.js.map