Skip to content

Commit

Permalink
Migrate from zerolog to slog (#248)
Browse files Browse the repository at this point in the history
* Begin adding new slog calls

* Fixed unit tests

* Add leveler example

* Add debug log level to Redis example

* Change location of server.Close() and add logs to example/hooks

* Begin removing references to zerolog

* Removed final references to zerolog

* Change where server.Close() occurs in main

* Change to 1.21 to remove x dependency

* Add slog

* Update references to 1.21

* Begin change of LogAttrs to standard logging interface

* Change the rest of LogAttrs to default

* Fix bad log

* Update badger.go

Changing "data" to "key" or "id" here might be more appropriate.

* Update badger.go

Changing "data" to "key" or "id" here might be more appropriate.

* Update server.go

Not checking if err is equal to nil

* Update server.go

printing information for ID or error is missing.

* Change references of err.Error() to err in slog

* Remove missed removal of Error() references for logging

---------

Co-authored-by: Derek Duncan <[email protected]>
Co-authored-by: Derek Duncan <[email protected]>
Co-authored-by: JB <[email protected]>
Co-authored-by: werbenhu <[email protected]>
  • Loading branch information
5 people authored Sep 6, 2023
1 parent e784c75 commit 44bac0a
Show file tree
Hide file tree
Showing 100 changed files with 561 additions and 8,569 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.19
go-version: 1.21

- name: Vet
run: go vet ./...
Expand All @@ -28,7 +28,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: '1.19'
go-version: '1.21'
- name: Check out code
uses: actions/checkout@v3
- name: Install dependencies
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.19.0-alpine3.15 AS builder
FROM golang:1.21.0-alpine3.18 AS builder

RUN apk update
RUN apk add git
Expand Down
3 changes: 2 additions & 1 deletion clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,8 @@ func (cl *Client) WriteLoop() {
select {
case pk := <-cl.State.outbound:
if err := cl.WritePacket(*pk); err != nil {
cl.ops.log.Debug().Err(err).Str("client", cl.ID).Interface("packet", pk).Msg("failed publishing packet")
// TODO : Figure out what to do with error
cl.ops.log.Debug("failed publishing packet", "error", err, "client", cl.ID, "packet", pk)
}
atomic.AddInt32(&cl.State.outboundQty, -1)
case <-cl.State.open.Done():
Expand Down
2 changes: 1 addition & 1 deletion clients_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func newTestClient() (cl *Client, r net.Conn, w net.Conn) {
cl = newClient(w, &ops{
info: new(system.Info),
hooks: new(Hooks),
log: &logger,
log: logger,
options: &Options{
Capabilities: &Capabilities{
ReceiveMaximum: 10,
Expand Down
7 changes: 4 additions & 3 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"os/signal"
"syscall"

"github.com/mochi-mqtt/server/v2"
mqtt "github.com/mochi-mqtt/server/v2"
"github.com/mochi-mqtt/server/v2/hooks/auth"
"github.com/mochi-mqtt/server/v2/listeners"
)
Expand Down Expand Up @@ -59,7 +59,8 @@ func main() {
}()

<-done
server.Log.Warn().Msg("caught signal, stopping...")
server.Log.Warn("caught signal, stopping...")
server.Close()
server.Log.Info().Msg("main.go finished")
server.Log.Info("main.go finished")

}
6 changes: 3 additions & 3 deletions examples/auth/basic/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"os/signal"
"syscall"

"github.com/mochi-mqtt/server/v2"
mqtt "github.com/mochi-mqtt/server/v2"
"github.com/mochi-mqtt/server/v2/hooks/auth"
"github.com/mochi-mqtt/server/v2/listeners"
)
Expand Down Expand Up @@ -77,7 +77,7 @@ func main() {
}()

<-done
server.Log.Warn().Msg("caught signal, stopping...")
server.Log.Warn("caught signal, stopping...")
server.Close()
server.Log.Info().Msg("main.go finished")
server.Log.Info("main.go finished")
}
6 changes: 3 additions & 3 deletions examples/auth/encoded/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"os/signal"
"syscall"

"github.com/mochi-mqtt/server/v2"
mqtt "github.com/mochi-mqtt/server/v2"
"github.com/mochi-mqtt/server/v2/hooks/auth"
"github.com/mochi-mqtt/server/v2/listeners"
)
Expand Down Expand Up @@ -59,7 +59,7 @@ func main() {
}()

<-done
server.Log.Warn().Msg("caught signal, stopping...")
server.Log.Warn("caught signal, stopping...")
server.Close()
server.Log.Info().Msg("main.go finished")
server.Log.Info("main.go finished")
}
6 changes: 3 additions & 3 deletions examples/benchmark/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"os/signal"
"syscall"

"github.com/mochi-mqtt/server/v2"
mqtt "github.com/mochi-mqtt/server/v2"
"github.com/mochi-mqtt/server/v2/hooks/auth"
"github.com/mochi-mqtt/server/v2/listeners"
)
Expand Down Expand Up @@ -46,7 +46,7 @@ func main() {
}()

<-done
server.Log.Warn().Msg("caught signal, stopping...")
server.Log.Warn("caught signal, stopping...")
server.Close()
server.Log.Info().Msg("main.go finished")
server.Log.Info("main.go finished")
}
16 changes: 10 additions & 6 deletions examples/debug/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ package main

import (
"log"
"log/slog"
"os"
"os/signal"
"syscall"

"github.com/mochi-mqtt/server/v2"
mqtt "github.com/mochi-mqtt/server/v2"
"github.com/mochi-mqtt/server/v2/hooks/auth"
"github.com/mochi-mqtt/server/v2/hooks/debug"
"github.com/mochi-mqtt/server/v2/listeners"
"github.com/rs/zerolog"
)

func main() {
Expand All @@ -27,8 +27,12 @@ func main() {
}()

server := mqtt.New(nil)
l := server.Log.Level(zerolog.DebugLevel)
server.Log = &l

level := new(slog.LevelVar)
server.Log = slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
Level: level,
}))
level.Set(slog.LevelDebug)

err := server.AddHook(new(debug.Hook), &debug.Options{
// ShowPacketData: true,
Expand Down Expand Up @@ -56,7 +60,7 @@ func main() {
}()

<-done
server.Log.Warn().Msg("caught signal, stopping...")
server.Log.Warn("caught signal, stopping...")
server.Close()
server.Log.Info().Msg("main.go finished")
server.Log.Info("main.go finished")
}
36 changes: 21 additions & 15 deletions examples/hooks/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ package main

import (
"bytes"
"fmt"
"log"
"os"
"os/signal"
"syscall"
"time"

"github.com/mochi-mqtt/server/v2"
mqtt "github.com/mochi-mqtt/server/v2"
"github.com/mochi-mqtt/server/v2/hooks/auth"
"github.com/mochi-mqtt/server/v2/listeners"
"github.com/mochi-mqtt/server/v2/packets"
Expand Down Expand Up @@ -62,9 +63,9 @@ func main() {
Payload: []byte("injected scheduled message"),
})
if err != nil {
server.Log.Error().Err(err).Msg("server.InjectPacket")
server.Log.Error("server.InjectPacket", "error", err)
}
server.Log.Info().Msgf("main.go injected packet to direct/publish")
server.Log.Info("main.go injected packet to direct/publish")
}
}()

Expand All @@ -74,16 +75,16 @@ func main() {
for range time.Tick(time.Second * 5) {
err := server.Publish("direct/publish", []byte("packet scheduled message"), false, 0)
if err != nil {
server.Log.Error().Err(err).Msg("server.Publish")
server.Log.Error("server.Publish", "error", err)
}
server.Log.Info().Msgf("main.go issued direct message to direct/publish")
server.Log.Info("main.go issued direct message to direct/publish")
}
}()

<-done
server.Log.Warn().Msg("caught signal, stopping...")
server.Log.Warn("caught signal, stopping...")
server.Close()
server.Log.Info().Msg("main.go finished")
server.Log.Info("main.go finished")
}

type ExampleHook struct {
Expand All @@ -106,39 +107,44 @@ func (h *ExampleHook) Provides(b byte) bool {
}

func (h *ExampleHook) Init(config any) error {
h.Log.Info().Msg("initialised")
h.Log.Info("initialised")
return nil
}

func (h *ExampleHook) OnConnect(cl *mqtt.Client, pk packets.Packet) error {
h.Log.Info().Str("client", cl.ID).Msgf("client connected")
h.Log.Info("client connected", "client", cl.ID)
return nil
}

func (h *ExampleHook) OnDisconnect(cl *mqtt.Client, err error, expire bool) {
h.Log.Info().Str("client", cl.ID).Bool("expire", expire).Err(err).Msg("client disconnected")
if err != nil {
h.Log.Info("client disconnected", "client", cl.ID, "expire", expire, "error", err)
} else {
h.Log.Info("client disconnected", "client", cl.ID, "expire", expire)
}

}

func (h *ExampleHook) OnSubscribed(cl *mqtt.Client, pk packets.Packet, reasonCodes []byte) {
h.Log.Info().Str("client", cl.ID).Interface("filters", pk.Filters).Msgf("subscribed qos=%v", reasonCodes)
h.Log.Info(fmt.Sprintf("subscribed qos=%v", reasonCodes), "client", cl.ID, "filters", pk.Filters)
}

func (h *ExampleHook) OnUnsubscribed(cl *mqtt.Client, pk packets.Packet) {
h.Log.Info().Str("client", cl.ID).Interface("filters", pk.Filters).Msg("unsubscribed")
h.Log.Info("unsubscribed", "client", cl.ID, "filters", pk.Filters)
}

func (h *ExampleHook) OnPublish(cl *mqtt.Client, pk packets.Packet) (packets.Packet, error) {
h.Log.Info().Str("client", cl.ID).Str("payload", string(pk.Payload)).Msg("received from client")
h.Log.Info("received from client", "client", cl.ID, "payload", string(pk.Payload))

pkx := pk
if string(pk.Payload) == "hello" {
pkx.Payload = []byte("hello world")
h.Log.Info().Str("client", cl.ID).Str("payload", string(pkx.Payload)).Msg("received modified packet from client")
h.Log.Info("received modified packet from client", "client", cl.ID, "payload", string(pkx.Payload))
}

return pkx, nil
}

func (h *ExampleHook) OnPublished(cl *mqtt.Client, pk packets.Packet) {
h.Log.Info().Str("client", cl.ID).Str("payload", string(pk.Payload)).Msg("published to client")
h.Log.Info("published to client", "client", cl.ID, "payload", string(pk.Payload))
}
6 changes: 3 additions & 3 deletions examples/paho.testing/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"os/signal"
"syscall"

"github.com/mochi-mqtt/server/v2"
mqtt "github.com/mochi-mqtt/server/v2"
"github.com/mochi-mqtt/server/v2/listeners"
"github.com/mochi-mqtt/server/v2/packets"
)
Expand Down Expand Up @@ -45,9 +45,9 @@ func main() {
}()

<-done
server.Log.Warn().Msg("caught signal, stopping...")
server.Log.Warn("caught signal, stopping...")
server.Close()
server.Log.Info().Msg("main.go finished")
server.Log.Info("main.go finished")
}

type pahoAuthHook struct {
Expand Down
7 changes: 3 additions & 4 deletions examples/persistence/badger/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"os/signal"
"syscall"

"github.com/mochi-mqtt/server/v2"
mqtt "github.com/mochi-mqtt/server/v2"
"github.com/mochi-mqtt/server/v2/hooks/auth"
"github.com/mochi-mqtt/server/v2/hooks/storage/badger"
"github.com/mochi-mqtt/server/v2/listeners"
Expand Down Expand Up @@ -52,8 +52,7 @@ func main() {
}()

<-done
server.Log.Warn().Msg("caught signal, stopping...")
server.Log.Warn("caught signal, stopping...")
server.Close()
server.Log.Info().Msg("main.go finished")

server.Log.Info("main.go finished")
}
6 changes: 3 additions & 3 deletions examples/persistence/bolt/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"syscall"
"time"

"github.com/mochi-mqtt/server/v2"
mqtt "github.com/mochi-mqtt/server/v2"
"github.com/mochi-mqtt/server/v2/hooks/auth"
"github.com/mochi-mqtt/server/v2/hooks/storage/bolt"
"github.com/mochi-mqtt/server/v2/listeners"
Expand Down Expand Up @@ -54,7 +54,7 @@ func main() {
}()

<-done
server.Log.Warn().Msg("caught signal, stopping...")
server.Log.Warn("caught signal, stopping...")
server.Close()
server.Log.Info().Msg("main.go finished")
server.Log.Info("main.go finished")
}
17 changes: 10 additions & 7 deletions examples/persistence/redis/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ package main

import (
"log"
"log/slog"
"os"
"os/signal"
"syscall"

"github.com/mochi-mqtt/server/v2"
mqtt "github.com/mochi-mqtt/server/v2"
"github.com/mochi-mqtt/server/v2/hooks/auth"
"github.com/mochi-mqtt/server/v2/hooks/storage/redis"
"github.com/mochi-mqtt/server/v2/listeners"
"github.com/rs/zerolog"

rv8 "github.com/go-redis/redis/v8"
)
Expand All @@ -30,8 +30,12 @@ func main() {

server := mqtt.New(nil)
_ = server.AddHook(new(auth.AllowHook), nil)
l := server.Log.Level(zerolog.DebugLevel)
server.Log = &l

level := new(slog.LevelVar)
server.Log = slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
Level: level,
}))
level.Set(slog.LevelDebug)

err := server.AddHook(new(redis.Hook), &redis.Options{
Options: &rv8.Options{
Expand All @@ -58,8 +62,7 @@ func main() {
}()

<-done
server.Log.Warn().Msg("caught signal, stopping...")
server.Log.Warn("caught signal, stopping...")
server.Close()
server.Log.Info().Msg("main.go finished")

server.Log.Info("main.go finished")
}
6 changes: 3 additions & 3 deletions examples/tcp/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"os/signal"
"syscall"

"github.com/mochi-mqtt/server/v2"
mqtt "github.com/mochi-mqtt/server/v2"
"github.com/mochi-mqtt/server/v2/hooks/auth"
"github.com/mochi-mqtt/server/v2/listeners"
)
Expand Down Expand Up @@ -52,7 +52,7 @@ func main() {
}()

<-done
server.Log.Warn().Msg("caught signal, stopping...")
server.Log.Warn("caught signal, stopping...")
server.Close()
server.Log.Info().Msg("main.go finished")
server.Log.Info("main.go finished")
}
Loading

0 comments on commit 44bac0a

Please sign in to comment.