Skip to content

Commit

Permalink
✨ feat(handler): add more quick config func for build handler.
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Feb 2, 2023
1 parent 400f636 commit 6ce5dc7
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 35 deletions.
3 changes: 3 additions & 0 deletions common.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ const (
// CallerFlagPkg report full package name.
// eg: "github.com/gookit/slog_test"
CallerFlagPkg
// CallerFlagPkgFnl report full package name + filename + line.
// eg: "github.com/gookit/slog_test,logger_test.go:48"
CallerFlagPkgFnl
// CallerFlagFpLine report full filepath with line.
// eg: "/work/go/gookit/slog/logger_test.go:48"
CallerFlagFpLine
Expand Down
50 changes: 20 additions & 30 deletions handler/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,30 +212,22 @@ func (c *Config) wrapBuffer(w io.Writer) (bw flushSyncCloseWriter) {

// WithLogfile setting
func WithLogfile(logfile string) ConfigFn {
return func(c *Config) {
c.Logfile = logfile
}
return func(c *Config) { c.Logfile = logfile }
}

// WithLevelMode setting
func WithLevelMode(mode uint8) ConfigFn {
return func(c *Config) {
c.LevelMode = mode
}
return func(c *Config) { c.LevelMode = mode }
}

// WithLogLevel setting
func WithLogLevel(level slog.Level) ConfigFn {
return func(c *Config) {
c.Level = level
}
return func(c *Config) { c.Level = level }
}

// WithLogLevels setting
func WithLogLevels(levels slog.Levels) ConfigFn {
return func(c *Config) {
c.Levels = levels
}
return func(c *Config) { c.Levels = levels }
}

// WithLevelNames set levels by level names.
Expand All @@ -252,42 +244,40 @@ func WithLevelNames(names []string) ConfigFn {

// WithRotateTime setting
func WithRotateTime(rt rotatefile.RotateTime) ConfigFn {
return func(c *Config) {
c.RotateTime = rt
}
return func(c *Config) { c.RotateTime = rt }
}

// WithBackupNum setting
func WithBackupNum(bt uint) ConfigFn {
return func(c *Config) { c.BackupNum = bt }
}

// WithBackupTime setting
func WithBackupTime(bt uint) ConfigFn {
return func(c *Config) { c.BackupTime = bt }
}

// WithBuffMode setting
func WithBuffMode(buffMode string) ConfigFn {
return func(c *Config) {
c.BuffMode = buffMode
}
return func(c *Config) { c.BuffMode = buffMode }
}

// WithBuffSize setting
func WithBuffSize(buffSize int) ConfigFn {
return func(c *Config) {
c.BuffSize = buffSize
}
return func(c *Config) { c.BuffSize = buffSize }
}

// WithMaxSize setting
func WithMaxSize(maxSize uint64) ConfigFn {
return func(c *Config) {
c.MaxSize = maxSize
}
return func(c *Config) { c.MaxSize = maxSize }
}

// WithCompress setting
func WithCompress(compress bool) ConfigFn {
return func(c *Config) {
c.Compress = compress
}
return func(c *Config) { c.Compress = compress }
}

// WithUseJSON setting
func WithUseJSON(useJSON bool) ConfigFn {
return func(c *Config) {
c.UseJSON = useJSON
}
return func(c *Config) { c.UseJSON = useJSON }
}
5 changes: 5 additions & 0 deletions handler/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ func TestNewConfig(t *testing.T) {
c := handler.NewConfig(
handler.WithCompress(true),
handler.WithLevelMode(handler.LevelModeValue),
handler.WithBackupNum(20),
handler.WithBackupTime(1800),
func(c *handler.Config) {
c.BackupTime = 23
},
).
With(handler.WithBuffSize(129)).
WithConfigFn(handler.WithLogLevel(slog.ErrorLevel))
Expand Down
15 changes: 10 additions & 5 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,27 +48,32 @@ func getCaller(callerSkip int) (fr runtime.Frame, ok bool) {
}

func formatCaller(rf *runtime.Frame, flag uint8) (cs string) {
lineNum := strconv.FormatInt(int64(rf.Line), 10)
switch flag {
case CallerFlagFull:
return rf.Function + "," + path.Base(rf.File) + ":" + strconv.FormatInt(int64(rf.Line), 10)
return rf.Function + "," + path.Base(rf.File) + ":" + lineNum
case CallerFlagFunc:
return rf.Function
case CallerFlagFcLine:
return rf.Function + ":" + strconv.Itoa(rf.Line)
return rf.Function + ":" + lineNum
case CallerFlagPkg:
i := strings.LastIndex(rf.Function, "/")
i += strings.IndexByte(rf.Function[i+1:], '.')
return rf.Function[:i+1]
case CallerFlagPkgFnl:
i := strings.LastIndex(rf.Function, "/")
i += strings.IndexByte(rf.Function[i+1:], '.')
return rf.Function[:i+1] + "," + path.Base(rf.File) + ":" + lineNum
case CallerFlagFnlFcn:
ss := strings.Split(rf.Function, ".")
return path.Base(rf.File) + ":" + strconv.Itoa(rf.Line) + "," + ss[len(ss)-1]
return path.Base(rf.File) + ":" + lineNum + "," + ss[len(ss)-1]
case CallerFlagFnLine:
return path.Base(rf.File) + ":" + strconv.Itoa(rf.Line)
return path.Base(rf.File) + ":" + lineNum
case CallerFlagFcName:
ss := strings.Split(rf.Function, ".")
return ss[len(ss)-1]
default: // CallerFlagFpLine
return rf.File + ":" + strconv.Itoa(rf.Line)
return rf.File + ":" + lineNum
}
}

Expand Down

0 comments on commit 6ce5dc7

Please sign in to comment.