query_by_timerange
Retrieve log entries from a file or directory within a specified time range. Filter results by log level and limit the number of entries.
Instructions
Query logs within a specific time range
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| startTime | Yes | Start time in ISO8601 format | |
| endTime | Yes | End time in ISO8601 format | |
| logPath | No | Path to log file or directory | /var/log |
| level | No | Filter by log level | |
| limit | No | Maximum number of results |
Implementation Reference
- src/tools/query_by_timerange.ts:57-84 (handler)Main handler function that executes the query_by_timerange tool logic. Validates required startTime/endTime params, finds .log files, reads them line-by-line filtering by time range and optional level, and returns matching results.
export async function queryByTimerange(input: ToolInput): Promise<{ content: Array<{ type: string; text: string }> }> { const { startTime, endTime, logPath = '/var/log', level, limit = 100 } = input as QueryByTimerangeInput; if (!startTime || !endTime) { return { content: [{ type: 'text', text: 'Error: startTime and endTime are required' }] }; } const files = findLogFiles(logPath); if (files.length === 0) { return { content: [{ type: 'text', text: `No .log files found in ${logPath}` }] }; } const allResults: string[] = []; for (const file of files) { const results = await queryFile(file, startTime, endTime, level, limit - allResults.length); allResults.push(...results); if (allResults.length >= limit) break; } return { content: [{ type: 'text', text: allResults.length > 0 ? allResults.join('\n') : `No logs found between ${startTime} and ${endTime}` }] }; } - src/tools/query_by_timerange.ts:7-13 (schema)Input interface extending ToolInput, defining the schema with startTime, endTime (required), logPath, level, and limit (optional) fields.
interface QueryByTimerangeInput extends ToolInput { startTime: string; endTime: string; logPath?: string; level?: LogLevel; limit?: number; } - src/index.ts:152-153 (registration)Dispatch/case statement in CallToolRequestSchema handler that routes 'query_by_timerange' calls to the queryByTimerange function.
case 'query_by_timerange': return await queryByTimerange(args || {}); - src/index.ts:65-97 (registration)Tool registration in the TOOLS array with name, description, and JSON schema for input validation (startTime/endTime required, optional logPath/level/limit).
{ name: 'query_by_timerange', description: 'Query logs within a specific time range', inputSchema: { type: 'object', properties: { startTime: { type: 'string', description: 'Start time in ISO8601 format' }, endTime: { type: 'string', description: 'End time in ISO8601 format' }, logPath: { type: 'string', default: '/var/log', description: 'Path to log file or directory' }, level: { type: 'string', enum: ['ERROR', 'WARN', 'INFO', 'DEBUG'], description: 'Filter by log level' }, limit: { type: 'number', default: 100, description: 'Maximum number of results' } }, required: ['startTime', 'endTime'] } }, - Helper function queryFile that reads a single .log file via readline, parses each line with parseLogLine, filters by time range and level, and collects matching results.
async function queryFile( filePath: string, startTime: string, endTime: string, level?: LogLevel, limit?: number ): Promise<string[]> { const results: string[] = []; const stream = fs.createReadStream(filePath); const rl = readline.createInterface({ input: stream }); for await (const line of rl) { const entry = parseLogLine(line); if (!entry) continue; if (!isWithinTimeRange(entry, startTime, endTime)) continue; if (level && entry.level !== level) continue; results.push(`[${entry.timestamp.toISOString()}] [${entry.level}] ${entry.message}`); if (limit && results.length >= limit) break; } return results; }