rednote-mcp
Version:
A friendly tool to help you access and interact with Xiaohongshu (RedNote) content through Model Context Protocol.
93 lines (92 loc) • 3.9 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.stream = exports.LOGS_DIR = void 0;
exports.packLogs = packLogs;
const winston_1 = __importDefault(require("winston"));
const path_1 = __importDefault(require("path"));
const fs_1 = __importDefault(require("fs"));
require("winston-daily-rotate-file");
const os_1 = __importDefault(require("os"));
const archiver_1 = __importDefault(require("archiver"));
// Get platform-specific app data directory
function getAppDataDir() {
const platform = process.platform;
const appName = 'rednote-mcp';
switch (platform) {
case 'win32':
// Windows: %APPDATA%\rednote-mcp\logs
return path_1.default.join(process.env.APPDATA || '', appName, 'logs');
case 'darwin':
// macOS: ~/Library/Application Support/rednote-mcp/logs
return path_1.default.join(os_1.default.homedir(), 'Library', 'Application Support', appName, 'logs');
case 'linux':
// Linux: ~/.local/share/rednote-mcp/logs
return path_1.default.join(os_1.default.homedir(), '.local', 'share', appName, 'logs');
default:
// Fallback to current directory if platform is not recognized
return path_1.default.join(process.cwd(), 'logs');
}
}
// Constants for log management
exports.LOGS_DIR = getAppDataDir();
const MAX_LOG_SIZE = 10 * 1024 * 1024; // 10MB
const MAX_LOG_FILES = 5; // Keep last 5 log files
// Create logs directory if it doesn't exist
if (!fs_1.default.existsSync(exports.LOGS_DIR)) {
fs_1.default.mkdirSync(exports.LOGS_DIR, { recursive: true });
}
// Configure logger
const logger = winston_1.default.createLogger({
level: 'info',
format: winston_1.default.format.combine(winston_1.default.format.timestamp(), winston_1.default.format.json()),
transports: [
// Write all logs with level 'error' and below to error.log
new winston_1.default.transports.DailyRotateFile({
filename: path_1.default.join(exports.LOGS_DIR, 'error-%DATE%.log'),
datePattern: 'YYYY-MM-DD',
maxSize: MAX_LOG_SIZE,
maxFiles: MAX_LOG_FILES,
level: 'error',
format: winston_1.default.format.combine(winston_1.default.format.timestamp(), winston_1.default.format.json())
}),
// Write all logs with level 'info' and below to combined.log
new winston_1.default.transports.DailyRotateFile({
filename: path_1.default.join(exports.LOGS_DIR, 'combined-%DATE%.log'),
datePattern: 'YYYY-MM-DD',
maxSize: MAX_LOG_SIZE,
maxFiles: MAX_LOG_FILES,
format: winston_1.default.format.combine(winston_1.default.format.timestamp(), winston_1.default.format.json())
})
]
});
// Add a stream for Morgan (if needed in the future)
exports.stream = {
write: (message) => {
logger.info(message.trim());
}
};
// Function to pack logs into a zip file
async function packLogs() {
const output = fs_1.default.createWriteStream(path_1.default.join(process.cwd(), 'rednote-logs.zip'));
const archive = (0, archiver_1.default)('zip', {
zlib: { level: 9 } // Sets the compression level
});
return new Promise((resolve, reject) => {
output.on('close', () => {
resolve(path_1.default.join(process.cwd(), 'rednote-logs.zip'));
});
archive.on('error', (err) => {
reject(err);
});
archive.pipe(output);
// Add all log files to the archive
if (fs_1.default.existsSync(exports.LOGS_DIR)) {
archive.directory(exports.LOGS_DIR, 'logs');
}
archive.finalize();
});
}
exports.default = logger;