I wrote a color coded log tail utility to make things easier for a colleague to see what was happening when tracking down some issues. So now I have an fun little log viewer. In reality, I had some down time and needed to do anything to make it through the day.
$LogPath = Read-Host -Prompt "Log path [REQUIRED]"
$LineCount = Read-Host -Prompt "Amount of lines to watch [40 DEFAULT]"
$ErrorKW = Read-Host -Prompt "Enter error keywords, comma separated [OPTIONAL]"
$WarningKW = Read-Host -Prompt "Enter warning keywords, comma separated [OPTIONAL]"
$SuccessKW = Read-Host -Prompt "Enter success keywords, comma separated [OPTIONAL]"
$MiscKW = Read-Host -Prompt "Enter misc. keywords, comma separated [OPTIONAL]"
$Filter = Read-Host -Prompt "Enter filter items, comma separated [OPTIONAL]"
clear
if (-not $LogPath) {
Write-Host "Please provide a log to tail."
Exit 0
}
if (-not $LineCount) {
$LineCount = 40
} else {
$LineCount = [int]$LineCount
}
if (-not $ErrorKW.Trim()) {
$ErrorArray = @("fail", "fatal", "unsuccessful", "critical", "error")
} else {
$ErrorKW = $ErrorKW.Trim().ToLower()
$ErrorArray = @()
$ErrorKW -split ',' | ForEach-Object {
$ErrorArray += $_.Trim()
}
}
if (-not $WarningKW.Trim()) {
$WarningArray = @("warn")
} else {
$WarningKW = $WarningKW.Trim().ToLower()
$WarningArray = @()
$WarningKW -split ',' | ForEach-Object {
$WarningArray += $_.Trim()
}
}
if (-not $SuccessKW.Trim()) {
$SuccessArray = @("ok", "success", "succeeded")
} else {
$SuccessKW = $SuccessKW.Trim().ToLower()
$SuccessArray = @()
$SuccessKW -split ',' | ForEach-Object {
$SuccessArray += $_.Trim()
}
}
if (-not $MiscKW.Trim()) {
$MiscArray = @("info", "debug")
} else {
$MiscKW = $MiscKW.Trim().ToLower()
$MiscArray = @()
$MiscKW -split ',' | ForEach-Object {
$MiscArray += $_.Trim()
}
}
if ($Filter.Trim()) {
$Filter= $Filter.Trim().ToLower()
$FilterArray = @()
$Filter -split ',' | ForEach-Object {
$FilterArray += $_.Trim()
}
}
Write-Host "watching: $LogPath" -ForegroundColor Cyan
Write-Host "ctrl + c to exit."
Write-Host "--------------------------------------------------------------"
$LineCount = 0
gc -Path $LogPath -Tail $LineCount -Wait |
% {
$ErrorMatch = $null
$WarningMatch = $null
$SuccessMatch = $null
$MiscMatch = $null
if ($_.Trim()) {
$Line = $_.Trim().ToLower()
$Output = $_.Replace("`n","").Replace("`r","").Trim()
$FilterMatch = $FilterArray | Where-Object { $Line -match "$_+" }
if (-not $FilterMatch.Count -gt 0) {
$ErrorMatch = $ErrorArray | Where-Object { $Line -match "$_+" }
$WarningMatch = $WarningArray | Where-Object { $Line -match "$_+" }
$SuccessMatch = $SuccessArray | Where-Object { $Line -match "$_+" }
$MiscMatch = $MiscArray | Where-Object { $Line -match "$_+" }
if ( $ErrorMatch.Count -gt 0 ) {
Write-Host $Output -ForegroundColor Red
} elseif ( $WarningMatch.Count -gt 0 ) {
Write-Host $Output -ForegroundColor Yellow
} elseif ( $SuccessMatch.Count -gt 0 ) {
Write-Host $Output -ForegroundColor Green
} elseif ( $MiscMatch.Count -gt 0 ) {
Write-Host $Output -ForegroundColor Cyan
} else {
Write-Host $Output
}
}
}
}