Spaces:
Sleeping
Sleeping
File size: 4,470 Bytes
36d488d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 | const winston = require('winston');
const path = require('path');
const chalk = require('chalk');
class Logger {
constructor(component = 'System') {
this.component = component;
this.winston = this.createWinstonLogger();
}
createWinstonLogger() {
const logDir = path.join(__dirname, '..', 'logs');
return winston.createLogger({
level: process.env.LOG_LEVEL || 'info',
format: winston.format.combine(
winston.format.timestamp(),
winston.format.errors({ stack: true }),
winston.format.json()
),
defaultMeta: { component: this.component },
transports: [
// Write all logs to combined.log
new winston.transports.File({
filename: path.join(logDir, 'combined.log'),
maxsize: 5242880, // 5MB
maxFiles: 5,
}),
// Write error logs to error.log
new winston.transports.File({
filename: path.join(logDir, 'error.log'),
level: 'error',
maxsize: 5242880, // 5MB
maxFiles: 3,
}),
// Write agent-specific logs
new winston.transports.File({
filename: path.join(logDir, `${this.component.toLowerCase()}.log`),
maxsize: 2097152, // 2MB
maxFiles: 3,
})
]
});
}
info(message, ...args) {
this.winston.info(message, ...args);
console.log(this.formatConsoleMessage('INFO', message, chalk.blue));
}
success(message, ...args) {
this.winston.info(message, ...args);
console.log(this.formatConsoleMessage('SUCCESS', message, chalk.green));
}
warn(message, ...args) {
this.winston.warn(message, ...args);
console.log(this.formatConsoleMessage('WARN', message, chalk.yellow));
}
error(message, error = null, ...args) {
if (error) {
this.winston.error(message, { error: error.message, stack: error.stack, ...args });
} else {
this.winston.error(message, ...args);
}
console.log(this.formatConsoleMessage('ERROR', message, chalk.red));
if (error && process.env.NODE_ENV !== 'production') {
console.error(chalk.red(error.stack));
}
}
debug(message, ...args) {
this.winston.debug(message, ...args);
if (process.env.NODE_ENV !== 'production') {
console.log(this.formatConsoleMessage('DEBUG', message, chalk.gray));
}
}
formatConsoleMessage(level, message, colorFunc) {
const timestamp = new Date().toLocaleTimeString();
const componentTag = chalk.cyan(`[${this.component}]`);
const levelTag = colorFunc(`[${level}]`);
return `${chalk.gray(timestamp)} ${componentTag} ${levelTag} ${message}`;
}
// Method to create specialized loggers for different purposes
static createAgentLogger(agentName) {
return new Logger(agentName);
}
static createSystemLogger() {
return new Logger('System');
}
static createAPILogger() {
return new Logger('API');
}
// Performance logging
startTimer(label) {
const startTime = Date.now();
return {
end: () => {
const duration = Date.now() - startTime;
this.info(`${label} completed in ${duration}ms`);
return duration;
}
};
}
// Structured logging for important events
logEvent(eventType, data = {}) {
this.winston.info('System Event', {
eventType,
timestamp: new Date().toISOString(),
...data
});
}
// Log content generation pipeline
logContentPipeline(stage, contentId, status, data = {}) {
this.winston.info('Content Pipeline', {
stage,
contentId,
status,
timestamp: new Date().toISOString(),
...data
});
}
// Log publishing events
logPublishing(action, videoId, status, data = {}) {
this.winston.info('Publishing Event', {
action,
videoId,
status,
timestamp: new Date().toISOString(),
...data
});
}
// Log analytics events
logAnalytics(videoId, metrics, insights = []) {
this.winston.info('Analytics Update', {
videoId,
metrics,
insights,
timestamp: new Date().toISOString()
});
}
// Log errors with context
logErrorWithContext(error, context = {}) {
this.winston.error('System Error', {
error: {
message: error.message,
stack: error.stack,
name: error.name
},
context,
timestamp: new Date().toISOString()
});
}
}
module.exports = { Logger }; |