diff --git a/src/logging/conv.go b/src/logging/conv.go new file mode 100644 index 0000000..f9dae5a --- /dev/null +++ b/src/logging/conv.go @@ -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{} +} diff --git a/src/logging/func.go b/src/logging/func.go deleted file mode 100644 index 823df3c..0000000 --- a/src/logging/func.go +++ /dev/null @@ -1,48 +0,0 @@ -package logging - -import ( - "github.com/sirupsen/logrus" -) - -func (lg Logging) Debug(msg string, fields interface{}) { - switch val := fields.(type) { - case logrus.Fields: - lg.Logrus.WithFields(val).Debug(msg) - default: - lg.Logrus.Debug(msg) - } -} - -func (lg Logging) Info(msg string, fields interface{}) { - switch val := fields.(type) { - case logrus.Fields: - lg.Logrus.WithFields(val).Info(msg) - default: - lg.Logrus.Info(msg) - } -} - -func (lg Logging) Error(msg interface{}, fields interface{}) { - var msgStr string - switch val := msg.(type) { - case error: - msgStr = val.Error() - default: - msgStr = val.(string) - } - switch val := fields.(type) { - case logrus.Fields: - lg.Logrus.WithFields(val).Error(msgStr) - default: - lg.Logrus.Error(msgStr) - } -} - -func (lg Logging) Fatal(msg string, fields interface{}) { - switch val := fields.(type) { - case logrus.Fields: - lg.Logrus.WithFields(val).Fatal(msg) - default: - lg.Logrus.Fatal(msg) - } -} diff --git a/src/logging/init.go b/src/logging/init.go index 1464c38..e6b572c 100644 --- a/src/logging/init.go +++ b/src/logging/init.go @@ -2,6 +2,7 @@ package logging import ( "os" + "strings" "github.com/sirupsen/logrus" ) @@ -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") diff --git a/src/logging/log.go b/src/logging/log.go new file mode 100644 index 0000000..ca1d24b --- /dev/null +++ b/src/logging/log.go @@ -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) +} diff --git a/src/logging/logif.go b/src/logging/logif.go new file mode 100644 index 0000000..7d18960 --- /dev/null +++ b/src/logging/logif.go @@ -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) + } + } +} diff --git a/src/main.go b/src/main.go index 0487afb..bb229ea 100644 --- a/src/main.go +++ b/src/main.go @@ -7,8 +7,6 @@ import ( "fmt" "regexp" "time" - - "github.com/sirupsen/logrus" ) func main() { @@ -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, diff --git a/src/watcher/init.go b/src/watcher/init.go index 80f7597..ec9f8ef 100644 --- a/src/watcher/init.go +++ b/src/watcher/init.go @@ -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 { @@ -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) diff --git a/src/watcher/template.go b/src/watcher/template.go index b23e4ae..fdade8a 100644 --- a/src/watcher/template.go +++ b/src/watcher/template.go @@ -2,6 +2,7 @@ package watcher import ( "bytes" + "eagle-eye/src/logging" "fmt" "sort" "text/template" @@ -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() } diff --git a/src/watcher/watch.go b/src/watcher/watch.go index bbb2de3..2068ab6 100644 --- a/src/watcher/watch.go +++ b/src/watcher/watch.go @@ -1,11 +1,11 @@ package watcher import ( + "eagle-eye/src/logging" "fmt" "time" "github.com/radovskyb/watcher" - "github.com/sirupsen/logrus" ) var ( @@ -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) @@ -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: @@ -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) { @@ -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) @@ -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(), }) }