rashi-discord-bot-lib
Version:
đ Powerful Discord bot framework with built-in database, event handling, and utilities
124 lines âĸ 4.38 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Logger = void 0;
const winston_1 = __importDefault(require("winston"));
const path_1 = require("path");
const { combine, timestamp, printf, colorize, errors } = winston_1.default.format;
// Custom log format with proper types
const logFormat = printf(({ level, message, timestamp, stack }) => {
return `${timestamp} [${level}]: ${stack || message}`;
});
// Console format with colors and proper types
const consoleFormat = printf(({ level, message }) => {
const emojiMap = {
error: 'â',
warn: 'â ī¸',
info: 'âšī¸',
debug: 'đ'
};
const emoji = emojiMap[level] || 'âšī¸';
return `${emoji} ${message}`;
});
class Logger {
winston;
terminalEnabled = true;
constructor(options) {
const logDir = options?.logDir || './logs';
const enableFiles = options?.enableFiles ?? true;
const transports = [
new winston_1.default.transports.Console({
format: combine(colorize(), consoleFormat),
silent: !this.terminalEnabled
})
];
if (enableFiles) {
// Error logs
transports.push(new winston_1.default.transports.File({
filename: (0, path_1.join)(logDir, 'error.log'),
level: 'error',
format: combine(timestamp(), errors({ stack: true }), logFormat)
}));
// Combined logs
transports.push(new winston_1.default.transports.File({
filename: (0, path_1.join)(logDir, 'combined.log'),
format: combine(timestamp(), errors({ stack: true }), logFormat)
}));
}
this.winston = winston_1.default.createLogger({
level: process.env.LOG_LEVEL || 'info',
format: combine(timestamp(), errors({ stack: true }), winston_1.default.format.json()),
transports,
exceptionHandlers: enableFiles ? [
new winston_1.default.transports.File({
filename: (0, path_1.join)(logDir, 'exceptions.log')
})
] : [],
rejectionHandlers: enableFiles ? [
new winston_1.default.transports.File({
filename: (0, path_1.join)(logDir, 'rejections.log')
})
] : []
});
}
enableTerminalLogs() {
this.terminalEnabled = true;
const consoleTransport = this.winston.transports.find((t) => t instanceof winston_1.default.transports.Console);
if (consoleTransport && 'silent' in consoleTransport) {
consoleTransport.silent = false;
}
}
disableTerminalLogs() {
this.terminalEnabled = false;
const consoleTransport = this.winston.transports.find((t) => t instanceof winston_1.default.transports.Console);
if (consoleTransport && 'silent' in consoleTransport) {
consoleTransport.silent = true;
}
}
info(message, meta) {
this.winston.info(message, meta);
}
success(message, meta) {
// Winston doesn't have success level, use info with prefix
this.winston.info(`â
${message}`, meta);
}
warn(message, meta) {
this.winston.warn(message, meta);
}
error(message, error) {
if (error instanceof Error) {
this.winston.error(message, { error: error.stack });
}
else if (error) {
this.winston.error(message, { error });
}
else {
this.winston.error(message);
}
}
debug(message, meta) {
this.winston.debug(message, meta);
}
setLevel(level) {
this.winston.level = level;
}
getLevel() {
return this.winston.level;
}
// Method to add custom transports
addTransport(transport) {
this.winston.add(transport);
}
// Method to remove transports
removeTransport(transport) {
this.winston.remove(transport);
}
// Get winston instance for advanced usage
getWinston() {
return this.winston;
}
}
exports.Logger = Logger;
//# sourceMappingURL=Logger.js.map