function Get-Nginxlog {
[CmdletBinding()]
param (
[Parameter()]
[string]$LogFile,
[Parameter()]
[int]$Tail = 100,
[Parameter()]
[int]$TotalCount
)
$getContentParas = @{
Path = $LogFile
}
if ($TotalCount -gt 0) {
$getContentParas['TotalCount'] = $TotalCount
}
if ($Tail -gt 0) {
$getContentParas['Tail'] = $Tail
}
$LogParserConf = @(
@{
Name = "RemoteAddr"
Pattern = "(.+) - "
}
@{
Name = "RemoteUser"
Pattern = "(.+) "
}
@{
Name = "LocalTime"
Pattern = "*\[(.*)\] "
}
@{
Name = "Request"
Pattern = "`"(.*?)`" "
}
@{
Name = "Status"
Pattern = "(.+?) "
}
@{
Name = "BodyBytesSent"
Pattern = "(.+?) "
}
@{
Name = "Referer"
Pattern = "`"(.*?)`" "
}
@{
Name = "UserAgent"
Pattern = "`"(.*?)`" "
}
@{
Name = "RequestTime"
Pattern = "(.+?) "
}
@{
Name = "UpStreamResponseTime"
Pattern = "(.+?) "
}
@{
Name = "UpstreamCacheStatus"
Pattern = "(.+)"
}
)
Get-Content @getContentParas | ForEach-Object {
$line = $_
$pattern = ( $LogParserConf | ForEach-Object { $_.Pattern } ) -join ''
$line -match $pattern
$props = [ordered]@{}
1..($Matches.Count - 1) | ForEach-Object {
$props[$LogParserConf[$_ - 1].Name] = $Matches[$_]
}
[PSCustomObject]$props
}
}
本文链接: https://www.pstips.net/parse-nginx-log.html
请尊重原作者和编辑的辛勤劳动,欢迎转载,并注明出处!
请尊重原作者和编辑的辛勤劳动,欢迎转载,并注明出处!