Skip to content

Commit

Permalink
Update logging
Browse files Browse the repository at this point in the history
  • Loading branch information
triole committed Jan 4, 2023
1 parent 3966dde commit 141b083
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 83 deletions.
12 changes: 12 additions & 0 deletions src/logging/conv.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package logging

import "github.com/sirupsen/logrus"

type F map[string]interface{}

func (lg Logging) conv(fields F) logrus.Fields {
if fields != nil {
return logrus.Fields(fields)
}
return logrus.Fields{}
}
48 changes: 0 additions & 48 deletions src/logging/func.go

This file was deleted.

22 changes: 11 additions & 11 deletions src/logging/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package logging

import (
"os"
"strings"

"github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -56,24 +57,23 @@ func Init(loglevel, logFile string, nocolours, JSONLog bool) (lg Logging) {
openLogFile, err := os.OpenFile(
logFile, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644,
)
if err != nil {
lg.Fatal(
"Can not open log file",
logrus.Fields{
"logfile": logFile,
"error": err.Error(),
},
)
}
lg.IfErrFatal(
"Can not open log file",
F{
"logfile": logFile,
"error": err.Error(),
},
)

lg.setLevel(loglevel)
lg.Logrus.SetOutput(openLogFile)
}
lg.setLevel(loglevel)

return lg
}

func (lg *Logging) setLevel(level string) {
if val, ok := logLevels[level]; ok {
if val, ok := logLevels[strings.ToLower(level)]; ok {
lg.Logrus.SetLevel(val)
} else {
lg.setLevel("info")
Expand Down
17 changes: 17 additions & 0 deletions src/logging/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package logging

func (lg Logging) Debug(msg string, fields F) {
lg.Logrus.WithFields(lg.conv(fields)).Debug(msg)
}

func (lg Logging) Info(msg string, fields F) {
lg.Logrus.WithFields(lg.conv(fields)).Info(msg)
}

func (lg Logging) Error(msg interface{}, fields F) {
lg.Logrus.WithFields(lg.conv(fields)).Error(msg)
}

func (lg Logging) Fatal(msg string, fields F) {
lg.Logrus.WithFields(lg.conv(fields)).Fatal(msg)
}
17 changes: 17 additions & 0 deletions src/logging/logif.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package logging

func (lg Logging) IfErrFatal(msg string, fields F) {
for key, val := range fields {
if (key == "error" || key == "err") && val != nil {
lg.Fatal(msg, fields)
}
}
}

func (lg Logging) IfErrError(msg string, fields F) {
for key, val := range fields {
if (key == "error" || key == "err") && val != nil {
lg.Error(msg, fields)
}
}
}
4 changes: 1 addition & 3 deletions src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import (
"fmt"
"regexp"
"time"

"github.com/sirupsen/logrus"
)

func main() {
Expand All @@ -35,7 +33,7 @@ func main() {
mode = "just spectate"
}

conf.Logging.Info("Watch folder", logrus.Fields{
conf.Logging.Info("Watch folder", logging.F{
"folder": conf.Folder,
"action": mode,
"log-file": CLI.LogFile,
Expand Down
4 changes: 2 additions & 2 deletions src/watcher/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package watcher

import (
"eagle-eye/src/conf"
"eagle-eye/src/logging"

"github.com/radovskyb/watcher"
"github.com/sirupsen/logrus"
)

type Watcher struct {
Expand All @@ -20,7 +20,7 @@ func Init(conf conf.Conf) (w Watcher) {
w.Watcher.AddFilterHook(watcher.RegexFilterHook(conf.Regex, false))

if w.Conf.RunInitially {
conf.Logging.Info("Run initially", logrus.Fields{
conf.Logging.Info("Run initially", logging.F{
"cmds": conf.Command,
})
w.runCmd(conf.Command, conf.Pause, conf.Verbose)
Expand Down
5 changes: 2 additions & 3 deletions src/watcher/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package watcher

import (
"bytes"
"eagle-eye/src/logging"
"fmt"
"sort"
"text/template"
Expand All @@ -26,9 +27,7 @@ func (w Watcher) execTemplate(tplStr string, varMap map[string]interface{}) stri
)
buf := &bytes.Buffer{}
err := tmpl.Execute(buf, varMap)
if err != nil {
panic(err)
}
w.Conf.Logging.IfErrFatal("Template error", logging.F{"error": err})
return buf.String()
}

Expand Down
30 changes: 14 additions & 16 deletions src/watcher/watch.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package watcher

import (
"eagle-eye/src/logging"
"fmt"
"time"

"github.com/radovskyb/watcher"
"github.com/sirupsen/logrus"
)

var (
Expand All @@ -27,7 +27,7 @@ type tVarMapEntry struct {
}

func (w Watcher) Run() {

var err error
chin := make(EventChan)
go w.ticker(chin)
go w.runChannelWatcher(chin)
Expand All @@ -42,7 +42,7 @@ func (w Watcher) Run() {
}
chin <- event
case err := <-w.Watcher.Error:
w.Conf.Logging.Fatal("An error occured", logrus.Fields{
w.Conf.Logging.Fatal("An error occured", logging.F{
"error": err,
})
case <-w.Watcher.Closed:
Expand All @@ -51,21 +51,19 @@ func (w Watcher) Run() {
}
}()

if err := w.Watcher.AddRecursive(w.Conf.Folder); err != nil {
w.Conf.Logging.Fatal("Unable to add folders to watch list", logrus.Fields{
"error": err,
})
}
err = w.Watcher.AddRecursive(w.Conf.Folder)
w.Conf.Logging.IfErrFatal("Unable to add folders to watch list", logging.F{
"error": err,
})

go func() {
w.Watcher.Wait()
}()

if err := w.Watcher.Start(w.Conf.Interval); err != nil {
w.Conf.Logging.Fatal("Can not start watcher", logrus.Fields{
"error": err,
})
}
err = w.Watcher.Start(w.Conf.Interval)
w.Conf.Logging.IfErrFatal("Can not start watcher", logging.F{
"error": err,
})
}

func (w Watcher) runChannelWatcher(chin EventChan) {
Expand All @@ -90,7 +88,7 @@ func (w Watcher) runChannelWatcher(chin EventChan) {
cmdArr := w.iterTemplate(
w.Conf.Command, w.makeVarMap(ev.Event),
)
w.Conf.Logging.Info("Run", logrus.Fields{
w.Conf.Logging.Info("Run", logging.F{
"cmds": cmdArr,
})
w.runCmd(cmdArr, w.Conf.Pause, w.Conf.Verbose)
Expand Down Expand Up @@ -121,13 +119,13 @@ func (w Watcher) printEvent(event watcher.Event) {
t = "FOLDER"
}
if w.Conf.Spectate {
w.Conf.Logging.Info("Event", logrus.Fields{
w.Conf.Logging.Info("Event", logging.F{
"event": event.Op.String(),
"path": fmt.Sprintf(event.Path),
"type": t,
})
} else {
w.Conf.Logging.Debug("Event", logrus.Fields{
w.Conf.Logging.Debug("Event", logging.F{
"event": event.Op.String(),
})
}
Expand Down

0 comments on commit 141b083

Please sign in to comment.