Skip to content

Commit

Permalink
feat(logfx): add PrettyMode to Config and implement new log handler
Browse files Browse the repository at this point in the history
- Added PrettyMode field to Config struct with default true.
- Created new Handler struct in handler.go for flexible logging.
- Implemented NewHandler function.
- Modified RegisterLogger to use NewHandler.
- Updated replacerGenerator to conditionally handle prettyMode.
  • Loading branch information
eser committed Aug 18, 2024
1 parent 3ec9ec9 commit ac0f02e
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 21 deletions.
5 changes: 3 additions & 2 deletions pkg/bliss/logfx/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package logfx

type Config struct {
Level string `conf:"LEVEL" default:"INFO"`
AddSource bool `conf:"ADD_SOURCE" default:"false"`
Level string `conf:"LEVEL" default:"INFO"`
PrettyMode bool `conf:"PRETTY" default:"true"`
AddSource bool `conf:"ADD_SOURCE" default:"false"`
}
76 changes: 76 additions & 0 deletions pkg/bliss/logfx/handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package logfx

import (
"context"
"fmt"
"io"
"log/slog"
"strings"
)

type Handler struct {
InnerHandler slog.Handler

writer io.Writer
config *Config
}

func NewHandler(w io.Writer, config *Config) (*Handler, error) {
var level slog.Level

err := level.UnmarshalText([]byte(config.Level))
if err != nil {
return nil, fmt.Errorf("failed to parse log level: %w", err)
}

opts := &slog.HandlerOptions{
Level: level,
ReplaceAttr: replacerGenerator(config.PrettyMode),
AddSource: config.AddSource,
}

innerHandler := slog.NewJSONHandler(w, opts)

return &Handler{
InnerHandler: innerHandler,

writer: w,
config: config,
}, nil
}

func (h *Handler) Enabled(ctx context.Context, level slog.Level) bool {
return h.InnerHandler.Enabled(ctx, level)
}

func (h *Handler) Handle(ctx context.Context, rec slog.Record) error {
if h.config.PrettyMode {
out := strings.Builder{}

out.WriteString(rec.Time.Format("2006-01-02 15:04:05"))
out.WriteRune(' ')
out.WriteString(rec.Level.String())
out.WriteRune(' ')
out.WriteString(rec.Message)
out.WriteRune(' ')

_, err := io.WriteString(h.writer, out.String())
if err != nil {
return fmt.Errorf("failed to write log: %w", err)
}
}

return h.InnerHandler.Handle(ctx, rec)
}

func (h *Handler) WithAttrs(attrs []slog.Attr) slog.Handler {
return &Handler{
InnerHandler: h.InnerHandler.WithAttrs(attrs),
}
}

func (h *Handler) WithGroup(name string) slog.Handler {
return &Handler{
InnerHandler: h.InnerHandler.WithGroup(name),
}
}
10 changes: 1 addition & 9 deletions pkg/bliss/logfx/mod.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,11 @@ var Module = fx.Module(
)

func RegisterLogger(config *Config) (*slog.Logger, error) {
var level slog.Level

err := level.UnmarshalText([]byte(config.Level))
handler, err := NewHandler(os.Stderr, config)
if err != nil {
return nil, err
}

handler := slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{
Level: level,
ReplaceAttr: replaceAttr,
AddSource: config.AddSource,
})

logger := slog.New(handler)

slog.SetDefault(logger)
Expand Down
25 changes: 15 additions & 10 deletions pkg/bliss/logfx/replacer.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,23 @@ type (
}
)

func replaceAttr(groups []string, attr slog.Attr) slog.Attr {
switch attr.Value.Kind() { //nolint:gocritic,wsl,exhaustive
// other cases

case slog.KindAny:
switch v := attr.Value.Any().(type) { //nolint:gocritic
case error:
attr.Value = fmtErr(v)
func replacerGenerator(prettyMode bool) func([]string, slog.Attr) slog.Attr {
return func(groups []string, attr slog.Attr) slog.Attr {
if prettyMode {
if attr.Key == slog.TimeKey || attr.Key == slog.LevelKey || attr.Key == slog.MessageKey {
return slog.Attr{} //nolint:exhaustruct
}
}

if attr.Value.Kind() == slog.KindAny {
switch v := attr.Value.Any().(type) { //nolint:gocritic
case error:
attr.Value = fmtErr(v)
}
}
}

return attr
return attr
}
}

// fmtErr returns a slog.GroupValue with keys "msg" and "trace". If the error
Expand Down

0 comments on commit ac0f02e

Please sign in to comment.