Skip to content

Commit

Permalink
replace github.com/disgoorg/log with log/slog
Browse files Browse the repository at this point in the history
  • Loading branch information
topi314 committed Nov 19, 2023
1 parent c572e08 commit e48f7d6
Show file tree
Hide file tree
Showing 13 changed files with 113 additions and 122 deletions.
9 changes: 4 additions & 5 deletions _examples/disgo/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ package main

import (
"context"
"log/slog"

"github.com/disgoorg/disgo/bot"
"github.com/disgoorg/disgo/discord"
"github.com/disgoorg/disgo/events"
"github.com/disgoorg/log"
"github.com/disgoorg/snowflake/v2"

"github.com/disgoorg/disgolink/v3/disgolink"
"github.com/disgoorg/snowflake/v2"
)

func newBot() *Bot {
Expand All @@ -32,11 +31,11 @@ func (b *Bot) onApplicationCommand(event *events.ApplicationCommandInteractionCr

handler, ok := b.Handlers[data.CommandName()]
if !ok {
log.Info("unknown command: ", data.CommandName())
slog.Info("unknown command", slog.String("command", data.CommandName()))
return
}
if err := handler(event, data); err != nil {
log.Error("error handling command: ", err)
slog.Error("error handling command", slog.Any("err", err))
}
}

Expand Down
5 changes: 3 additions & 2 deletions _examples/disgo/commands.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package main

import (
"log/slog"

"github.com/disgoorg/disgo/bot"
"github.com/disgoorg/disgo/discord"
"github.com/disgoorg/disgolink/v3/lavalink"
"github.com/disgoorg/json"
"github.com/disgoorg/log"
)

var commands = []discord.ApplicationCommandCreate{
Expand Down Expand Up @@ -152,6 +153,6 @@ var commands = []discord.ApplicationCommandCreate{

func registerCommands(client bot.Client) {
if _, err := client.Rest().SetGuildCommands(client.ApplicationID(), GuildId, commands); err != nil {
log.Warn(err)
slog.Error("error while registering commands", slog.Any("err", err))
}
}
14 changes: 7 additions & 7 deletions _examples/disgo/go.mod
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
module github.com/disgoorg/disgolink/v3/_examples/disgo

go 1.18
go 1.21

require (
github.com/disgoorg/disgo v0.16.7
github.com/disgoorg/disgolink/v3 v3.0.0-20230717123709-208f1c210164
github.com/disgoorg/disgo v0.17.0
github.com/disgoorg/disgolink/v3 v3.0.0-20230810103102-c572e085fba1
github.com/disgoorg/json v1.1.0
github.com/disgoorg/log v1.2.1
github.com/disgoorg/snowflake/v2 v2.0.1
)

require (
github.com/gorilla/websocket v1.5.0 // indirect
github.com/gorilla/websocket v1.5.1 // indirect
github.com/sasha-s/go-csync v0.0.0-20210812194225-61421b77c44b // indirect
golang.org/x/crypto v0.10.0 // indirect
golang.org/x/exp v0.0.0-20230626212559-97b1e661b5df // indirect
golang.org/x/sys v0.10.0 // indirect
golang.org/x/crypto v0.15.0 // indirect
golang.org/x/net v0.18.0 // indirect
golang.org/x/sys v0.14.0 // indirect
)
30 changes: 17 additions & 13 deletions _examples/disgo/go.sum
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/disgoorg/disgo v0.16.7 h1:Y4cI+DpLXCMbHJcjIFs5dspKfn4Ous4lN/et2vKgcb8=
github.com/disgoorg/disgo v0.16.7/go.mod h1:wo61ZLPn6bxHVdUODjyZ3fZTnCT7giD3uknsDUwMGn8=
github.com/disgoorg/disgolink/v3 v3.0.0-20230717123709-208f1c210164 h1:9d8fAFJcpmMYz6Cp9szVKbbiWtSikBJ+1QNB1j1ZvAk=
github.com/disgoorg/disgolink/v3 v3.0.0-20230717123709-208f1c210164/go.mod h1:iaNDNaZTwgceZ9yFD/qwYN7eBl++riQte2VFascmPTY=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/disgoorg/disgo v0.17.0 h1:/LcgXgPDhzHt3GkQ4cpjmIJBim1/VYfS31VhGYif3Ms=
github.com/disgoorg/disgo v0.17.0/go.mod h1:AE2J/8oLR2PtYfqcARsk1mgBxQ5z3Z1OD6Lc2SA0gak=
github.com/disgoorg/disgolink/v3 v3.0.0-20230810103102-c572e085fba1 h1:vjQ80/lj2dAiRxE4Y+F1xOmDKs6Xgdt6Ua73IxTykxY=
github.com/disgoorg/disgolink/v3 v3.0.0-20230810103102-c572e085fba1/go.mod h1:iaNDNaZTwgceZ9yFD/qwYN7eBl++riQte2VFascmPTY=
github.com/disgoorg/json v1.1.0 h1:7xigHvomlVA9PQw9bMGO02PHGJJPqvX5AnwlYg/Tnys=
github.com/disgoorg/json v1.1.0/go.mod h1:BHDwdde0rpQFDVsRLKhma6Y7fTbQKub/zdGO5O9NqqA=
github.com/disgoorg/log v1.2.1 h1:kZYAWkUBcGy4LbZcgYtgYu49xNVLy+xG5Uq3yz5VVQs=
github.com/disgoorg/log v1.2.1/go.mod h1:hhQWYTFTnIGzAuFPZyXJEi11IBm9wq+/TVZt/FEwX0o=
github.com/disgoorg/snowflake/v2 v2.0.1 h1:CuUxGLwggUxEswZOmZ+mZ5i0xSumQdXW9tXW7uGqe+0=
github.com/disgoorg/snowflake/v2 v2.0.1/go.mod h1:SPU9c2CNn5DSyb86QcKtdZgix9osEtKrHLW4rMhfLCs=
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/gorilla/websocket v1.5.1 h1:gmztn0JnHVt9JZquRuzLw3g4wouNVzKL15iLr/zn/QY=
github.com/gorilla/websocket v1.5.1/go.mod h1:x3kM2JMyaluk02fnUJpQuwD2dCS5NDG2ZHL0uE0tcaY=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sasha-s/go-csync v0.0.0-20210812194225-61421b77c44b h1:qYTY2tN72LhgDj2rtWG+LI6TXFl2ygFQQ4YezfVaGQE=
github.com/sasha-s/go-csync v0.0.0-20210812194225-61421b77c44b/go.mod h1:/pA7k3zsXKdjjAiUhB5CjuKib9KJGCaLvZwtxGC8U0s=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
golang.org/x/crypto v0.10.0 h1:LKqV2xt9+kDzSTfOhx4FrkEBcMrAgHSYgzywV9zcGmM=
golang.org/x/crypto v0.10.0/go.mod h1:o4eNf7Ede1fv+hwOwZsTHl9EsPFO6q6ZvYR8vYfY45I=
golang.org/x/exp v0.0.0-20230626212559-97b1e661b5df h1:UA2aFVmmsIlefxMk29Dp2juaUSth8Pyn3Tq5Y5mJGME=
golang.org/x/exp v0.0.0-20230626212559-97b1e661b5df/go.mod h1:FXUEEKJgO7OQYeo8N01OfiKP8RXMtf6e8aTskBGqWdc=
golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA=
golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
golang.org/x/crypto v0.15.0 h1:frVn1TEaCEaZcn3Tmd7Y2b5KKPaZ+I32Q2OA3kYp5TA=
golang.org/x/crypto v0.15.0/go.mod h1:4ChreQoLWfG3xLDer1WdlH5NdlQ3+mwnQq1YTKY+72g=
golang.org/x/net v0.18.0 h1:mIYleuAkSbHh0tCv7RvjL3F6ZVbLjq4+R7zbOn3Kokg=
golang.org/x/net v0.18.0/go.mod h1:/czyP5RqHAH4odGYxBJ1qz0+CE5WZ+2j1YgoEo8F2jQ=
golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q=
golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
26 changes: 13 additions & 13 deletions _examples/disgo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"log/slog"
"os"
"os/signal"
"regexp"
Expand All @@ -16,7 +17,6 @@ import (
"github.com/disgoorg/disgo/events"
"github.com/disgoorg/disgo/gateway"
"github.com/disgoorg/disgolink/v3/disgolink"
"github.com/disgoorg/log"
"github.com/disgoorg/snowflake/v2"
)

Expand All @@ -34,11 +34,9 @@ var (
)

func main() {
log.SetFlags(log.LstdFlags | log.Lshortfile)
log.SetLevel(log.LevelInfo)
log.Info("starting disgo example...")
log.Info("disgo version: ", disgo.Version)
log.Info("disgolink version: ", disgolink.Version)
slog.Info("starting disgo example...")
slog.Info("disgo version", slog.String("version", disgo.Version))
slog.Info("disgolink version: ", slog.String("version", disgolink.Version))

b := newBot()

Expand All @@ -54,7 +52,8 @@ func main() {
bot.WithEventListenerFunc(b.onVoiceServerUpdate),
)
if err != nil {
log.Fatal(err)
slog.Error("error while building disgo client", slog.Any("err", err))
os.Exit(1)
}
b.Client = client

Expand Down Expand Up @@ -89,7 +88,8 @@ func main() {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err = client.OpenGateway(ctx); err != nil {
log.Fatal(err)
slog.Error("failed to open gateway", slog.Any("err", err))
os.Exit(1)
}
defer client.Close(context.TODO())

Expand All @@ -100,16 +100,16 @@ func main() {
Secure: NodeSecure,
})
if err != nil {
log.Fatal(err)
slog.Error("failed to add node", slog.Any("err", err))
os.Exit(1)
}
version, err := node.Version(ctx)
if err != nil {
log.Fatal(err)
slog.Error("failed to get node version", slog.Any("err", err))
os.Exit(1)
}
log.Infof("node version: %s", version)
log.Infof("node session id: %s", node.SessionID())

log.Info("DisGo example is now running. Press CTRL-C to exit.")
slog.Info("DisGo example is now running. Press CTRL-C to exit.", slog.String("node_version", version), slog.String("node_session_id", node.SessionID()))
s := make(chan os.Signal, 1)
signal.Notify(s, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
<-s
Expand Down
20 changes: 9 additions & 11 deletions _examples/disgo/player_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,22 @@ package main

import (
"context"
"fmt"

"github.com/disgoorg/log"
"log/slog"

"github.com/disgoorg/disgolink/v3/disgolink"
"github.com/disgoorg/disgolink/v3/lavalink"
)

func (b *Bot) onPlayerPause(player disgolink.Player, event lavalink.PlayerPauseEvent) {
fmt.Printf("onPlayerPause: %v\n", event)
slog.Info("player paused", slog.Any("event", event))
}

func (b *Bot) onPlayerResume(player disgolink.Player, event lavalink.PlayerResumeEvent) {
fmt.Printf("onPlayerResume: %v\n", event)
slog.Info("player resumed", slog.Any("event", event))
}

func (b *Bot) onTrackStart(player disgolink.Player, event lavalink.TrackStartEvent) {
fmt.Printf("onTrackStart: %v\n", event)
slog.Info("track started", slog.Any("event", event))
}

func (b *Bot) onTrackEnd(player disgolink.Player, event lavalink.TrackEndEvent) {
Expand Down Expand Up @@ -48,22 +46,22 @@ func (b *Bot) onTrackEnd(player disgolink.Player, event lavalink.TrackEndEvent)
return
}
if err := player.Update(context.TODO(), lavalink.WithTrack(nextTrack)); err != nil {
log.Error("Failed to play next track: ", err)
slog.Error("Failed to play next track", slog.Any("err", err))
}
}

func (b *Bot) onTrackException(player disgolink.Player, event lavalink.TrackExceptionEvent) {
fmt.Printf("onTrackException: %v\n", event)
slog.Info("track exception", slog.Any("event", event))
}

func (b *Bot) onTrackStuck(player disgolink.Player, event lavalink.TrackStuckEvent) {
fmt.Printf("onTrackStuck: %v\n", event)
slog.Info("track stuck", slog.Any("event", event))
}

func (b *Bot) onWebSocketClosed(player disgolink.Player, event lavalink.WebSocketClosedEvent) {
fmt.Printf("onWebSocketClosed: %v\n", event)
slog.Info("websocket closed", slog.Any("event", event))
}

func (b *Bot) onUnknownEvent(p disgolink.Player, e lavalink.UnknownEvent) {
fmt.Printf("onUnknownEvent event: %s, data: %s\n", e.Type_, string(e.Data))
slog.Info("unknown event", slog.Any("event", e.Type()), slog.String("data", string(e.Data)))
}
39 changes: 16 additions & 23 deletions disgolink/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,16 @@ package disgolink

import (
"context"
"log/slog"

Check failure on line 5 in disgolink/client.go

View workflow job for this annotation

GitHub Actions / gotest

package log/slog is not in GOROOT (/opt/hostedtoolcache/go/1.18.10/x64/src/log/slog)

Check failure on line 5 in disgolink/client.go

View workflow job for this annotation

GitHub Actions / gobuild

package log/slog is not in GOROOT (/opt/hostedtoolcache/go/1.18.10/x64/src/log/slog)
"net/http"
"runtime/debug"
"sync"
"time"

"github.com/disgoorg/disgolink/v3/lavalink"
"github.com/disgoorg/log"
"github.com/disgoorg/snowflake/v2"
)

type Client interface {
Logger() log.Logger

AddNode(ctx context.Context, config NodeConfig) (Node, error)
ForNodes(nodeFunc func(node Node))
Node(name string) Node
Expand Down Expand Up @@ -43,30 +40,25 @@ type Client interface {
}

func New(userID snowflake.ID, opts ...ConfigOpt) Client {
config := DefaultConfig()
config.Apply(opts)
cfg := DefaultConfig()
cfg.Apply(opts)
cfg.Logger = cfg.Logger.With(slog.String("name", "disgolink_client"))

if config.Logger == nil {
config.Logger = log.Default()
}
if config.HTTPClient == nil {
config.HTTPClient = &http.Client{Timeout: 20 * time.Second}
}
return &clientImpl{
logger: config.Logger,
httpClient: config.HTTPClient,
logger: cfg.Logger,
httpClient: cfg.HTTPClient,
userID: userID,
nodes: map[string]Node{},
players: map[snowflake.ID]Player{},
listeners: config.Listeners,
plugins: config.Plugins,
listeners: cfg.Listeners,
plugins: cfg.Plugins,
}
}

var _ Client = (*clientImpl)(nil)

type clientImpl struct {
logger log.Logger
logger *slog.Logger
httpClient *http.Client
userID snowflake.ID

Expand All @@ -83,17 +75,18 @@ type clientImpl struct {
plugins []Plugin
}

func (c *clientImpl) Logger() log.Logger {
return c.logger
}

func (c *clientImpl) AddNode(ctx context.Context, config NodeConfig) (Node, error) {
node := &nodeImpl{
logger: c.logger.With(slog.String("name", "disgolink_node"), slog.String("node_name", config.Name)),
config: config,
lavalink: c,
status: StatusDisconnected,
}
node.rest = newRestClientImpl(node, c.httpClient)
node.rest = &restClientImpl{
logger: c.logger.With(slog.String("name", "disgolink_rest_client"), slog.String("node_name", config.Name)),
node: node,
httpClient: c.httpClient,
}
if err := node.Open(ctx); err != nil {
return nil, err
}
Expand Down Expand Up @@ -186,7 +179,7 @@ func (c *clientImpl) EmitEvent(player Player, event lavalink.Message) {

defer func() {
if r := recover(); r != nil {
c.Logger().Errorf("recovered from panic in event listener: %#v\nstack: %s", r, string(debug.Stack()))
c.logger.Error("recovered from panic in event listener", slog.Any("r", r), slog.String("stack", string(debug.Stack())))
return
}
}()
Expand Down
8 changes: 4 additions & 4 deletions disgolink/client_config.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
package disgolink

import (
"log/slog"
"net/http"
"time"

"github.com/disgoorg/disgolink/v3/lavalink"
"github.com/disgoorg/log"
)

func DefaultConfig() *Config {
return &Config{
Logger: log.Default(),
Logger: slog.Default(),
HTTPClient: &http.Client{Timeout: 10 * time.Second},
}
}

type Config struct {
Logger log.Logger
Logger *slog.Logger
HTTPClient *http.Client
Listeners []EventListener
Plugins []Plugin
Expand All @@ -31,7 +31,7 @@ func (c *Config) Apply(opts []ConfigOpt) {
}

// WithLogger lets you inject your own logger implementing log.Logger
func WithLogger(logger log.Logger) ConfigOpt {
func WithLogger(logger *slog.Logger) ConfigOpt {
return func(config *Config) {
config.Logger = logger
}
Expand Down
Loading

0 comments on commit e48f7d6

Please sign in to comment.