49 lines
1.2 KiB
Go
49 lines
1.2 KiB
Go
package parser
|
|
|
|
import (
|
|
"log/slog"
|
|
"regexp"
|
|
"strings"
|
|
"time"
|
|
"tixel_watch/helpers"
|
|
"tixel_watch/models"
|
|
)
|
|
|
|
var (
|
|
amServicePattern = regexp.MustCompile(`^(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?Z)\s+(\w+)\s+(\d+)\s+---\s+\[\s*([^\]]*)\]\s+([\w\.]+)\s*:\s*(.*)$`)
|
|
)
|
|
|
|
type AMParser struct{}
|
|
|
|
func (a *AMParser) Parse(line string) (models.LogMessage, error) {
|
|
newEntry := models.LogMessage{
|
|
Service: "access-manager",
|
|
}
|
|
syslogFields, logContent := helpers.ExtractSyslogHeader(line)
|
|
newEntry.Host = syslogFields.Hostname
|
|
|
|
matches := amServicePattern.FindStringSubmatch(strings.TrimSpace(logContent))
|
|
if len(matches) != 7 {
|
|
newEntry.Timestamp = time.Now()
|
|
newEntry.LogMessage = line
|
|
return newEntry, nil
|
|
}
|
|
|
|
timestampStr := strings.Join(strings.Split(matches[1], " "), "T")
|
|
timestamp, err := helpers.ParseRFC3339WithOptionalZ(timestampStr)
|
|
if err != nil {
|
|
slog.Error("unable to parse time", "error", err)
|
|
return newEntry, err
|
|
}
|
|
baseInfo := models.AMBaseInfo{
|
|
ProcessID: matches[3],
|
|
ThreadID: strings.TrimSpace(matches[4]),
|
|
LoggerName: matches[5],
|
|
}
|
|
newEntry.Timestamp = timestamp
|
|
newEntry.LogLevel = matches[2]
|
|
newEntry.LogMessage = matches[6]
|
|
newEntry.ServiceInformation = baseInfo
|
|
|
|
return newEntry, nil
|
|
}
|