Skip to content

Commit

Permalink
refactor: refactor handlers with level and formatter logic
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Apr 26, 2022
1 parent 648d002 commit 7d583b5
Show file tree
Hide file tree
Showing 22 changed files with 357 additions and 388 deletions.
51 changes: 6 additions & 45 deletions define.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package slog

import (
"errors"
"io"
"strings"
)

Expand Down Expand Up @@ -57,50 +56,6 @@ func (ls Levels) Contains(level Level) bool {
return false
}

// FlushWriter is the interface satisfied by logging destinations.
type FlushWriter interface {
Flush() error
// Writer the output writer
io.Writer
}

// FlushCloseWriter is the interface satisfied by logging destinations.
type FlushCloseWriter interface {
Flush() error
// WriteCloser the output writer
io.WriteCloser
}

// FormatterWriterHandler interface
type FormatterWriterHandler interface {
Handler
// Formatter record formatter
Formatter() Formatter
// Writer the output writer
Writer() io.Writer
}

//
// Handler interface
//

// Handler interface definition
type Handler interface {
// Closer Close handler.
// You should first call Flush() on close logic.
// Refer the FileHandler.Close() handle
io.Closer
// Flush and sync logs to disk file.
Flush() error
// IsHandling Checks whether the given record will be handled by this handler.
IsHandling(level Level) bool
// Handle a log record.
//
// All records may be passed to this method, and the handler should discard
// those that it does not want to handle.
Handle(*Record) error
}

//
// Processor interface
//
Expand Down Expand Up @@ -171,6 +126,12 @@ type FormattableHandler interface {
SetFormatter(Formatter)
}

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

// Formattable definition
type Formattable struct {
formatter Formatter
Expand Down
96 changes: 96 additions & 0 deletions handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package slog

import "io"

// FlushWriter is the interface satisfied by logging destinations.
type FlushWriter interface {
Flush() error
// Writer the output writer
io.Writer
}

// FlushCloseWriter is the interface satisfied by logging destinations.
type FlushCloseWriter interface {
Flush() error
// WriteCloser the output writer
io.WriteCloser
}

// FormatterWriterHandler interface
type FormatterWriterHandler interface {
Handler
// Formatter record formatter
Formatter() Formatter
// Writer the output writer
Writer() io.Writer
}

//
// Handler interface
//

// Handler interface definition
type Handler interface {
// Closer Close handler.
// You should first call Flush() on close logic.
// Refer the FileHandler.Close() handle
io.Closer
// Flush and sync logs to disk file.
Flush() error
// IsHandling Checks whether the given record will be handled by this handler.
IsHandling(level Level) bool
// Handle a log record.
//
// All records may be passed to this method, and the handler should discard
// those that it does not want to handle.
Handle(*Record) error
}

/********************************************************************************
* Common parts for handler
********************************************************************************/

// LevelWithFormatter struct definition
//
// - support set log formatter
// - only support set one log level
type LevelWithFormatter struct {
Formattable
// Level for log message. if current level <= Level will log message
Level Level
}

// NewLvFormatter create new instance
func NewLvFormatter(lv Level) *LevelWithFormatter {
return &LevelWithFormatter{Level: lv}
}

// IsHandling Check if the current level can be handling
func (h *LevelWithFormatter) IsHandling(level Level) bool {
return h.Level.ShouldHandling(level)
}

// LevelsWithFormatter struct definition
//
// - support set log formatter
// - support setting multi log levels
type LevelsWithFormatter struct {
Formattable
// Levels for log message
Levels []Level
}

// NewLvsFormatter create new instance
func NewLvsFormatter(levels []Level) *LevelsWithFormatter {
return &LevelsWithFormatter{Levels: levels}
}

// IsHandling Check if the current level can be handling
func (h *LevelsWithFormatter) IsHandling(level Level) bool {
for _, l := range h.Levels {
if l == level {
return true
}
}
return false
}
57 changes: 0 additions & 57 deletions handler/base_common.go

This file was deleted.

74 changes: 74 additions & 0 deletions handler/buffer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package handler

import (
"io"
"os"

"github.com/gookit/slog"
"github.com/gookit/slog/bufwrite"
)

// NewBuffered create new BufferedHandler
func NewBuffered(cWriter io.WriteCloser, bufSize int, levels ...slog.Level) *FlushCloseHandler {
return NewBufferedHandler(cWriter, bufSize, levels...)
}

// NewBufferedHandler create new BufferedHandler
func NewBufferedHandler(cWriter io.WriteCloser, bufSize int, levels ...slog.Level) *FlushCloseHandler {
if len(levels) == 0 {
levels = slog.AllLevels
}

return &FlushCloseHandler{
Output: bufwrite.NewBufIOWriterSize(cWriter, bufSize),
// log levels
LevelFormattable: slog.NewLvsFormatter(levels),
}
}

// LineBufferedFile handler
func LineBufferedFile(logfile string, bufSize int, levels []slog.Level) (slog.Handler, error) {
cfg := NewConfig(
WithLogfile(logfile),
WithBuffSize(bufSize),
WithLogLevels(levels),
WithBuffMode(BuffModeLine),
)

output, err := cfg.SyncCloseWriter()
if err != nil {
return nil, err
}

return &SyncCloseHandler{
Output: output,
// init log levels
LevelFormattable: slog.NewLvsFormatter(cfg.Levels),
}, nil
}

// LineBuffOsFile handler
func LineBuffOsFile(f *os.File, bufSize int, levels []slog.Level) slog.Handler {
if f == nil {
panic("slog: the os file cannot be nil")
}

return &SyncCloseHandler{
Output: bufwrite.NewLineWriterSize(f, bufSize),
// init log levels
LevelFormattable: slog.NewLvsFormatter(levels),
}
}

// LineBuffWriter handler
func LineBuffWriter(w io.Writer, bufSize int, levels []slog.Level) slog.Handler {
if w == nil {
panic("slog: the io writer cannot be nil")
}

return &IOWriterHandler{
Output: bufwrite.NewLineWriterSize(w, bufSize),
// init log levels
LevelFormattable: slog.NewLvsFormatter(levels),
}
}
49 changes: 0 additions & 49 deletions handler/buffer_lined.go

This file was deleted.

Loading

0 comments on commit 7d583b5

Please sign in to comment.