count_by_level
Count log entries grouped by severity level (ERROR, WARN, INFO, DEBUG) for a given log path and time period.
Instructions
Count log entries by severity level (ERROR, WARN, INFO, DEBUG)
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| logPath | No | Path to log file or directory | /var/log |
| startTime | No | Start time in ISO8601 format | |
| endTime | No | End time in ISO8601 format |
Implementation Reference
- src/tools/count_by_level.ts:50-78 (handler)Main exported handler function for the count_by_level tool. Parses input (logPath, startTime, endTime), finds all .log files via findLogFiles, aggregates counts from each file using countFile, and returns a formatted text summary of ERROR/WARN/INFO/DEBUG/total counts.
export async function countByLevel(input: ToolInput): Promise<{ content: Array<{ type: string; text: string }> }> { const { logPath = '/var/log', startTime, endTime } = input as CountByLevelInput; const files = findLogFiles(logPath); if (files.length === 0) { return { content: [{ type: 'text', text: `No .log files found in ${logPath}` }] }; } const totalCounts: CountResult = { ERROR: 0, WARN: 0, INFO: 0, DEBUG: 0, total: 0 }; for (const file of files) { const counts = await countFile(file, startTime, endTime); totalCounts.ERROR += counts.ERROR; totalCounts.WARN += counts.WARN; totalCounts.INFO += counts.INFO; totalCounts.DEBUG += counts.DEBUG; totalCounts.total += counts.total; } const text = `Log Level Distribution: ERROR: ${totalCounts.ERROR} WARN: ${totalCounts.WARN} INFO: ${totalCounts.INFO} DEBUG: ${totalCounts.DEBUG} ──────────── Total: ${totalCounts.total}`; return { content: [{ type: 'text', text }] }; } - src/tools/count_by_level.ts:7-11 (schema)Input type definition for count_by_level extending ToolInput with optional logPath, startTime, and endTime properties.
interface CountByLevelInput extends ToolInput { logPath?: string; startTime?: string; endTime?: string; } - src/index.ts:43-64 (registration)Registration of the count_by_level tool in the TOOLS array with name, description, and inputSchema specifying logPath, startTime, and endTime as optional parameters.
{ name: 'count_by_level', description: 'Count log entries by severity level (ERROR, WARN, INFO, DEBUG)', inputSchema: { type: 'object', properties: { logPath: { type: 'string', default: '/var/log', description: 'Path to log file or directory' }, startTime: { type: 'string', description: 'Start time in ISO8601 format' }, endTime: { type: 'string', description: 'End time in ISO8601 format' } } } }, - src/index.ts:150-151 (registration)Routing dispatch: the CallToolRequestSchema handler maps the 'count_by_level' case to invoke countByLevel(args || {}).
case 'count_by_level': return await countByLevel(args || {}); - src/tools/count_by_level.ts:30-48 (helper)Helper function countFile that reads a single log file line-by-line, parses each line with parseLogLine, filters by time range with isWithinTimeRange, and accumulates counts by log level.
async function countFile( filePath: string, startTime?: string, endTime?: string ): Promise<CountResult> { const counts: CountResult = { ERROR: 0, WARN: 0, INFO: 0, DEBUG: 0, total: 0 }; const stream = fs.createReadStream(filePath); const rl = readline.createInterface({ input: stream }); for await (const line of rl) { const entry = parseLogLine(line); if (entry && isWithinTimeRange(entry, startTime, endTime)) { counts[entry.level as LogLevel]++; counts.total++; } } return counts; }