Skip to content

Commit

Permalink
Fix logging inconsistencies
Browse files Browse the repository at this point in the history
  • Loading branch information
sebm253 committed May 7, 2024
1 parent 791bf45 commit 7bfde5b
Show file tree
Hide file tree
Showing 19 changed files with 46 additions and 42 deletions.
2 changes: 1 addition & 1 deletion _examples/application_commands/http/example.go
Expand Up @@ -85,7 +85,7 @@ func commandListener(event *events.ApplicationCommandInteractionCreate) {
SetEphemeral(data.Bool("ephemeral")).
Build(),
); err != nil {
event.Client().Logger().Error("error on sending response: ", err)
event.Client().Logger().Error("error on sending response", slog.Any("err", err))
}
}
}
2 changes: 1 addition & 1 deletion _examples/application_commands/localization/example.go
Expand Up @@ -99,7 +99,7 @@ func commandListener(event *events.ApplicationCommandInteractionCreate) {
Build(),
)
if err != nil {
event.Client().Logger().Error("error on sending response: ", err)
event.Client().Logger().Error("error on sending response", slog.Any("err", err))
}
}
}
4 changes: 2 additions & 2 deletions _examples/components/example.go
Expand Up @@ -43,13 +43,13 @@ func main() {
}),
)
if err != nil {
slog.Error("error while building bot: ", err)
slog.Error("error while building bot", slog.Any("err", err))
return
}
defer client.Close(context.TODO())

if err = client.OpenGateway(context.TODO()); err != nil {
slog.Error("error while connecting to gateway: ", err)
slog.Error("error while connecting to gateway", slog.Any("err", err))
return
}

Expand Down
4 changes: 2 additions & 2 deletions _examples/ping_pong/example.go
Expand Up @@ -28,14 +28,14 @@ func main() {
bot.WithEventListenerFunc(onMessageCreate),
)
if err != nil {
slog.Error("error while building disgo", slog.String("err", err.Error()))
slog.Error("error while building disgo", slog.Any("err", err))
return
}

defer client.Close(context.TODO())

if err = client.OpenGateway(context.TODO()); err != nil {
slog.Error("errors while connecting to gateway", slog.String("err", err.Error()))
slog.Error("errors while connecting to gateway", slog.Any("err", err))
return
}

Expand Down
2 changes: 1 addition & 1 deletion bot/member_chunking_manager.go
Expand Up @@ -120,7 +120,7 @@ func (m *memberChunkingManagerImpl) HandleChunk(payload gateway.EventGuildMember
request, ok := m.chunkingRequests[payload.Nonce]
m.chunkingRequestsMu.RUnlock()
if !ok {
m.logger.Debug("received unknown member chunk event: ", slog.Any("payload", payload))
m.logger.Debug("received unknown member chunk event", slog.Any("payload", payload))
return
}

Expand Down
20 changes: 10 additions & 10 deletions gateway/gateway_impl.go
Expand Up @@ -102,12 +102,12 @@ func (g *gatewayImpl) open(ctx context.Context) error {
}()
rawBody, bErr := io.ReadAll(rs.Body)
if bErr != nil {
g.config.Logger.Error("error while reading response body", slog.String("err", bErr.Error()))
g.config.Logger.Error("error while reading response body", slog.Any("err", bErr))
}
body = string(rawBody)
}

g.config.Logger.Error("error connecting to the gateway", slog.String("err", err.Error()), slog.String("url", gatewayURL), slog.String("body", body))
g.config.Logger.Error("error connecting to the gateway", slog.Any("err", err), slog.String("url", gatewayURL), slog.String("body", body))
return err
}

Expand Down Expand Up @@ -143,7 +143,7 @@ func (g *gatewayImpl) CloseWithCode(ctx context.Context, code int, message strin
g.config.RateLimiter.Close(ctx)
g.config.Logger.Debug("closing gateway connection", slog.Int("code", code), slog.String("message", message))
if err := g.conn.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(code, message)); err != nil && !errors.Is(err, websocket.ErrCloseSent) {
g.config.Logger.Debug("error writing close code", slog.String("err", err.Error()))
g.config.Logger.Debug("error writing close code", slog.Any("err", err))
}
_ = g.conn.Close()
g.conn = nil
Expand Down Expand Up @@ -219,7 +219,7 @@ func (g *gatewayImpl) reconnectTry(ctx context.Context, try int) error {
if errors.Is(err, discord.ErrGatewayAlreadyConnected) {
return err
}
g.config.Logger.Error("failed to reconnect gateway", slog.String("err", err.Error()))
g.config.Logger.Error("failed to reconnect gateway", slog.Any("err", err))
g.status = StatusDisconnected
return g.reconnectTry(ctx, try+1)
}
Expand All @@ -229,7 +229,7 @@ func (g *gatewayImpl) reconnectTry(ctx context.Context, try int) error {
func (g *gatewayImpl) reconnect() {
err := g.reconnectTry(context.Background(), 0)
if err != nil {
g.config.Logger.Error("failed to reopen gateway", slog.String("err", err.Error()))
g.config.Logger.Error("failed to reopen gateway", slog.Any("err", err))
}
}

Expand Down Expand Up @@ -261,7 +261,7 @@ func (g *gatewayImpl) sendHeartbeat() {
if errors.Is(err, discord.ErrShardNotConnected) || errors.Is(err, syscall.EPIPE) {
return
}
g.config.Logger.Error("failed to send heartbeat", slog.String("err", err.Error()))
g.config.Logger.Error("failed to send heartbeat", slog.Any("err", err))
closeCtx, closeCancel := context.WithTimeout(context.Background(), 5*time.Second)
defer closeCancel()
g.CloseWithCode(closeCtx, websocket.CloseServiceRestart, "heartbeat timeout")
Expand Down Expand Up @@ -292,7 +292,7 @@ func (g *gatewayImpl) identify() {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := g.Send(ctx, OpcodeIdentify, identify); err != nil {
g.config.Logger.Error("error sending Identify command", slog.String("err", err.Error()))
g.config.Logger.Error("error sending Identify command", slog.Any("err", err))
}
g.status = StatusWaitingForReady
}
Expand All @@ -309,7 +309,7 @@ func (g *gatewayImpl) resume() {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := g.Send(ctx, OpcodeResume, resume); err != nil {
g.config.Logger.Error("error sending resume command", slog.String("err", err.Error()))
g.config.Logger.Error("error sending resume command", slog.Any("err", err))
}
}

Expand Down Expand Up @@ -354,7 +354,7 @@ loop:
// we closed the connection ourselves. Don't try to reconnect here
reconnect = false
} else {
g.config.Logger.Debug("failed to read next message from gateway", slog.String("err", err.Error()))
g.config.Logger.Debug("failed to read next message from gateway", slog.Any("err", err))
}

// make sure the connection is properly closed
Expand All @@ -372,7 +372,7 @@ loop:

message, err := g.parseMessage(mt, r)
if err != nil {
g.config.Logger.Error("error while parsing gateway message", slog.String("err", err.Error()))
g.config.Logger.Error("error while parsing gateway message", slog.Any("err", err))
continue
}

Expand Down
2 changes: 1 addition & 1 deletion handler/middleware/go.go
Expand Up @@ -9,7 +9,7 @@ import (

// Go is a middleware that runs the next handler in a goroutine.
var Go = GoErr(func(event *handler.InteractionEvent, err error) {
event.Client().Logger().Error("failed to handle interaction", slog.String("err", err.Error()))
event.Client().Logger().Error("failed to handle interaction", slog.Any("err", err))
})

// GoDefer combines Go and Defer
Expand Down
2 changes: 1 addition & 1 deletion handler/mux.go
Expand Up @@ -11,7 +11,7 @@ import (
)

var defaultErrorHandler ErrorHandler = func(event *InteractionEvent, err error) {
event.Client().Logger().Error("error handling interaction", slog.String("err", err.Error()))
event.Client().Logger().Error("error handling interaction", slog.Any("err", err))
}

// New returns a new Router.
Expand Down
4 changes: 3 additions & 1 deletion handlers/guild_handlers.go
@@ -1,6 +1,8 @@
package handlers

import (
"log/slog"

"github.com/disgoorg/disgo/bot"
"github.com/disgoorg/disgo/discord"
"github.com/disgoorg/disgo/events"
Expand Down Expand Up @@ -80,7 +82,7 @@ func gatewayHandlerGuildCreate(client bot.Client, sequenceNumber int, shardID in
if client.MemberChunkingManager().MemberChunkingFilter()(event.ID) {
go func() {
if _, err := client.MemberChunkingManager().RequestMembersWithQuery(event.ID, "", 0); err != nil {
client.Logger().Error("failed to chunk guild on guild_create. error: ", err)
client.Logger().Error("failed to chunk guild on guild_create", slog.Any("err", err))
}
}()
}
Expand Down
4 changes: 3 additions & 1 deletion handlers/interaction_create_http_handler.go
@@ -1,6 +1,8 @@
package handlers

import (
"log/slog"

"github.com/disgoorg/disgo/bot"
"github.com/disgoorg/disgo/discord"
"github.com/disgoorg/disgo/httpserver"
Expand All @@ -18,7 +20,7 @@ func (h *httpserverHandlerInteractionCreate) HandleHTTPEvent(client bot.Client,
if err := respondFunc(discord.InteractionResponse{
Type: discord.InteractionResponseTypePong,
}); err != nil {
client.Logger().Error("failed to respond to http interaction ping: ", err)
client.Logger().Error("failed to respond to http interaction ping", slog.Any("err", err))
}
return
}
Expand Down
2 changes: 1 addition & 1 deletion httpserver/server.go
Expand Up @@ -121,7 +121,7 @@ func HandleInteraction(publicKey PublicKey, logger *slog.Logger, handleFunc Even

var v EventInteractionCreate
if err := json.NewDecoder(buff).Decode(&v); err != nil {
logger.Error("error while decoding interaction: ", err)
logger.Error("error while decoding interaction", slog.Any("err", err))
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
Expand Down
4 changes: 2 additions & 2 deletions httpserver/server_impl.go
Expand Up @@ -18,7 +18,7 @@ func New(publicKey string, eventHandlerFunc EventHandlerFunc, opts ...ConfigOpt)

hexDecodedKey, err := hex.DecodeString(publicKey)
if err != nil {
config.Logger.Debug("error while decoding hex string", slog.String("err", err.Error()))
config.Logger.Debug("error while decoding hex string", slog.Any("err", err))
}

return &serverImpl{
Expand Down Expand Up @@ -47,7 +47,7 @@ func (s *serverImpl) Start() {
err = s.config.HTTPServer.ListenAndServe()
}
if !errors.Is(err, http.ErrServerClosed) {
s.config.Logger.Error("error while running http server", slog.String("err", err.Error()))
s.config.Logger.Error("error while running http server", slog.Any("err", err))
}
}()
}
Expand Down
2 changes: 1 addition & 1 deletion rest/rest_client.go
Expand Up @@ -152,7 +152,7 @@ func (c *clientImpl) retry(endpoint *CompiledEndpoint, rqBody any, rsBody any, t
case http.StatusOK, http.StatusCreated, http.StatusNoContent:
if rsBody != nil && rs.Body != nil {
if err = json.Unmarshal(rawRsBody, rsBody); err != nil {
c.config.Logger.Error("error unmarshalling response body", slog.String("err", err.Error()), slog.String("endpoint", endpoint.URL), slog.String("code", rs.Status), slog.String("body", string(rawRsBody)))
c.config.Logger.Error("error unmarshalling response body", slog.Any("err", err), slog.String("endpoint", endpoint.URL), slog.String("code", rs.Status), slog.String("body", string(rawRsBody)))
return fmt.Errorf("error unmarshalling response body: %w", err)
}
}
Expand Down
8 changes: 4 additions & 4 deletions sharding/shard_manager_impl.go
Expand Up @@ -72,15 +72,15 @@ func (m *shardManagerImpl) closeHandler(shard gateway.Gateway, err error) {
go func() {
defer wg.Done()
if err := m.config.RateLimiter.WaitBucket(context.TODO(), shardID); err != nil {
m.config.Logger.Error("failed to wait shard bucket", slog.String("err", err.Error()), slog.Int("shard_id", shardID))
m.config.Logger.Error("failed to wait shard bucket", slog.Any("err", err), slog.Int("shard_id", shardID))
return
}
defer m.config.RateLimiter.UnlockBucket(shardID)

newShard := m.config.GatewayCreateFunc(m.token, m.eventHandlerFunc, m.closeHandler, append(m.config.GatewayConfigOpts, gateway.WithShardID(shardID), gateway.WithShardCount(newShardCount))...)
m.shards[shardID] = newShard
if err := newShard.Open(context.TODO()); err != nil {
m.config.Logger.Error("failed to re shard", slog.String("err", err.Error()), slog.Int("shard_id", shardID))
m.config.Logger.Error("failed to re shard", slog.Any("err", err), slog.Int("shard_id", shardID))
}
}()
}
Expand All @@ -104,15 +104,15 @@ func (m *shardManagerImpl) Open(ctx context.Context) {
go func() {
defer wg.Done()
if err := m.config.RateLimiter.WaitBucket(ctx, shardID); err != nil {
m.config.Logger.Error("failed to wait shard bucket", slog.String("err", err.Error()), slog.Int("shard_id", shardID))
m.config.Logger.Error("failed to wait shard bucket", slog.Any("err", err), slog.Int("shard_id", shardID))
return
}
defer m.config.RateLimiter.UnlockBucket(shardID)

shard := m.config.GatewayCreateFunc(m.token, m.eventHandlerFunc, m.closeHandler, append(m.config.GatewayConfigOpts, gateway.WithShardID(shardID), gateway.WithShardCount(m.config.ShardCount))...)
m.shards[shardID] = shard
if err := shard.Open(ctx); err != nil {
m.config.Logger.Error("failed to open shard", slog.String("err", err.Error()), slog.Int("shard_id", shardID))
m.config.Logger.Error("failed to open shard", slog.Any("err", err), slog.Int("shard_id", shardID))
}
}()
}
Expand Down
2 changes: 1 addition & 1 deletion sharding/shard_rate_limiter_impl.go
Expand Up @@ -40,7 +40,7 @@ func (r *rateLimiterImpl) Close(ctx context.Context) {
go func() {
defer wg.Done()
if err := b.mu.CLock(ctx); err != nil {
r.config.Logger.Error("failed to close bucket: ", err)
r.config.Logger.Error("failed to close bucket", slog.Any("err", err))
}
b.mu.Unlock()
}()
Expand Down
4 changes: 2 additions & 2 deletions voice/audio_receiver.go
Expand Up @@ -88,12 +88,12 @@ func (s *defaultAudioReceiver) receive() {
return
}
if err != nil {
s.logger.Error("error while reading packet", slog.String("err", err.Error()))
s.logger.Error("error while reading packet", slog.Any("err", err))
return
}
if s.opusReceiver != nil {
if err = s.opusReceiver.ReceiveOpusFrame(s.conn.UserIDBySSRC(packet.SSRC), packet); err != nil {
s.logger.Error("error while receiving opus frame", slog.String("err", err.Error()))
s.logger.Error("error while receiving opus frame", slog.Any("err", err))
}
}

Expand Down
4 changes: 2 additions & 2 deletions voice/audio_sender.go
Expand Up @@ -101,7 +101,7 @@ func (s *defaultAudioSender) send() {
}
opus, err := s.opusProvider.ProvideOpusFrame()
if err != nil && err != io.EOF {
s.logger.Error("error while reading opus frame", slog.String("err", err.Error()))
s.logger.Error("error while reading opus frame", slog.Any("err", err))
return
}
if len(opus) == 0 {
Expand Down Expand Up @@ -143,7 +143,7 @@ func (s *defaultAudioSender) handleErr(err error) {
s.Close()
return
}
s.logger.Error("failed to send audio", slog.String("err", err.Error()))
s.logger.Error("failed to send audio", slog.Any("err", err))
}

func (s *defaultAudioSender) Close() {
Expand Down
6 changes: 3 additions & 3 deletions voice/conn.go
Expand Up @@ -190,7 +190,7 @@ func (c *connImpl) HandleVoiceServerUpdate(update botgateway.EventVoiceServerUpd
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := c.gateway.Open(ctx, c.state); err != nil {
c.config.Logger.Error("error opening voice gateway. error: ", err)
c.config.Logger.Error("error opening voice gateway", slog.Any("err", err))
}
}()
}
Expand All @@ -202,7 +202,7 @@ func (c *connImpl) handleMessage(op Opcode, data GatewayMessageData) {
defer cancel()
ourAddress, ourPort, err := c.udp.Open(ctx, d.IP, d.Port, d.SSRC)
if err != nil {
c.config.Logger.Error("voice: failed to open voiceudp conn. error: ", err)
c.config.Logger.Error("voice: failed to open voiceudp conn", slog.Any("err", err))
break
}
if err = c.Gateway().Send(ctx, OpcodeSelectProtocol, GatewayMessageDataSelectProtocol{
Expand All @@ -213,7 +213,7 @@ func (c *connImpl) handleMessage(op Opcode, data GatewayMessageData) {
Mode: EncryptionModeNormal,
},
}); err != nil {
c.config.Logger.Error("voice: failed to send select protocol. error: ", err)
c.config.Logger.Error("voice: failed to send select protocol", slog.Any("err", err))
}

case GatewayMessageDataSessionDescription:
Expand Down
10 changes: 5 additions & 5 deletions voice/gateway.go
Expand Up @@ -173,7 +173,7 @@ func (g *gatewayImpl) CloseWithCode(code int, message string) {
if g.conn != nil {
g.config.Logger.Debug("closing voice gateway connection", slog.Int("code", code), slog.String("message", message))
if err := g.conn.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(code, message)); err != nil && !errors.Is(err, websocket.ErrCloseSent) {
g.config.Logger.Debug("error writing close code", slog.String("err", err.Error()))
g.config.Logger.Debug("error writing close code", slog.Any("err", err))
}
_ = g.conn.Close()
g.conn = nil
Expand Down Expand Up @@ -204,7 +204,7 @@ func (g *gatewayImpl) sendHeartbeat() {
if !errors.Is(err, ErrGatewayNotConnected) || errors.Is(err, syscall.EPIPE) {
return
}
g.config.Logger.Error("failed to send heartbeat", slog.String("err", err.Error()))
g.config.Logger.Error("failed to send heartbeat", slog.Any("err", err))
g.CloseWithCode(websocket.CloseServiceRestart, "heartbeat timeout")
go g.reconnect()
return
Expand Down Expand Up @@ -244,7 +244,7 @@ loop:

message, err := g.parseMessage(reader)
if err != nil {
g.config.Logger.Error("error while parsing voice gateway event", slog.String("err", err.Error()))
g.config.Logger.Error("error while parsing voice gateway event", slog.Any("err", err))
continue
}

Expand Down Expand Up @@ -350,7 +350,7 @@ func (g *gatewayImpl) reconnectTry(ctx context.Context, try int) error {
if errors.Is(err, discord.ErrGatewayAlreadyConnected) {
return err
}
g.config.Logger.Error("failed to reconnect voice gateway", slog.String("err", err.Error()))
g.config.Logger.Error("failed to reconnect voice gateway", slog.Any("err", err))
g.status = StatusDisconnected
return g.reconnectTry(ctx, try+1)
}
Expand All @@ -359,7 +359,7 @@ func (g *gatewayImpl) reconnectTry(ctx context.Context, try int) error {

func (g *gatewayImpl) reconnect() {
if err := g.reconnectTry(context.Background(), 0); err != nil {
g.config.Logger.Error("failed to reopen voice gateway", slog.String("err", err.Error()))
g.config.Logger.Error("failed to reopen voice gateway", slog.Any("err", err))
}
}

Expand Down

0 comments on commit 7bfde5b

Please sign in to comment.