syslog filter could take several filters

This commit is contained in:
nemunaire 2026-01-01 22:37:02 +07:00
commit 69594c2fe4
4 changed files with 48 additions and 23 deletions

View file

@ -14,9 +14,9 @@ import (
// SyslogTailer tails a syslog file and filters messages to the logging system.
type SyslogTailer struct {
path string
filter string
source string
path string
filters []string
source string
file *os.File
done chan struct{}
@ -29,12 +29,12 @@ type SyslogTailer struct {
// path: Path to the syslog file (e.g., "/var/log/messages")
// filter: Filter string to match in lines (e.g., "daemon.info iwd:")
// source: Source name for logging (e.g., "iwd")
func NewSyslogTailer(path, filter, source string) *SyslogTailer {
func NewSyslogTailer(path string, filters []string, source string) *SyslogTailer {
return &SyslogTailer{
path: path,
filter: filter,
source: source,
done: make(chan struct{}),
path: path,
filters: filters,
source: source,
done: make(chan struct{}),
}
}
@ -185,15 +185,22 @@ func (t *SyslogTailer) readLines(file *os.File) error {
line := scanner.Text()
// Check if the line contains the filter string
if !strings.Contains(line, t.filter) {
// Check if the line contains any of the filter strings
var matchedFilter string
for _, filter := range t.filters {
if strings.Contains(line, filter) {
matchedFilter = filter
break
}
}
if matchedFilter == "" {
continue
}
// Parse the syslog line to extract the message
// We look for "iwd:" (or whatever comes after the filter)
// The filter is "daemon.info iwd:" so we want to extract text after "iwd:"
daemonPrefix := extractDaemonPrefix(t.filter)
daemonPrefix := extractDaemonPrefix(matchedFilter)
message, ok := ParseSyslogLine(line, daemonPrefix)
if !ok {
// Couldn't parse the line, skip it