Skip to content

Commit

Permalink
fix: Log Delegation (#48)
Browse files Browse the repository at this point in the history
* fix: log setter overwrite

Signed-off-by: Martin Buchleitner <[email protected]>

* fix: log setter overwrite

Signed-off-by: Martin Buchleitner <[email protected]>

* fix: log setter overwrite

Signed-off-by: Martin Buchleitner <[email protected]>

* fix: log setter overwrite

Signed-off-by: Martin Buchleitner <[email protected]>

* delgation function trial

Signed-off-by: Martin Buchleitner <[email protected]>

* delgation function - correct message info

Signed-off-by: Martin Buchleitner <[email protected]>

---------

Signed-off-by: Martin Buchleitner <[email protected]>
  • Loading branch information
mabunixda authored Sep 4, 2024
1 parent f82b7ed commit d7b720b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
20 changes: 20 additions & 0 deletions logrus_hook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package wattpilot

import (
log "github.com/sirupsen/logrus"
)

type CallHook struct {
Call func(string, string)
LogLevels []log.Level
}

func (hook *CallHook) Fire(entry *log.Entry) error {
hook.Call(entry.Level.String(), entry.Message)
return nil
}

// Levels define on which log levels this hook would trigger
func (hook *CallHook) Levels() []log.Level {
return hook.LogLevels
}
7 changes: 3 additions & 4 deletions wattpilot.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"encoding/json"
"errors"
"fmt"
"log"
"os"
"os/signal"
"strconv"
Expand All @@ -20,6 +19,7 @@ import (
"time"

"github.com/sirupsen/logrus"

"golang.org/x/crypto/pbkdf2"
"nhooyr.io/websocket"
)
Expand Down Expand Up @@ -116,9 +116,8 @@ func New(host string, password string) *Wattpilot {
return w

}
func (w *Wattpilot) SetLogger(logger *log.Logger) {
w.logger.Out = logger.Writer()
// w.logger.SetOutput(logger)
func (w *Wattpilot) SetLogger(delegate func(string, string)) {
w.logger.AddHook(&CallHook{Call: delegate, LogLevels: logrus.AllLevels})
}

func (w *Wattpilot) SetLogLevel(level logrus.Level) {
Expand Down

0 comments on commit d7b720b

Please sign in to comment.