-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(logfx): add PrettyMode to Config and implement new log handler
- 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
Showing
4 changed files
with
95 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters