Skip to content

Commit

Permalink
remove slog WithGroup and use name logger field
Browse files Browse the repository at this point in the history
  • Loading branch information
topi314 committed Nov 10, 2023
1 parent 566a604 commit b993c92
Show file tree
Hide file tree
Showing 32 changed files with 57 additions and 37 deletions.
22 changes: 11 additions & 11 deletions bot/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ func BuildClient(token string, cfg *Config, gatewayEventHandlerFunc func(client
rest.WithUserAgent(fmt.Sprintf("DiscordBot (%s, %s)", github, version)),
rest.WithLogger(client.logger),
func(config *rest.Config) {
config.RateLimiterConfigOpts = append([]rest.RateLimiterConfigOpt{rest.WithRateLimiterLogger(cfg.Logger.WithGroup("rate_limiter"))}, config.RateLimiterConfigOpts...)
config.RateLimiterConfigOpts = append([]rest.RateLimiterConfigOpt{rest.WithRateLimiterLogger(cfg.Logger)}, config.RateLimiterConfigOpts...)
},
}, cfg.RestClientConfigOpts...)

Expand All @@ -243,12 +243,12 @@ func BuildClient(token string, cfg *Config, gatewayEventHandlerFunc func(client
client.restServices = cfg.Rest

if cfg.VoiceManager == nil {
cfg.VoiceManager = voice.NewManager(client.UpdateVoiceState, *id, append([]voice.ManagerConfigOpt{voice.WithLogger(cfg.Logger.WithGroup("voice"))}, cfg.VoiceManagerConfigOpts...)...)
cfg.VoiceManager = voice.NewManager(client.UpdateVoiceState, *id, append([]voice.ManagerConfigOpt{voice.WithLogger(cfg.Logger)}, cfg.VoiceManagerConfigOpts...)...)
}
client.voiceManager = cfg.VoiceManager

if cfg.EventManager == nil {
cfg.EventManager = NewEventManager(client, append([]EventManagerConfigOpt{WithEventManagerLogger(cfg.Logger.WithGroup("event_manager"))}, cfg.EventManagerConfigOpts...)...)
cfg.EventManager = NewEventManager(client, append([]EventManagerConfigOpt{WithEventManagerLogger(cfg.Logger)}, cfg.EventManagerConfigOpts...)...)
}
client.eventManager = cfg.EventManager

Expand All @@ -261,12 +261,12 @@ func BuildClient(token string, cfg *Config, gatewayEventHandlerFunc func(client

cfg.GatewayConfigOpts = append([]gateway.ConfigOpt{
gateway.WithURL(gatewayRs.URL),
gateway.WithLogger(cfg.Logger.WithGroup("gateway")),
gateway.WithLogger(cfg.Logger),
gateway.WithOS(os),
gateway.WithBrowser(name),
gateway.WithDevice(name),
func(config *gateway.Config) {
config.RateLimiterConfigOpts = append([]gateway.RateLimiterConfigOpt{gateway.WithRateLimiterLogger(cfg.Logger.WithGroup("rate_limiter"))}, config.RateLimiterConfigOpts...)
config.RateLimiterConfigOpts = append([]gateway.RateLimiterConfigOpt{gateway.WithRateLimiterLogger(cfg.Logger)}, config.RateLimiterConfigOpts...)
},
}, cfg.GatewayConfigOpts...)

Expand All @@ -291,17 +291,17 @@ func BuildClient(token string, cfg *Config, gatewayEventHandlerFunc func(client
sharding.WithShardIDs(shardIDs...),
sharding.WithGatewayConfigOpts(
gateway.WithURL(gatewayBotRs.URL),
gateway.WithLogger(cfg.Logger.WithGroup("gateway")),
gateway.WithLogger(cfg.Logger),
gateway.WithOS(os),
gateway.WithBrowser(name),
gateway.WithDevice(name),
func(config *gateway.Config) {
config.RateLimiterConfigOpts = append([]gateway.RateLimiterConfigOpt{gateway.WithRateLimiterLogger(cfg.Logger.WithGroup("rate_limiter"))}, config.RateLimiterConfigOpts...)
config.RateLimiterConfigOpts = append([]gateway.RateLimiterConfigOpt{gateway.WithRateLimiterLogger(cfg.Logger)}, config.RateLimiterConfigOpts...)
},
),
sharding.WithLogger(cfg.Logger.WithGroup("sharding")),
sharding.WithLogger(cfg.Logger),
func(config *sharding.Config) {
config.RateLimiterConfigOpts = append([]sharding.RateLimiterConfigOpt{sharding.WithRateLimiterLogger(cfg.Logger.WithGroup("rate_limiter")), sharding.WithMaxConcurrency(gatewayBotRs.SessionStartLimit.MaxConcurrency)}, config.RateLimiterConfigOpts...)
config.RateLimiterConfigOpts = append([]sharding.RateLimiterConfigOpt{sharding.WithRateLimiterLogger(cfg.Logger), sharding.WithMaxConcurrency(gatewayBotRs.SessionStartLimit.MaxConcurrency)}, config.RateLimiterConfigOpts...)
},
}, cfg.ShardManagerConfigOpts...)

Expand All @@ -311,15 +311,15 @@ func BuildClient(token string, cfg *Config, gatewayEventHandlerFunc func(client

if cfg.HTTPServer == nil && cfg.PublicKey != "" {
cfg.HTTPServerConfigOpts = append([]httpserver.ConfigOpt{
httpserver.WithLogger(cfg.Logger.WithGroup("http_server")),
httpserver.WithLogger(cfg.Logger),
}, cfg.HTTPServerConfigOpts...)

cfg.HTTPServer = httpserver.New(cfg.PublicKey, httpServerEventHandlerFunc(client), cfg.HTTPServerConfigOpts...)
}
client.httpServer = cfg.HTTPServer

if cfg.MemberChunkingManager == nil {
cfg.MemberChunkingManager = NewMemberChunkingManager(client, cfg.Logger.WithGroup("member_chunking_manager"), cfg.MemberChunkingFilter)
cfg.MemberChunkingManager = NewMemberChunkingManager(client, cfg.Logger, cfg.MemberChunkingFilter)
}
client.memberChunkingManager = cfg.MemberChunkingManager

Expand Down
1 change: 1 addition & 0 deletions bot/event_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var _ EventManager = (*eventManagerImpl)(nil)
func NewEventManager(client Client, opts ...EventManagerConfigOpt) EventManager {
cfg := DefaultEventManagerConfig()
cfg.Apply(opts)
cfg.Logger = cfg.Logger.With(slog.String("name", "bot_event_manager"))

return &eventManagerImpl{
client: client,
Expand Down
2 changes: 1 addition & 1 deletion bot/event_manager_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
// DefaultEventManagerConfig returns a new EventManagerConfig with all default values.
func DefaultEventManagerConfig() *EventManagerConfig {
return &EventManagerConfig{
Logger: slog.Default().WithGroup("event_manager"),
Logger: slog.Default(),
}
}

Expand Down
4 changes: 3 additions & 1 deletion bot/member_chunking_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ func NewMemberChunkingManager(client Client, logger *slog.Logger, memberChunking
memberChunkingFilter = MemberChunkingFilterNone
}
if logger == nil {
logger = slog.Default().WithGroup("member_chunking_manager")
logger = slog.Default()
}
logger = logger.With(slog.String("name", "bot_member_chunking_manager"))

return &memberChunkingManagerImpl{
client: client,
logger: logger,
Expand Down
2 changes: 1 addition & 1 deletion gateway/gateway_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
// DefaultConfig returns a Config with sensible defaults.
func DefaultConfig() *Config {
return &Config{
Logger: slog.Default().WithGroup("gateway"),
Logger: slog.Default(),
Dialer: websocket.DefaultDialer,
LargeThreshold: 50,
Intents: IntentsDefault,
Expand Down
3 changes: 1 addition & 2 deletions gateway/gateway_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ var _ Gateway = (*gatewayImpl)(nil)
func New(token string, eventHandlerFunc EventHandlerFunc, closeHandlerFunc CloseHandlerFunc, opts ...ConfigOpt) Gateway {
config := DefaultConfig()
config.Apply(opts)

config.Logger = config.Logger.With(slog.Int("shard_id", config.ShardID), slog.Int("shard_count", config.ShardCount))
config.Logger = config.Logger.With(slog.String("name", "gateway"), slog.Int("shard_id", config.ShardID), slog.Int("shard_count", config.ShardCount))

return &gatewayImpl{
config: *config,
Expand Down
2 changes: 1 addition & 1 deletion gateway/gateway_rate_limiter_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
// DefaultRateLimiterConfig returns a RateLimiterConfig with sensible defaults.
func DefaultRateLimiterConfig() *RateLimiterConfig {
return &RateLimiterConfig{
Logger: slog.Default().WithGroup("rate_limiter"),
Logger: slog.Default(),
CommandsPerMinute: CommandsPerMinute,
}
}
Expand Down
2 changes: 2 additions & 0 deletions gateway/gateway_rate_limiter_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gateway

import (
"context"
"log/slog"
"time"

"github.com/sasha-s/go-csync"
Expand All @@ -11,6 +12,7 @@ import (
func NewRateLimiter(opts ...RateLimiterConfigOpt) RateLimiter {
config := DefaultRateLimiterConfig()
config.Apply(opts)
config.Logger = config.Logger.With(slog.String("name", "gateway_rate_limiter"))

return &rateLimiterImpl{
config: *config,
Expand Down
5 changes: 3 additions & 2 deletions httpserver/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ import (
// DefaultConfig returns a Config with sensible defaults.
func DefaultConfig() *Config {
return &Config{
URL: "/interactions/callback",
Address: ":80",
Logger: slog.Default(),
HTTPServer: &http.Server{},
ServeMux: http.NewServeMux(),
URL: "/interactions/callback",
Address: ":80",
}
}

Expand Down
1 change: 1 addition & 0 deletions httpserver/server_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ var _ Server = (*serverImpl)(nil)
func New(publicKey string, eventHandlerFunc EventHandlerFunc, opts ...ConfigOpt) Server {
config := DefaultConfig()
config.Apply(opts)
config.Logger = config.Logger.With(slog.String("name", "httpserver"))

hexDecodedKey, err := hex.DecodeString(publicKey)
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions oauth2/client_impl.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package oauth2

import (
"log/slog"
"time"

"github.com/disgoorg/snowflake/v2"
Expand All @@ -13,6 +14,7 @@ import (
func New(id snowflake.ID, secret string, opts ...ConfigOpt) Client {
config := DefaultConfig()
config.Apply(opts)
config.Logger = config.Logger.With(slog.String("name", "oauth2"))

return &clientImpl{
id: id,
Expand Down
6 changes: 3 additions & 3 deletions oauth2/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
// DefaultConfig is the configuration which is used by default
func DefaultConfig() *Config {
return &Config{
Logger: slog.Default().WithGroup("oauth2"),
Logger: slog.Default(),
}
}

Expand All @@ -32,13 +32,13 @@ func (c *Config) Apply(opts []ConfigOpt) {
opt(c)
}
if c.RestClient == nil {
c.RestClient = rest.NewClient("", append([]rest.ConfigOpt{rest.WithLogger(c.Logger.WithGroup("rest"))}, c.RestClientConfigOpts...)...)
c.RestClient = rest.NewClient("", append([]rest.ConfigOpt{rest.WithLogger(c.Logger)}, c.RestClientConfigOpts...)...)
}
if c.OAuth2 == nil {
c.OAuth2 = rest.NewOAuth2(c.RestClient)
}
if c.StateController == nil {
c.StateController = NewStateController(append([]StateControllerConfigOpt{WithStateControllerLogger(c.Logger.WithGroup("state_controller"))}, c.StateControllerConfigOpts...)...)
c.StateController = NewStateController(append([]StateControllerConfigOpt{WithStateControllerLogger(c.Logger)}, c.StateControllerConfigOpts...)...)
}
}

Expand Down
1 change: 1 addition & 0 deletions oauth2/state_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type StateController interface {
func NewStateController(opts ...StateControllerConfigOpt) StateController {
config := DefaultStateControllerConfig()
config.Apply(opts)
config.Logger = config.Logger.With(slog.String("name", "oauth2_state_controller"))

states := newTTLMap(config.MaxTTL)
for state, url := range config.States {
Expand Down
2 changes: 1 addition & 1 deletion oauth2/state_controller_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
// DefaultStateControllerConfig is the default configuration for the StateController
func DefaultStateControllerConfig() *StateControllerConfig {
return &StateControllerConfig{
Logger: slog.Default().WithGroup("state_controller"),
Logger: slog.Default(),
States: map[string]string{},
NewStateFunc: func() string { return insecurerandstr.RandStr(32) },
MaxTTL: time.Hour,
Expand Down
1 change: 1 addition & 0 deletions rest/rest_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
func NewClient(botToken string, opts ...ConfigOpt) Client {
config := DefaultConfig()
config.Apply(opts)
config.Logger = config.Logger.With(slog.String("name", "rest_client"))

config.RateLimiter.Reset()

Expand Down
2 changes: 1 addition & 1 deletion rest/rest_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
// DefaultConfig is the configuration which is used by default
func DefaultConfig() *Config {
return &Config{
Logger: slog.Default().WithGroup("rest"),
Logger: slog.Default(),
HTTPClient: &http.Client{Timeout: 20 * time.Second},
URL: fmt.Sprintf("%sv%d", API, Version),
}
Expand Down
1 change: 1 addition & 0 deletions rest/rest_rate_limiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type RateLimiter interface {
func NewRateLimiter(opts ...RateLimiterConfigOpt) RateLimiter {
config := DefaultRateLimiterConfig()
config.Apply(opts)
config.Logger = config.Logger.With(slog.String("name", "rest_rate_limiter"))

rateLimiter := &rateLimiterImpl{
config: *config,
Expand Down
2 changes: 1 addition & 1 deletion rest/rest_rate_limiter_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
// DefaultRateLimiterConfig is the configuration which is used by default.
func DefaultRateLimiterConfig() *RateLimiterConfig {
return &RateLimiterConfig{
Logger: slog.Default().WithGroup("rate_limiter"),
Logger: slog.Default(),
MaxRetries: MaxRetries,
CleanupInterval: CleanupInterval,
}
Expand Down
2 changes: 1 addition & 1 deletion sharding/shard_manager_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
// DefaultConfig returns a Config with sensible defaults.
func DefaultConfig() *Config {
return &Config{
Logger: slog.Default().WithGroup("sharding"),
Logger: slog.Default(),
GatewayCreateFunc: gateway.New,
ShardSplitCount: ShardSplitCount,
}
Expand Down
2 changes: 1 addition & 1 deletion sharding/shard_manager_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ var _ ShardManager = (*shardManagerImpl)(nil)
func New(token string, eventHandlerFunc gateway.EventHandlerFunc, opts ...ConfigOpt) ShardManager {
config := DefaultConfig()
config.Apply(opts)
config.Logger = config.Logger.With(slog.String("name", "sharding"))

return &shardManagerImpl{
shards: map[int]gateway.Gateway{},
Expand Down Expand Up @@ -53,7 +54,6 @@ func (m *shardManagerImpl) closeHandler(shard gateway.Gateway, err error) {
delete(m.config.ShardIDs, shard.ShardID())

newShardCount := shard.ShardCount() * m.config.ShardSplitCount

if newShardCount > m.config.ShardCount {
m.config.ShardCount = newShardCount
}
Expand Down
2 changes: 1 addition & 1 deletion sharding/shard_rate_limiter_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
// DefaultRateLimiterConfig returns a RateLimiterConfig with sensible defaults.
func DefaultRateLimiterConfig() *RateLimiterConfig {
return &RateLimiterConfig{
Logger: slog.Default().WithGroup("rate_limiter"),
Logger: slog.Default(),
MaxConcurrency: MaxConcurrency,
}
}
Expand Down
1 change: 1 addition & 0 deletions sharding/shard_rate_limiter_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var _ RateLimiter = (*rateLimiterImpl)(nil)
func NewRateLimiter(opts ...RateLimiterConfigOpt) RateLimiter {
config := DefaultRateLimiterConfig()
config.Apply(opts)
config.Logger = config.Logger.With(slog.String("name", "sharding_rate_limiter"))

return &rateLimiterImpl{
buckets: map[int]*bucket{},
Expand Down
6 changes: 4 additions & 2 deletions voice/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package voice

import (
"context"
"log/slog"
"sync"
"time"

Expand Down Expand Up @@ -61,6 +62,7 @@ type (
func NewConn(guildID snowflake.ID, userID snowflake.ID, voiceStateUpdateFunc StateUpdateFunc, removeConnFunc func(), opts ...ConnConfigOpt) Conn {
config := DefaultConnConfig()
config.Apply(opts)
config.Logger = config.Logger.With(slog.String("name", "voice_conn"))

conn := &connImpl{
config: *config,
Expand All @@ -75,8 +77,8 @@ func NewConn(guildID snowflake.ID, userID snowflake.ID, voiceStateUpdateFunc Sta
ssrcs: map[uint32]snowflake.ID{},
}

conn.gateway = config.GatewayCreateFunc(conn.handleMessage, conn.handleGatewayClose, append([]GatewayConfigOpt{WithGatewayLogger(config.Logger.WithGroup("gateway"))}, config.GatewayConfigOpts...)...)
conn.udp = config.UDPConnCreateFunc(append([]UDPConnConfigOpt{WithUDPConnLogger(config.Logger.WithGroup("udp"))}, config.UDPConnConfigOpts...)...)
conn.gateway = config.GatewayCreateFunc(conn.handleMessage, conn.handleGatewayClose, append([]GatewayConfigOpt{WithGatewayLogger(config.Logger)}, config.GatewayConfigOpts...)...)
conn.udp = config.UDPConnCreateFunc(append([]UDPConnConfigOpt{WithUDPConnLogger(config.Logger)}, config.UDPConnConfigOpts...)...)

return conn
}
Expand Down
2 changes: 1 addition & 1 deletion voice/conn_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
// DefaultConnConfig returns a ConnConfig with sensible defaults.
func DefaultConnConfig() *ConnConfig {
return &ConnConfig{
Logger: slog.Default().WithGroup("conn"),
Logger: slog.Default(),
GatewayCreateFunc: NewGateway,
UDPConnCreateFunc: NewUDPConn,
AudioSenderCreateFunc: NewAudioSender,
Expand Down
1 change: 1 addition & 0 deletions voice/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ type Gateway interface {
func NewGateway(eventHandlerFunc EventHandlerFunc, closeHandlerFunc CloseHandlerFunc, opts ...GatewayConfigOpt) Gateway {
config := DefaultGatewayConfig()
config.Apply(opts)
config.Logger = config.Logger.With(slog.String("name", "voice_conn_gateway"))

return &gatewayImpl{
config: *config,
Expand Down
2 changes: 1 addition & 1 deletion voice/gateway_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
// DefaultGatewayConfig returns a GatewayConfig with sensible defaults.
func DefaultGatewayConfig() *GatewayConfig {
return &GatewayConfig{
Logger: slog.Default().WithGroup("gateway"),
Logger: slog.Default(),
Dialer: websocket.DefaultDialer,
AutoReconnect: true,
}
Expand Down
4 changes: 3 additions & 1 deletion voice/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ type (
func NewManager(voiceStateUpdateFunc StateUpdateFunc, userID snowflake.ID, opts ...ManagerConfigOpt) Manager {
config := DefaultManagerConfig()
config.Apply(opts)
config.Logger = config.Logger.With(slog.String("name", "voice"))

return &managerImpl{
config: *config,
voiceStateUpdateFunc: voiceStateUpdateFunc,
Expand Down Expand Up @@ -92,7 +94,7 @@ func (m *managerImpl) CreateConn(guildID snowflake.ID) Conn {
var once sync.Once
removeFunc := func() { once.Do(func() { m.RemoveConn(guildID) }) }

conn := m.config.ConnCreateFunc(guildID, m.userID, m.voiceStateUpdateFunc, removeFunc, append([]ConnConfigOpt{WithConnLogger(m.config.Logger.WithGroup("conn"))}, m.config.ConnOpts...)...)
conn := m.config.ConnCreateFunc(guildID, m.userID, m.voiceStateUpdateFunc, removeFunc, append([]ConnConfigOpt{WithConnLogger(m.config.Logger)}, m.config.ConnOpts...)...)
m.conns[guildID] = conn

return conn
Expand Down
2 changes: 1 addition & 1 deletion voice/manager_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import "log/slog"
// DefaultManagerConfig returns the default ManagerConfig with sensible defaults.
func DefaultManagerConfig() *ManagerConfig {
return &ManagerConfig{
Logger: slog.Default().WithGroup("voice"),
Logger: slog.Default(),
ConnCreateFunc: NewConn,
}
}
Expand Down
1 change: 1 addition & 0 deletions voice/udp_conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ type (
func NewUDPConn(opts ...UDPConnConfigOpt) UDPConn {
config := DefaultUDPConnConfig()
config.Apply(opts)
config.Logger = config.Logger.With(slog.String("name", "voice_conn_udp_conn"))

return &udpConnImpl{
config: config,
Expand Down
Loading

0 comments on commit b993c92

Please sign in to comment.