refactor: use parser package for systemd logs
This commit is contained in:
parent
9aa1b7384d
commit
e468b3a0e3
13 changed files with 897 additions and 275 deletions
49
parser/tcc_parser.go
Normal file
49
parser/tcc_parser.go
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
package parser
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
"tixel_watch/helpers"
|
||||
"tixel_watch/models"
|
||||
)
|
||||
|
||||
var (
|
||||
tccServicePattern = 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 TCCParser struct{}
|
||||
|
||||
func (t *TCCParser) Parse(line string) (models.LogMessage, error) {
|
||||
newEntry := models.LogMessage{
|
||||
Service: "tixel-control-center",
|
||||
}
|
||||
syslogFields, logContent := helpers.ExtractSyslogHeader(line)
|
||||
newEntry.Host = syslogFields.Hostname
|
||||
|
||||
matches := tccServicePattern.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.TCCBaseInfo{
|
||||
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
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue