Log Error Checker Script
PowerShell script that scans log files for HTTP 4xx/5xx status codes and error keywords.
Usage
# Current directory, *.log files
.\script.ps1
# Custom folder, *.log files
.\script.ps1 -LogPath 'C:\logs'
# Custom folder, *.txt files
.\script.ps1 -LogPath 'C:\logs' -FilePattern '*.txt'
What It Does
- Scans log files for HTTP error status codes (4xx/5xx)
- Looks for error keywords: error, exception, failed, fatal, timeout
- Shows line numbers and context for each error found
- Provides summary of total errors across all files
Script Code
# Log Error Checker Script
# Scans log files for HTTP 4xx/5xx status codes and error keywords
param(
[string]$LogPath = ".",
[string]$FilePattern = "*.log"
)
Write-Host "=== Log Error Checker ===" -ForegroundColor Cyan
Write-Host "Scanning: $LogPath for $FilePattern" -ForegroundColor Yellow
Write-Host ""
# Get all log files
$logFiles = Get-ChildItem -Path $LogPath -Filter $FilePattern -File
if (-not $logFiles) {
Write-Warning "No log files found!"
exit
}
$totalErrors = 0
foreach ($file in $logFiles) {
Write-Host "Checking: $($file.Name)" -ForegroundColor Green
$lineNum = 0
$fileErrors = 0
Get-Content $file.FullName | ForEach-Object {
$lineNum++
$line = $_
# Look for HTTP status codes in proper context (not ports!)
$httpPatterns = @(
'HTTP/\d\.\d\s+([45]\d{2})', # HTTP/1.1 404
'status[:\s]+([45]\d{2})', # status: 404
'"[A-Z]+\s+\S+\s+([45]\d{2})', # "GET /path 404
'\s([45]\d{2})\s+(Not Found|Forbidden|Internal Server Error|Bad Gateway)' # 404 Not Found
)
foreach ($pattern in $httpPatterns) {
if ($line -match $pattern) {
$statusCode = $matches[1]
Write-Host " Line $lineNum : HTTP $statusCode" -ForegroundColor Red
Write-Host " $line" -ForegroundColor Gray
$fileErrors++
$totalErrors++
break
}
}
# Look for general error keywords
$errorWords = @('error', 'exception', 'failed', 'fatal', 'timeout')
foreach ($word in $errorWords) {
if ($line -match "\b$word\b" -and $line -notmatch "HTTP/\d\.\d\s+[45]\d{2}") {
Write-Host " Line $lineNum : $word detected" -ForegroundColor Magenta
Write-Host " $line" -ForegroundColor Gray
$fileErrors++
$totalErrors++
break
}
}
}
if ($fileErrors -eq 0) {
Write-Host " ✓ No errors found" -ForegroundColor Green
} else {
Write-Host " ✗ Found $fileErrors errors" -ForegroundColor Red
}
Write-Host ""
}
Write-Host "=== SUMMARY ===" -ForegroundColor Cyan
Write-Host "Total errors found: $totalErrors" -ForegroundColor $(if($totalErrors -gt 0){"Red"}else{"Green"})
Last updated: 2025-08-26 20:00 UTC