Add better loggers.

This commit is contained in:
David Newhall II 2019-06-19 01:29:03 -07:00
parent f79fdb86ac
commit b8c0ba3c47
2 changed files with 18 additions and 8 deletions

View File

@ -1,6 +1,7 @@
package unifipoller
import (
"fmt"
"log"
"strings"
)
@ -21,7 +22,7 @@ func (u *UnifiPoller) LogErrors(errs []error, prefix string) {
for _, err := range errs {
if err != nil {
u.errorCount++
log.Printf("[ERROR] (%v/%v) %v: %v", u.errorCount, u.MaxErrors, prefix, err.Error())
_ = log.Output(2, fmt.Sprintf("[ERROR] (%v/%v) %v: %v", u.errorCount, u.MaxErrors, prefix, err))
}
}
}
@ -39,6 +40,18 @@ func StringInSlice(str string, slc []string) bool {
// Logf prints a log entry if quiet is false.
func (u *UnifiPoller) Logf(m string, v ...interface{}) {
if !u.Quiet {
log.Printf("[INFO] "+m, v...)
_ = log.Output(2, fmt.Sprintf("[INFO] "+m, v...))
}
}
// LogDebugf prints a debug log entry if debug is true and quite is false
func (u *UnifiPoller) LogDebugf(m string, v ...interface{}) {
if u.Debug && !u.Quiet {
_ = log.Output(2, fmt.Sprintf("[DEBUG] "+m, v...))
}
}
// LogErrorf prints an error log entry.
func (u *UnifiPoller) LogErrorf(m string, v ...interface{}) {
_ = log.Output(2, fmt.Sprintf("[ERROR] "+m, v...))
}

View File

@ -61,7 +61,7 @@ func (u *UnifiPoller) Run() (err error) {
}
if log.SetFlags(0); u.Debug {
log.SetFlags(log.Lshortfile | log.Lmicroseconds | log.Ldate)
log.Println("[DEBUG] Debug Logging Enabled")
u.LogDebugf("Debug Logging Enabled")
}
log.Printf("[INFO] Unifi-Poller v%v Starting Up! PID: %d", Version, os.Getpid())
@ -95,11 +95,8 @@ func (u *UnifiPoller) GetUnifi() (err error) {
if err != nil {
return errors.Wrap(err, "unifi controller")
}
u.Unifi.ErrorLog = log.Printf // Log all errors.
// Doing it this way allows debug error logs (line numbers, etc)
if u.Debug && !u.Quiet {
u.Unifi.DebugLog = log.Printf // Log debug messages.
}
u.Unifi.ErrorLog = u.LogErrorf // Log all errors.
u.Unifi.DebugLog = u.LogDebugf // Log debug messages.
v, err := u.GetServer()
if err != nil {
v.ServerVersion = "unknown"