Skip to content

Commit

Permalink
Add logging level file config (#396)
Browse files Browse the repository at this point in the history
* add logging level to file config

* remove last zerolog references

* add levelvar to unit test

---------

Co-authored-by: Derek Duncan <derekduncan@gmail.com>
Co-authored-by: JB <28275108+mochi-co@users.noreply.github.com>
  • Loading branch information
3 people committed Apr 30, 2024
1 parent d5d9b02 commit b9d2dfb
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 8 deletions.
27 changes: 24 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ package config

import (
"encoding/json"
"log/slog"
"os"

"github.com/mochi-mqtt/server/v2/hooks/auth"
"github.com/mochi-mqtt/server/v2/hooks/debug"
Expand All @@ -21,9 +23,27 @@ import (

// config defines the structure of configuration data to be parsed from a config source.
type config struct {
Options mqtt.Options
Listeners []listeners.Config `yaml:"listeners" json:"listeners"`
HookConfigs HookConfigs `yaml:"hooks" json:"hooks"`
Options mqtt.Options
Listeners []listeners.Config `yaml:"listeners" json:"listeners"`
HookConfigs HookConfigs `yaml:"hooks" json:"hooks"`
LoggingConfig LoggingConfig `yaml:"logging" json:"logging"`
}

type LoggingConfig struct {
Level string
}

func (lc LoggingConfig) ToLogger() *slog.Logger {
var level slog.Level
if err := level.UnmarshalText([]byte(lc.Level)); err != nil {
level = slog.LevelInfo
}

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

// HookConfigs contains configurations to enable individual hooks.
Expand Down Expand Up @@ -149,6 +169,7 @@ func FromBytes(b []byte) (*mqtt.Options, error) {
o = c.Options
o.Hooks = c.HookConfigs.ToHooks()
o.Listeners = c.Listeners
o.Logger = c.LoggingConfig.ToLogger()

return &o, nil
}
6 changes: 5 additions & 1 deletion config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
package config

import (
"log/slog"
"os"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -60,7 +62,6 @@ options:
}
}
`)

parsedOptions = mqtt.Options{
Listeners: []listeners.Config{
{
Expand All @@ -81,6 +82,9 @@ options:
RestoreSysInfoOnRestart: true,
},
},
Logger: slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
Level: new(slog.LevelVar),
})),
}
)

Expand Down
2 changes: 2 additions & 0 deletions examples/config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,5 @@ options:
always_return_response_info: false
restore_sys_info_on_restart: false
no_inherited_properties_on_ack: false
logging:
level: INFO
2 changes: 1 addition & 1 deletion server.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ type Options struct {
// ClientNetReadBufferSize specifies the size of the client *bufio.Reader read buffer.
ClientNetReadBufferSize int `yaml:"client_net_read_buffer_size" json:"client_net_read_buffer_size"`

// Logger specifies a custom configured implementation of zerolog to override
// Logger specifies a custom configured implementation of log/slog to override
// the servers default logger configuration. If you wish to change the log level,
// of the default logger, you can do so by setting:
// server := mqtt.New(nil)
Expand Down
6 changes: 3 additions & 3 deletions server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2258,7 +2258,7 @@ func TestPublishToSubscribersExhaustedSendQuota(t *testing.T) {
require.True(t, subbed)

// coverage: subscriber publish errors are non-returnable
// can we hook into zerolog ?
// can we hook into log/slog ?
_ = r.Close()
pkx := *packets.TPacketData[packets.Publish].Get(packets.TPublishQos1).Packet
pkx.PacketID = 0
Expand All @@ -2279,7 +2279,7 @@ func TestPublishToSubscribersExhaustedPacketIDs(t *testing.T) {
require.True(t, subbed)

// coverage: subscriber publish errors are non-returnable
// can we hook into zerolog ?
// can we hook into log/slog ?
_ = r.Close()
pkx := *packets.TPacketData[packets.Publish].Get(packets.TPublishQos1).Packet
pkx.PacketID = 0
Expand All @@ -2296,7 +2296,7 @@ func TestPublishToSubscribersNoConnection(t *testing.T) {
require.True(t, subbed)

// coverage: subscriber publish errors are non-returnable
// can we hook into zerolog ?
// can we hook into log/slog ?
_ = r.Close()
s.publishToSubscribers(*packets.TPacketData[packets.Publish].Get(packets.TPublishBasic).Packet)
time.Sleep(time.Millisecond)
Expand Down

0 comments on commit b9d2dfb

Please sign in to comment.