Nginx Error Log in GO
Parses Nginx error log lines.
Try it in the GO tester →Pattern
regexGO
^(\d{4}/\d{2}/\d{2} \d{2}:\d{2}:\d{2}) \[(\w+)\] (\d+)#(\d+): (.*)$Go (RE2) code
goGo
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`^(\d{4}/\d{2}/\d{2} \d{2}:\d{2}:\d{2}) \[(\w+)\] (\d+)#(\d+): (.*)$`)
input := `2023/10/11 10:00:00 [error] 1234#5678: *1 connect() failed`
for _, match := range re.FindAllString(input, -1) {
fmt.Println(match)
}
}Uses `regexp.MustCompile` (panics on bad patterns at startup) and `FindAllString` for all matches.
How the pattern works
Captures timestamp, log level, PID, TID, and the error message from standard Nginx error log format.
Examples
Input
2023/10/11 10:00:00 [error] 1234#5678: *1 connect() failedMatches
2023/10/11 10:00:00 [error] 1234#5678: *1 connect() failed