standardize logs

This commit is contained in:
ppom 2023-04-27 12:33:56 +02:00
parent 067fb13e79
commit 7f93079f66
2 changed files with 23 additions and 23 deletions

View File

@ -67,7 +67,7 @@ func (c *Conf) setup() {
c.Patterns[patternName] = fmt.Sprintf("(?P<%s>%s)", patternName, pattern) c.Patterns[patternName] = fmt.Sprintf("(?P<%s>%s)", patternName, pattern)
} }
if len(c.Streams) == 0 { if len(c.Streams) == 0 {
log.Fatalln("Bad configuration: no streams configured!") log.Fatalln("FATAL Bad configuration: no streams configured!")
} }
for streamName := range c.Streams { for streamName := range c.Streams {
@ -75,7 +75,7 @@ func (c *Conf) setup() {
stream.name = streamName stream.name = streamName
if len(stream.Filters) == 0 { if len(stream.Filters) == 0 {
log.Fatalln("Bad configuration: no filters configured in '%s'!", stream.name) log.Fatalln("FATAL Bad configuration: no filters configured in '%s'!", stream.name)
} }
for filterName := range stream.Filters { for filterName := range stream.Filters {
@ -87,12 +87,12 @@ func (c *Conf) setup() {
// Parse Duration // Parse Duration
retryDuration, err := time.ParseDuration(filter.RetryPeriod) retryDuration, err := time.ParseDuration(filter.RetryPeriod)
if err != nil { if err != nil {
log.Fatalln("Failed to parse time in configuration file:", err) log.Fatalln("FATAL Bad configuration: Failed to parse time:", err)
} }
filter.retryDuration = retryDuration filter.retryDuration = retryDuration
if len(filter.Regex) == 0 { if len(filter.Regex) == 0 {
log.Fatalln("Bad configuration: no regexes configured in '%s.%s'!", stream.name, filter.name) log.Fatalln("FATAL Bad configuration: no regexes configured in '%s.%s'!", stream.name, filter.name)
} }
// Compute Regexes // Compute Regexes
// Look for Patterns inside Regexes // Look for Patterns inside Regexes
@ -108,7 +108,7 @@ func (c *Conf) setup() {
// no op // no op
default: default:
log.Fatalf( log.Fatalf(
"ERROR Can't mix different patterns (%s, %s) in same filter (%s.%s)\n", "Bad configuration: Can't mix different patterns (%s, %s) in same filter (%s.%s)\n",
filter.patternName, patternName, streamName, filterName, filter.patternName, patternName, streamName, filterName,
) )
} }
@ -120,7 +120,7 @@ func (c *Conf) setup() {
} }
if len(filter.Actions) == 0 { if len(filter.Actions) == 0 {
log.Fatalln("Bad configuration: no actions configured in '%s.%s'!", stream.name, filter.name) log.Fatalln("FATAL Bad configuration: no actions configured in '%s.%s'!", stream.name, filter.name)
} }
for actionName := range filter.Actions { for actionName := range filter.Actions {
@ -132,7 +132,7 @@ func (c *Conf) setup() {
if action.After != "" { if action.After != "" {
afterDuration, err := time.ParseDuration(action.After) afterDuration, err := time.ParseDuration(action.After)
if err != nil { if err != nil {
log.Fatalln("Failed to parse time in configuration file:", err) log.Fatalln("FATAL Bad configuration: Failed to parse time:", err)
} }
action.afterDuration = afterDuration action.afterDuration = afterDuration
} }
@ -151,34 +151,34 @@ func (c *Conf) updateFromDB() *gob.Encoder {
file, err := os.Open(DBname) file, err := os.Open(DBname)
if err != nil { if err != nil {
if errors.Is(err, os.ErrNotExist) { if errors.Is(err, os.ErrNotExist) {
log.Printf("WARN: No DB found at %s\n", DBname) log.Printf("WARN No DB found at %s. It's ok if this is the first time reaction is running.\n", DBname)
file, err := os.Create(DBname) file, err := os.Create(DBname)
if err != nil { if err != nil {
log.Fatalln("Failed to create DB:", err) log.Fatalln("FATAL Failed to create DB:", err)
} }
return gob.NewEncoder(file) return gob.NewEncoder(file)
} }
log.Fatalln("Failed to open DB:", err) log.Fatalln("FATAL Failed to open DB:", err)
} }
dec := gob.NewDecoder(file) dec := gob.NewDecoder(file)
newfile, err := os.Create(DBnewName) newfile, err := os.Create(DBnewName)
if err != nil { if err != nil {
log.Fatalln("Failed to create new DB:", err) log.Fatalln("FATAL Failed to create new DB:", err)
} }
enc := gob.NewEncoder(newfile) enc := gob.NewEncoder(newfile)
defer func() { defer func() {
err := file.Close() err := file.Close()
if err != nil { if err != nil {
log.Fatalln("ERRO: Failed to close old DB:", err) log.Fatalln("FATAL Failed to close old DB:", err)
} }
// It should be ok to rename an open file // It should be ok to rename an open file
err = os.Rename(DBnewName, DBname) err = os.Rename(DBnewName, DBname)
if err != nil { if err != nil {
log.Fatalln("ERRO: Failed to replace old DB with new one:", err) log.Fatalln("FATAL Failed to replace old DB with new one:", err)
} }
}() }()
@ -189,18 +189,18 @@ func (c *Conf) updateFromDB() *gob.Encoder {
defer func() { defer func() {
for sf, t := range discardedEntries { for sf, t := range discardedEntries {
if t { if t {
log.Printf("WARN: info discarded from the DB: stream/filter not found: %s.%s\n", sf.s, sf.f) log.Printf("WARN info discarded from the DB: stream/filter not found: %s.%s\n", sf.s, sf.f)
} }
} }
if malformedEntries > 0 { if malformedEntries > 0 {
log.Printf("WARN: %v malformed entries discarded from the DB\n", malformedEntries) log.Printf("WARN %v malformed entries discarded from the DB\n", malformedEntries)
} }
}() }()
encodeOrFatal := func(entry LogEntry) { encodeOrFatal := func(entry LogEntry) {
err = enc.Encode(entry) err = enc.Encode(entry)
if err != nil { if err != nil {
log.Fatalln("ERRO: couldn't write to new DB:", err) log.Fatalln("FATAL Failed to write to new DB:", err)
} }
} }
@ -250,13 +250,13 @@ func parseConf(filename string) (*Conf, *gob.Encoder) {
data, err := os.ReadFile(filename) data, err := os.ReadFile(filename)
if err != nil { if err != nil {
log.Fatalln("Failed to read configuration file:", err) log.Fatalln("FATAL Failed to read configuration file:", err)
} }
var conf Conf var conf Conf
err = yaml.Unmarshal(data, &conf) err = yaml.Unmarshal(data, &conf)
if err != nil { if err != nil {
log.Fatalln("Failed to parse configuration file:", err) log.Fatalln("FATAL Failed to parse configuration file:", err)
} }
conf.setup() conf.setup()

View File

@ -97,7 +97,7 @@ func (a *Action) exec(match string, advance time.Duration) {
cmd := exec.Command(computedCommand[0], computedCommand[1:]...) cmd := exec.Command(computedCommand[0], computedCommand[1:]...)
if ret := cmd.Run(); ret != nil { if ret := cmd.Run(); ret != nil {
log.Printf("ERR %s.%s.%s: run %s, code %s\n", a.filter.stream.name, a.filter.name, a.name, computedCommand, ret) log.Printf("ERROR %s.%s.%s: run %s, code %s\n", a.filter.stream.name, a.filter.name, a.name, computedCommand, ret)
} }
} }
@ -207,7 +207,7 @@ func Main() {
for { for {
select { select {
case finishedStream := <-endSignals: case finishedStream := <-endSignals:
log.Printf("ERR %s stream finished", finishedStream.name) log.Printf("ERROR %s stream finished", finishedStream.name)
noStreamsInExecution-- noStreamsInExecution--
if noStreamsInExecution == 0 { if noStreamsInExecution == 0 {
quit() quit()