[CmdletBinding()] param( [Parameter(Mandatory = $true)] [string]$Path, [ValidateRange(1, 500)] [int]$Top = 10, [string]$Pattern, [string]$Json, [string]$Csv ) $ErrorActionPreference = 'Stop' $resolved = Resolve-Path -LiteralPath $Path -ErrorAction Stop if (-not (Test-Path -LiteralPath $resolved -PathType Leaf)) { throw "Укажите файл: $resolved" } $levelRegex = [regex]'(?i)\b(TRACE|DEBUG|INFO|WARN|WARNING|ERROR|FATAL|CRITICAL)\b' $timeRegex = [regex]'^\s*(\d{4}-\d{2}-\d{2}[T ]\d{2}:\d{2}:\d{2})' $levels = @{} $messages = @{} $first = $null $last = $null $total = 0 foreach ($line in Get-Content -LiteralPath $resolved -Encoding UTF8) { if ($Pattern -and $line -notmatch $Pattern) { continue } $total++ $levelMatch = $levelRegex.Match($line) if ($levelMatch.Success) { $level = $levelMatch.Groups[1].Value.ToUpperInvariant() if ($level -eq 'WARNING') { $level = 'WARN' } if ($level -eq 'CRITICAL') { $level = 'FATAL' } } else { $level = 'OTHER' } if (-not $levels.ContainsKey($level)) { $levels[$level] = 0 } $levels[$level]++ $timeMatch = $timeRegex.Match($line) if ($timeMatch.Success) { if (-not $first) { $first = $timeMatch.Groups[1].Value } $last = $timeMatch.Groups[1].Value } $message = $line if ($timeMatch.Success) { $message = $message.Substring($timeMatch.Length) } $message = $levelRegex.Replace($message, '') $message = $message -replace '[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}', '' $message = $message -replace '0x[0-9a-fA-F]+', '0x#' $message = $message -replace '\d+', '#' $message = ($message -replace '\s+', ' ').Trim() if ($message.Length -gt 160) { $message = $message.Substring(0, 157) + '...' } if (-not $message) { $message = '(пустое сообщение)' } if (-not $messages.ContainsKey($message)) { $messages[$message] = 0 } $messages[$message]++ } if ($total -eq 0) { Write-Host 'Подходящих строк не найдено.' exit 1 } $levelRows = $levels.GetEnumerator() | Sort-Object Value -Descending | ForEach-Object { [pscustomobject]@{ Level = $_.Key; Count = $_.Value } } $topRows = $messages.GetEnumerator() | Sort-Object Value -Descending | Select-Object -First $Top | ForEach-Object { [pscustomobject]@{ Count = $_.Value; Message = $_.Key } } Write-Host "Файл: $resolved" Write-Host "Строк: $total, период: $first - $last" $levelRows | Format-Table -AutoSize $topRows | Format-Table -AutoSize -Wrap if ($Csv) { $topRows | Export-Csv -LiteralPath $Csv -NoTypeInformation -Encoding UTF8 Write-Host "CSV: $Csv" } if ($Json) { $summary = [pscustomobject]@{ File = $resolved.Path Lines = $total First = $first Last = $last Levels = @($levelRows) Top = @($topRows) } $jsonText = ConvertTo-Json -InputObject $summary -Depth 6 [IO.File]::WriteAllText([IO.Path]::GetFullPath($Json), $jsonText + [Environment]::NewLine, (New-Object System.Text.UTF8Encoding($false))) Write-Host "JSON: $Json" }