UNPKG

@simonecoelhosfo/optimizely-mcp-server

Version:

Optimizely MCP Server for AI assistants with integrated CLI tools

89 lines 3.01 kB
/** * Logging utilities for safe object logging with size limits */ /** * Maximum size for stringified objects in logs (in characters) */ const MAX_LOG_SIZE = 1000; /** * Safely log an object with size limits * @param obj The object to log * @param maxSize Maximum size in characters (default: 1000) * @returns Object safe for logging */ export function safeLogObject(obj, maxSize = MAX_LOG_SIZE) { if (!obj || typeof obj !== 'object') { return obj; } try { const stringified = JSON.stringify(obj); const size = stringified.length; if (size <= maxSize) { return obj; } // Object is too large, return summary return { __truncated: true, size, type: Array.isArray(obj) ? 'array' : 'object', preview: stringified.substring(0, maxSize) + '...', keys: Array.isArray(obj) ? `[${obj.length} items]` : Object.keys(obj).slice(0, 10).join(', ') + (Object.keys(obj).length > 10 ? '...' : '') }; } catch (error) { // Circular reference or other stringify error return { __error: true, message: 'Unable to stringify object', type: Array.isArray(obj) ? 'array' : 'object', keys: Array.isArray(obj) ? `[${obj.length} items]` : Object.keys(obj).slice(0, 10).join(', ') }; } } /** * Get object metadata for logging without the full content * @param obj The object to analyze * @returns Metadata about the object */ export function getObjectMetadata(obj) { if (!obj || typeof obj !== 'object') { return { type: typeof obj, value: obj }; } try { const stringified = JSON.stringify(obj); return { type: Array.isArray(obj) ? 'array' : 'object', size: stringified.length, itemCount: Array.isArray(obj) ? obj.length : Object.keys(obj).length, keys: Array.isArray(obj) ? undefined : Object.keys(obj).slice(0, 5).join(', ') + (Object.keys(obj).length > 5 ? '...' : ''), sampleKeys: Array.isArray(obj) && obj.length > 0 ? Object.keys(obj[0] || {}).slice(0, 5).join(', ') : undefined }; } catch (error) { return { type: Array.isArray(obj) ? 'array' : 'object', error: 'Unable to analyze object', itemCount: Array.isArray(obj) ? obj.length : Object.keys(obj).length }; } } /** * Format a payload for logging with size limits * @param payload The payload to format * @returns Formatted payload safe for logging */ export function formatPayloadForLogging(payload) { if (!payload) return payload; // For small payloads, log them directly try { const size = JSON.stringify(payload).length; if (size < 500) { return payload; } } catch { } // For large payloads, return metadata return getObjectMetadata(payload); } //# sourceMappingURL=LoggingUtils.js.map