Skip to content

Commit

Permalink
Fixed logger (#30)
Browse files Browse the repository at this point in the history
* fix(logger): fixed logger so it logs only correct levels

* refactor: removed some unneccessary logging
  • Loading branch information
maxguuse authored Jul 18, 2024
1 parent ed292cf commit f9d02ee
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 20 deletions.
22 changes: 12 additions & 10 deletions apps/discord/internal/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,18 @@ func New(opts ClientOpts) error {
lrRepo: opts.LiverolesRepo,
}

router.Use(func(hf disroute.HandlerFunc) disroute.HandlerFunc {
return func(ctx *disroute.Ctx) disroute.Response {
c.logger.Debug("interaction",
slog.String("type", ctx.Interaction().Type.String()),
slog.String("id", ctx.Interaction().ID),
slog.String("user", ctx.Interaction().Member.User.GlobalName))

return hf(ctx)
}
})
if opts.Cfg.Environment != "prod" {
router.Use(func(hf disroute.HandlerFunc) disroute.HandlerFunc {
return func(ctx *disroute.Ctx) disroute.Response {
c.logger.Debug("interaction",
slog.String("type", ctx.Interaction().Type.String()),
slog.String("id", ctx.Interaction().ID),
slog.String("user", ctx.Interaction().Member.User.GlobalName))

return hf(ctx)
}
})
}

router.Use(func(hf disroute.HandlerFunc) disroute.HandlerFunc {
return func(ctx *disroute.Ctx) disroute.Response {
Expand Down
10 changes: 5 additions & 5 deletions apps/discord/internal/client/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,15 @@ func (c *Client) onStatusChanged(_ *discordgo.Session, u *discordgo.PresenceUpda
slog.Any("error", err),
slog.String("guild_id", u.GuildID),
)

return
}

roles, err := c.lrRepo.GetLiveroles(ctx, int64(guildId))
if err != nil {
c.logger.Error("could not give streaming role", err)
c.logger.Warn("could not get liveroles from db", err)

return
}

var member *discordgo.Member
Expand Down Expand Up @@ -83,10 +87,6 @@ func (c *Client) onStatusChanged(_ *discordgo.Session, u *discordgo.PresenceUpda
if err != nil {
c.logger.Error("could not add role", err)
}
c.logger.Debug("Role added",
slog.Int("role", role.DiscordRoleID),
slog.String("user", u.Presence.User.ID),
)
}
} else {
for _, liveroleId := range liverolesIds {
Expand Down
6 changes: 5 additions & 1 deletion apps/discord/internal/client/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ func (c *Client) registerLogger() {

r := slog.NewRecord(time.Now(), lvl, msg, pcs[0])

_ = c.logger.Handler().Handle(context.Background(), r)
if !c.logger.Handler().Enabled(context.Background(), lvl) {
return
}

_ = c.logger.Handler().Handle(context.Background(), r) //nolint: errcheck
}
}
18 changes: 14 additions & 4 deletions libs/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
type Logger interface {
Info(input string, fields ...any)
Error(input string, fields ...any)
Warn(input string, fields ...any)
Debug(input string, fields ...any)
Handler() slog.Handler
}
Expand All @@ -30,12 +31,12 @@ func New(service string) func() Logger {
}
}

func newLogger(service string) Logger {
func newLogger(service string) Logger { //nolint: ireturn
var log *slog.Logger

replaceAttr := func(groups []string, attr slog.Attr) slog.Attr {
if attr.Key == slog.SourceKey {
source := attr.Value.Any().(*slog.Source)
source := attr.Value.Any().(*slog.Source) //nolint: forcetypeassert

pathParts := strings.Split(source.File, "/")
p := pathParts[len(pathParts)-4:]
Expand All @@ -58,7 +59,7 @@ func newLogger(service string) Logger {
slog.NewJSONHandler(
os.Stdout,
&slog.HandlerOptions{
Level: slog.LevelError,
Level: slog.LevelWarn,
AddSource: true,
ReplaceAttr: replaceAttr,
},
Expand Down Expand Up @@ -93,13 +94,22 @@ func (c *logger) handle(level slog.Level, input string, fields ...any) {
for _, f := range fields {
r.Add(f)
}
_ = c.log.Handler().Handle(context.Background(), r)

if !c.log.Handler().Enabled(context.Background(), level) {
return
}

_ = c.log.Handler().Handle(context.Background(), r) //nolint: errcheck
}

func (c *logger) Info(input string, fields ...any) {
c.handle(slog.LevelInfo, input, fields...)
}

func (c *logger) Warn(input string, fields ...any) {
c.handle(slog.LevelWarn, input, fields...)
}

func (c *logger) Error(input string, fields ...any) {
c.handle(slog.LevelError, input, fields...)
}
Expand Down

0 comments on commit f9d02ee

Please sign in to comment.