Skip to main content
Glama
log-buffer.ts1.62 kB
export class LogBuffer { private buffer: string[] = [] private maxLines: number private readonly maxLineLength = 5000 constructor(maxLines: number = 10000) { this.maxLines = maxLines } add(line: string): void { try { // Truncate extremely long lines to prevent memory issues const truncatedLine = line.length > this.maxLineLength ? line.substring(0, this.maxLineLength) + '... (truncated)' : line const timestamp = new Date().toISOString() this.buffer.push(`[${timestamp}] ${truncatedLine}`) // Remove multiple lines if buffer is significantly over limit if (this.buffer.length > this.maxLines) { const excess = this.buffer.length - this.maxLines this.buffer.splice(0, Math.max(1, excess)) } } catch (error) { // Fallback if timestamp generation or other operations fail try { this.buffer.push(`[error] Failed to add log: ${error instanceof Error ? error.message : 'unknown error'}`) // Ensure we don't exceed limits even in error case if (this.buffer.length > this.maxLines) { this.buffer.shift() } } catch { // If even error logging fails, just ensure buffer doesn't grow unbounded if (this.buffer.length > this.maxLines) { this.buffer.shift() } } } } getLines(count?: number): string[] { if (!count) { return [...this.buffer] } return this.buffer.slice(-count) } clear(): void { this.buffer = [] } get length(): number { return this.buffer.length } }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/brennancheung/mcp-rewatch'

If you have feedback or need assistance with the MCP directory API, please join our Discord server