Skip to content

Commit

Permalink
💥 break: rename some interfaces and structs
Browse files Browse the repository at this point in the history
- add func for quick cast Formatter
  • Loading branch information
inhere committed Jan 4, 2023
1 parent 4b4ced1 commit 38418c7
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 31 deletions.
30 changes: 23 additions & 7 deletions formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,49 @@ func (fn FormatterFunc) Format(r *Record) ([]byte, error) {
return fn(r)
}

// FormattableHandler interface
type FormattableHandler interface {
// Formattable interface
type Formattable interface {
// Formatter get the log formatter
Formatter() Formatter
// SetFormatter set the log formatter
SetFormatter(Formatter)
}

// Formattable definition
type Formattable struct {
// FormattableTrait definition
type FormattableTrait struct {
formatter Formatter
}

// Formatter get formatter. if not set, will return TextFormatter
func (f *Formattable) Formatter() Formatter {
func (f *FormattableTrait) Formatter() Formatter {
if f.formatter == nil {
f.formatter = NewTextFormatter()
}
return f.formatter
}

// SetFormatter to handler
func (f *Formattable) SetFormatter(formatter Formatter) {
func (f *FormattableTrait) SetFormatter(formatter Formatter) {
f.formatter = formatter
}

// Format log record to bytes
func (f *Formattable) Format(record *Record) ([]byte, error) {
func (f *FormattableTrait) Format(record *Record) ([]byte, error) {
return f.Formatter().Format(record)
}

// AsTextFormatter util func
func AsTextFormatter(f Formatter) *TextFormatter {
if tf, ok := f.(*TextFormatter); ok {
return tf
}
panic("slog: cannot cast input as *TextFormatter")
}

// AsJSONFormatter util func
func AsJSONFormatter(f Formatter) *JSONFormatter {
if jf, ok := f.(*JSONFormatter); ok {
return jf
}
panic("slog: cannot cast input as *JSONFormatter")
}
2 changes: 1 addition & 1 deletion formatter_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ type JSONFormatter struct {
}

// NewJSONFormatter create new JSONFormatter
func NewJSONFormatter(fn ...func(*JSONFormatter)) *JSONFormatter {
func NewJSONFormatter(fn ...func(f *JSONFormatter)) *JSONFormatter {
f := &JSONFormatter{
// Aliases: make(StringMap, 0),
Fields: DefaultFields,
Expand Down
18 changes: 17 additions & 1 deletion formatter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,25 @@ import (
"github.com/gookit/slog/handler"
)

func TestFormattableTrait_Formatter(t *testing.T) {
ft := &slog.FormattableTrait{}
tf := slog.AsTextFormatter(ft.Formatter())
assert.NotNil(t, tf)
assert.Panics(t, func() {
slog.AsJSONFormatter(ft.Formatter())
})

ft.SetFormatter(slog.NewJSONFormatter())
jf := slog.AsJSONFormatter(ft.Formatter())
assert.NotNil(t, jf)
assert.Panics(t, func() {
slog.AsTextFormatter(ft.Formatter())
})
}

func TestFormattable_Format(t *testing.T) {
r := newLogRecord("TEST_LOG_MESSAGE format")
f := &slog.Formattable{}
f := &slog.FormattableTrait{}
assert.Eq(t, "slog: TEST_LOG_MESSAGE format", r.GoString())

bts, err := f.Format(r)
Expand Down
12 changes: 9 additions & 3 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,16 @@ type Handler interface {

// LevelFormattable support limit log levels and provide formatter
type LevelFormattable interface {
FormattableHandler
Formattable
IsHandling(level Level) bool
}

// FormattableHandler interface
type FormattableHandler interface {
Handler
Formattable
}

/********************************************************************************
* Common parts for handler
********************************************************************************/
Expand All @@ -38,7 +44,7 @@ type LevelFormattable interface {
// - support set log formatter
// - only support set one log level
type LevelWithFormatter struct {
Formattable
FormattableTrait
// Level for log message. if current level >= Level will log message
Level Level
}
Expand All @@ -58,7 +64,7 @@ func (h *LevelWithFormatter) IsHandling(level Level) bool {
// - support set log formatter
// - support setting multi log levels
type LevelsWithFormatter struct {
Formattable
FormattableTrait
// Levels for log message
Levels []Level
}
Expand Down
27 changes: 10 additions & 17 deletions handler/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func (b *Builder) WithUseJSON(useJSON bool) *Builder {
}

// Build slog handler.
func (b *Builder) Build() slog.Handler {
func (b *Builder) Build() slog.FormattableHandler {
if b.Output != nil {
return b.buildFromWriter(b.Output)
}
Expand All @@ -119,17 +119,11 @@ func (b *Builder) Build() slog.Handler {
return b.buildFromWriter(w)
}

panic("missing some information for build slog handler")
panic("slog: missing information for build slog handler")
}

// Build slog handler.
func (b *Builder) reset() {
b.Output = nil
b.Config = NewEmptyConfig()
}

// Build slog handler.
func (b *Builder) buildFromWriter(w io.Writer) (h slog.Handler) {
func (b *Builder) buildFromWriter(w io.Writer) (h slog.FormattableHandler) {
defer b.reset()
bufSize := b.BuffSize
lf := b.newLevelFormattable()
Expand Down Expand Up @@ -178,14 +172,13 @@ func (b *Builder) buildFromWriter(w io.Writer) (h slog.Handler) {

// use json format.
if b.UseJSON {
type formatterSetter interface {
SetFormatter(slog.Formatter)
}

// has setter
if _, ok := h.(formatterSetter); ok {
h.(formatterSetter).SetFormatter(slog.NewJSONFormatter())
}
h.SetFormatter(slog.NewJSONFormatter())
}
return
}

// rest builder.
func (b *Builder) reset() {
b.Output = nil
b.Config = NewEmptyConfig()
}
4 changes: 2 additions & 2 deletions handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ type SyncCloseWriter interface {
//
// Deprecated: please use slog.LevelWithFormatter instead.
type LevelWithFormatter struct {
slog.Formattable
slog.FormattableTrait
// Level for log message. if current level <= Level will log message
Level slog.Level
}
Expand All @@ -74,7 +74,7 @@ func (h *LevelWithFormatter) IsHandling(level slog.Level) bool {
//
// Deprecated: please use slog.LevelsWithFormatter instead.
type LevelsWithFormatter struct {
slog.Formattable
slog.FormattableTrait
// Levels for log message
Levels []slog.Level
}
Expand Down

0 comments on commit 38418c7

Please sign in to comment.