feat: delete old specific parsers and use new generic parser in file_monitor
This commit is contained in:
parent
0830b403e0
commit
794180c6ab
9 changed files with 21 additions and 595 deletions
|
|
@ -8,6 +8,7 @@ import (
|
|||
"strings"
|
||||
"watch-tool/models"
|
||||
"watch-tool/parser"
|
||||
"watch-tool/patterns"
|
||||
|
||||
"github.com/hpcloud/tail"
|
||||
)
|
||||
|
|
@ -22,22 +23,28 @@ func NewFileMonitor(config ToolConfig, hostname string) *FileMonitor {
|
|||
var logParser parser.Parser
|
||||
|
||||
if config.Format.Pattern != "" {
|
||||
pattern, err := regexp.Compile(config.Format.Pattern)
|
||||
compiledRegex, err := regexp.Compile(config.Format.Pattern)
|
||||
if err != nil {
|
||||
slog.Error("invalid regex pattern", "tool", config.Name, "error", err)
|
||||
logParser = &parser.DefaultParser{}
|
||||
slog.Error("Invalid regex pattern in tool config", "tool", config.Name, "error", err)
|
||||
logParser = parser.NewGenericParser(config.Name, hostname)
|
||||
} else {
|
||||
logParser = &parser.RegexLogParser{
|
||||
Pattern: pattern,
|
||||
Fields: config.Format.Fields,
|
||||
Toolname: config.Name,
|
||||
gp := parser.NewGenericParser(config.Name, hostname)
|
||||
|
||||
customExtractor := patterns.CompiledExtractor{
|
||||
Name: "config_custom_pattern",
|
||||
Pattern: compiledRegex,
|
||||
Fields: config.Format.Fields,
|
||||
}
|
||||
|
||||
gp.Extractors = append(gp.Extractors, customExtractor)
|
||||
logParser = gp
|
||||
}
|
||||
} else {
|
||||
var err error
|
||||
logParser, err = parser.New(config.Name, "custom", hostname)
|
||||
if err != nil {
|
||||
slog.Error("cannot get tool specific parser", "error", err)
|
||||
slog.Error("Cannot get tool specific parser from factory", "error", err)
|
||||
logParser = parser.NewGenericParser(config.Name, hostname)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -74,7 +81,7 @@ func (fm *FileMonitor) Start(ctx context.Context, out chan<- models.LogMessage)
|
|||
}
|
||||
|
||||
if line.Err != nil {
|
||||
slog.Error("error reading log file", "tool", fm.config.Name, "error", line.Err)
|
||||
slog.Error("Error reading log file", "tool", fm.config.Name, "error", line.Err)
|
||||
continue
|
||||
}
|
||||
|
||||
|
|
@ -84,7 +91,11 @@ func (fm *FileMonitor) Start(ctx context.Context, out chan<- models.LogMessage)
|
|||
|
||||
entry, err := fm.parser.Parse(line.Text)
|
||||
if err != nil {
|
||||
slog.Error("error parsing log line", "error", err)
|
||||
slog.Error("Error parsing log line", "tool", fm.config.Name, "error", err)
|
||||
} else {
|
||||
if entry.Tool == "" {
|
||||
entry.Tool = fm.config.Name
|
||||
}
|
||||
}
|
||||
|
||||
select {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue