From 77eace3a6a8e2cdae9e7c5c8967fc00d16d4b24d Mon Sep 17 00:00:00 2001 From: Inhere Date: Tue, 25 Oct 2022 13:25:57 +0800 Subject: [PATCH] up: use any to replace all interface{} type --- .github/workflows/go.yml | 2 +- any.go | 7 ++++ common.go | 8 ++--- formatter_text.go | 3 +- logger.go | 69 ++++++++++++++++++++-------------------- record.go | 58 ++++++++++++++++----------------- rotatefile/rotatefile.go | 2 +- slog.go | 48 ++++++++++++++-------------- util.go | 10 +++--- 9 files changed, 108 insertions(+), 99 deletions(-) create mode 100644 any.go diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 45d8e47..d697b44 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -18,7 +18,7 @@ jobs: runs-on: ${{ matrix.os }} strategy: matrix: - go_version: [1.15, 1.16, 1.17, 1.18] + go_version: [1.19, 1.16, 1.17, 1.18] os: [ubuntu-latest, windows-latest] # , macOS-latest steps: diff --git a/any.go b/any.go new file mode 100644 index 0000000..cefb871 --- /dev/null +++ b/any.go @@ -0,0 +1,7 @@ +//go:build !go1.18 +// +build !go1.18 + +package slog + +// alias of interface{}, use for go < 1.18 +type any = interface{} diff --git a/common.go b/common.go index f9ae612..84a7def 100644 --- a/common.go +++ b/common.go @@ -78,8 +78,8 @@ const ( // StringMap string map short name type StringMap = map[string]string -// M short name of map[string]interface{} -type M map[string]interface{} +// M short name of map[string]any +type M map[string]any // String map to string func (m M) String() string { @@ -166,10 +166,10 @@ var ( // DoNothingOnExit handle func. use for testing. DoNothingOnExit = func(code int) {} // DoNothingOnPanic handle func. use for testing. - DoNothingOnPanic = func(v interface{}) {} + DoNothingOnPanic = func(v any) {} // DefaultPanicFn handle func - DefaultPanicFn = func(v interface{}) { + DefaultPanicFn = func(v any) { panic(v) } // DefaultClockFn create func diff --git a/formatter_text.go b/formatter_text.go index 0933468..3c1298f 100644 --- a/formatter_text.go +++ b/formatter_text.go @@ -42,7 +42,7 @@ type TextFormatter struct { FullDisplay bool // EncodeFunc data encode for Record.Data, Record.Extra, etc. // Default is encode by EncodeToString() - EncodeFunc func(v interface{}) string + EncodeFunc func(v any) string } // NewTextFormatter create new TextFormatter @@ -98,6 +98,7 @@ func (f *TextFormatter) Fields() []string { } // Format a log record +// //goland:noinspection GoUnhandledErrorResult func (f *TextFormatter) Format(r *Record) ([]byte, error) { buf := bytebufferpool.Get() diff --git a/logger.go b/logger.go index fde99b2..7ce3657 100644 --- a/logger.go +++ b/logger.go @@ -12,8 +12,8 @@ import ( // SLogger interface type SLogger interface { gsr.Logger - Log(level Level, v ...interface{}) - Logf(level Level, format string, v ...interface{}) + Log(level Level, v ...any) + Logf(level Level, format string, v ...any) } // Logger log dispatcher definition. @@ -49,7 +49,7 @@ type Logger struct { exitHandlers []func() // custom exit, panic handle. ExitFunc func(code int) - PanicFunc func(v interface{}) + PanicFunc func(v any) } // New create a new logger @@ -62,14 +62,14 @@ func NewWithConfig(fn func(l *Logger)) *Logger { return NewWithName("logger").Configure(fn) } -// NewWithHandlers create an new logger with handlers +// NewWithHandlers create a new logger with handlers func NewWithHandlers(hs ...Handler) *Logger { logger := NewWithName("logger") logger.AddHandlers(hs...) return logger } -// NewWithName create an new logger with name +// NewWithName create a new logger with name func NewWithName(name string) *Logger { logger := &Logger{ name: name, @@ -129,7 +129,8 @@ func (l *Logger) Configure(fn func(l *Logger)) *Logger { // FlushDaemon run flush handle on daemon // // Usage: -// go slog.FlushDaemon() +// +// go slog.FlushDaemon() func (l *Logger) FlushDaemon() { for range time.NewTicker(flushInterval).C { err := l.lockAndFlushAll() @@ -323,7 +324,7 @@ func (l *Logger) Record() *Record { } // WithField new record with field -func (l *Logger) WithField(name string, value interface{}) *Record { +func (l *Logger) WithField(name string, value any) *Record { r := l.newRecord() defer l.releaseRecord(r) @@ -368,7 +369,7 @@ func (l *Logger) WithContext(ctx context.Context) *Record { // --------------------------------------------------------------------------- // -func (l *Logger) log(level Level, args []interface{}) { +func (l *Logger) log(level Level, args []any) { r := l.newRecord() r.CallerSkip++ r.log(level, args) @@ -376,7 +377,7 @@ func (l *Logger) log(level Level, args []interface{}) { } // Logf a format message with level -func (l *Logger) logf(level Level, format string, args []interface{}) { +func (l *Logger) logf(level Level, format string, args []any) { r := l.newRecord() r.CallerSkip++ r.logf(level, format, args) @@ -384,128 +385,128 @@ func (l *Logger) logf(level Level, format string, args []interface{}) { } // Log a message with level -func (l *Logger) Log(level Level, args ...interface{}) { +func (l *Logger) Log(level Level, args ...any) { l.log(level, args) } // Logf a format message with level -func (l *Logger) Logf(level Level, format string, args ...interface{}) { +func (l *Logger) Logf(level Level, format string, args ...any) { l.logf(level, format, args) } // Print logs a message at level PrintLevel -func (l *Logger) Print(args ...interface{}) { +func (l *Logger) Print(args ...any) { l.log(PrintLevel, args) } // Println logs a message at level PrintLevel -func (l *Logger) Println(args ...interface{}) { +func (l *Logger) Println(args ...any) { l.log(PrintLevel, args) } // Printf logs a message at level PrintLevel -func (l *Logger) Printf(format string, args ...interface{}) { +func (l *Logger) Printf(format string, args ...any) { l.logf(PrintLevel, format, args) } // Warn logs a message at level Warn -func (l *Logger) Warn(args ...interface{}) { +func (l *Logger) Warn(args ...any) { l.log(WarnLevel, args) } // Warnf logs a message at level Warn -func (l *Logger) Warnf(format string, args ...interface{}) { +func (l *Logger) Warnf(format string, args ...any) { l.logf(WarnLevel, format, args) } // Warning logs a message at level Warn -func (l *Logger) Warning(args ...interface{}) { +func (l *Logger) Warning(args ...any) { l.log(WarnLevel, args) } // Info logs a message at level Info -func (l *Logger) Info(args ...interface{}) { +func (l *Logger) Info(args ...any) { l.log(InfoLevel, args) } // Infof logs a message at level Info -func (l *Logger) Infof(format string, args ...interface{}) { +func (l *Logger) Infof(format string, args ...any) { l.logf(InfoLevel, format, args) } // Trace logs a message at level Trace -func (l *Logger) Trace(args ...interface{}) { +func (l *Logger) Trace(args ...any) { l.log(TraceLevel, args) } // Tracef logs a message at level Trace -func (l *Logger) Tracef(format string, args ...interface{}) { +func (l *Logger) Tracef(format string, args ...any) { l.logf(TraceLevel, format, args) } // Error logs a message at level error -func (l *Logger) Error(args ...interface{}) { +func (l *Logger) Error(args ...any) { l.log(ErrorLevel, args) } // Errorf logs a message at level Error -func (l *Logger) Errorf(format string, args ...interface{}) { +func (l *Logger) Errorf(format string, args ...any) { l.logf(ErrorLevel, format, args) } // ErrorT logs a error type at level Error func (l *Logger) ErrorT(err error) { if err != nil { - l.log(ErrorLevel, []interface{}{err}) + l.log(ErrorLevel, []any{err}) } } // Notice logs a message at level Notice -func (l *Logger) Notice(args ...interface{}) { +func (l *Logger) Notice(args ...any) { l.log(NoticeLevel, args) } // Noticef logs a message at level Notice -func (l *Logger) Noticef(format string, args ...interface{}) { +func (l *Logger) Noticef(format string, args ...any) { l.logf(NoticeLevel, format, args) } // Debug logs a message at level Debug -func (l *Logger) Debug(args ...interface{}) { +func (l *Logger) Debug(args ...any) { l.log(DebugLevel, args) } // Debugf logs a message at level Debug -func (l *Logger) Debugf(format string, args ...interface{}) { +func (l *Logger) Debugf(format string, args ...any) { l.logf(DebugLevel, format, args) } // Fatal logs a message at level Fatal -func (l *Logger) Fatal(args ...interface{}) { +func (l *Logger) Fatal(args ...any) { l.log(FatalLevel, args) } // Fatalf logs a message at level Fatal -func (l *Logger) Fatalf(format string, args ...interface{}) { +func (l *Logger) Fatalf(format string, args ...any) { l.logf(FatalLevel, format, args) } // Fatalln logs a message at level Fatal -func (l *Logger) Fatalln(args ...interface{}) { +func (l *Logger) Fatalln(args ...any) { l.log(FatalLevel, args) } // Panic logs a message at level Panic -func (l *Logger) Panic(args ...interface{}) { +func (l *Logger) Panic(args ...any) { l.log(PanicLevel, args) } // Panicf logs a message at level Panic -func (l *Logger) Panicf(format string, args ...interface{}) { +func (l *Logger) Panicf(format string, args ...any) { l.logf(PanicLevel, format, args) } // Panicln logs a message at level Panic -func (l *Logger) Panicln(args ...interface{}) { +func (l *Logger) Panicln(args ...any) { l.log(PanicLevel, args) } diff --git a/record.go b/record.go index af0bb9c..b3654a6 100644 --- a/record.go +++ b/record.go @@ -103,7 +103,7 @@ func (r *Record) WithData(data M) *Record { } // WithField with a new field to record -func (r *Record) WithField(name string, val interface{}) *Record { +func (r *Record) WithField(name string, val any) *Record { return r.WithFields(M{name: val}) } @@ -184,7 +184,7 @@ func (r *Record) AddData(data M) *Record { } // AddValue add Data value to record -func (r *Record) AddValue(key string, value interface{}) *Record { +func (r *Record) AddValue(key string, value any) *Record { if r.Data == nil { r.Data = make(M, 8) return r @@ -214,7 +214,7 @@ func (r *Record) AddExtra(data M) *Record { } // SetExtraValue on record -func (r *Record) SetExtraValue(k string, v interface{}) { +func (r *Record) SetExtraValue(k string, v any) { if r.Extra == nil { r.Extra = make(M, 8) } @@ -228,7 +228,7 @@ func (r *Record) SetTime(t time.Time) *Record { } // AddField add new field to the record -func (r *Record) AddField(name string, val interface{}) *Record { +func (r *Record) AddField(name string, val any) *Record { if r.Fields == nil { r.Fields = make(M, 8) } @@ -283,130 +283,130 @@ func (r *Record) SetFields(fields M) *Record { // --------------------------------------------------------------------------- // -func (r *Record) log(level Level, args []interface{}) { +func (r *Record) log(level Level, args []any) { // will reduce memory allocation once // r.Message = strutil.Byte2str(formatArgsWithSpaces(args)) r.Message = formatArgsWithSpaces(args) r.logBytes(level) } -func (r *Record) logf(level Level, format string, args []interface{}) { +func (r *Record) logf(level Level, format string, args []any) { r.Message = fmt.Sprintf(format, args...) r.logBytes(level) } // Log a message with level -func (r *Record) Log(level Level, args ...interface{}) { +func (r *Record) Log(level Level, args ...any) { r.log(level, args) } // Logf a message with level -func (r *Record) Logf(level Level, format string, args ...interface{}) { +func (r *Record) Logf(level Level, format string, args ...any) { r.logf(level, format, args) } // Info logs a message at level Info -func (r *Record) Info(args ...interface{}) { +func (r *Record) Info(args ...any) { r.log(InfoLevel, args) } // Infof logs a message at level Info -func (r *Record) Infof(format string, args ...interface{}) { +func (r *Record) Infof(format string, args ...any) { r.logf(InfoLevel, format, args) } // Trace logs a message at level Trace -func (r *Record) Trace(args ...interface{}) { +func (r *Record) Trace(args ...any) { r.log(TraceLevel, args) } // Tracef logs a message at level Trace -func (r *Record) Tracef(format string, args ...interface{}) { +func (r *Record) Tracef(format string, args ...any) { r.logf(TraceLevel, format, args) } // Error logs a message at level Error -func (r *Record) Error(args ...interface{}) { +func (r *Record) Error(args ...any) { r.log(ErrorLevel, args) } // Errorf logs a message at level Error -func (r *Record) Errorf(format string, args ...interface{}) { +func (r *Record) Errorf(format string, args ...any) { r.logf(ErrorLevel, format, args) } // Warn logs a message at level Warn -func (r *Record) Warn(args ...interface{}) { +func (r *Record) Warn(args ...any) { r.log(WarnLevel, args) } // Warnf logs a message at level Warn -func (r *Record) Warnf(format string, args ...interface{}) { +func (r *Record) Warnf(format string, args ...any) { r.logf(ErrorLevel, format, args) } // Notice logs a message at level Notice -func (r *Record) Notice(args ...interface{}) { +func (r *Record) Notice(args ...any) { r.log(NoticeLevel, args) } // Noticef logs a message at level Notice -func (r *Record) Noticef(format string, args ...interface{}) { +func (r *Record) Noticef(format string, args ...any) { r.logf(NoticeLevel, format, args) } // Debug logs a message at level Debug -func (r *Record) Debug(args ...interface{}) { +func (r *Record) Debug(args ...any) { r.log(DebugLevel, args) } // Debugf logs a message at level Debug -func (r *Record) Debugf(format string, args ...interface{}) { +func (r *Record) Debugf(format string, args ...any) { r.logf(DebugLevel, format, args) } // Print logs a message at level Print -func (r *Record) Print(args ...interface{}) { +func (r *Record) Print(args ...any) { r.log(PrintLevel, args) } // Println logs a message at level Print, will not append \n. alias of Print -func (r *Record) Println(args ...interface{}) { +func (r *Record) Println(args ...any) { r.log(PrintLevel, args) } // Printf logs a message at level Print -func (r *Record) Printf(format string, args ...interface{}) { +func (r *Record) Printf(format string, args ...any) { r.logf(PrintLevel, format, args) } // Fatal logs a message at level Fatal -func (r *Record) Fatal(args ...interface{}) { +func (r *Record) Fatal(args ...any) { r.log(FatalLevel, args) } // Fatalln logs a message at level Fatal, will not append \n. -func (r *Record) Fatalln(args ...interface{}) { +func (r *Record) Fatalln(args ...any) { r.log(FatalLevel, args) } // Fatalf logs a message at level Fatal -func (r *Record) Fatalf(format string, args ...interface{}) { +func (r *Record) Fatalf(format string, args ...any) { r.logf(FatalLevel, format, args) } // Panic logs a message at level Panic -func (r *Record) Panic(args ...interface{}) { +func (r *Record) Panic(args ...any) { r.log(PanicLevel, args) } // Panicln logs a message at level Panic, will not append \n. -func (r *Record) Panicln(args ...interface{}) { +func (r *Record) Panicln(args ...any) { r.log(PanicLevel, args) } // Panicf logs a message at level Panic -func (r *Record) Panicf(format string, args ...interface{}) { +func (r *Record) Panicf(format string, args ...any) { r.logf(PanicLevel, format, args) } diff --git a/rotatefile/rotatefile.go b/rotatefile/rotatefile.go index e3366ad..05f0140 100644 --- a/rotatefile/rotatefile.go +++ b/rotatefile/rotatefile.go @@ -15,6 +15,6 @@ type RotateWriter interface { Sync() error } -func printErrln(args ...interface{}) { +func printErrln(args ...any) { _, _ = fmt.Fprintln(os.Stderr, args...) } diff --git a/slog.go b/slog.go index f135792..cc46f63 100644 --- a/slog.go +++ b/slog.go @@ -21,7 +21,6 @@ Quick usage: } More usage please see README. - */ package slog @@ -85,7 +84,8 @@ func FlushTimeout(timeout time.Duration) { std.FlushTimeout(timeout) } // FlushDaemon run flush handle on daemon // // Usage: -// go slog.FlushDaemon() +// +// go slog.FlushDaemon() func FlushDaemon() { std.FlushDaemon() } // SetLogLevel for the std logger @@ -130,107 +130,107 @@ func WithFields(fields M) *Record { // -------------------------- Add log messages with level ----------------------------- // Print logs a message at level PrintLevel -func Print(args ...interface{}) { std.log(PrintLevel, args) } +func Print(args ...any) { std.log(PrintLevel, args) } // Println logs a message at level PrintLevel -func Println(args ...interface{}) { std.log(PrintLevel, args) } +func Println(args ...any) { std.log(PrintLevel, args) } // Printf logs a message at level PrintLevel -func Printf(format string, args ...interface{}) { std.logf(PrintLevel, format, args) } +func Printf(format string, args ...any) { std.logf(PrintLevel, format, args) } // Trace logs a message at level Trace -func Trace(args ...interface{}) { std.log(TraceLevel, args) } +func Trace(args ...any) { std.log(TraceLevel, args) } // Tracef logs a message at level Trace -func Tracef(format string, args ...interface{}) { std.logf(TraceLevel, format, args) } +func Tracef(format string, args ...any) { std.logf(TraceLevel, format, args) } // Info logs a message at level Info -func Info(args ...interface{}) { +func Info(args ...any) { std.log(InfoLevel, args) } // Infof logs a message at level Info -func Infof(format string, args ...interface{}) { +func Infof(format string, args ...any) { std.logf(InfoLevel, format, args) } // Notice logs a message at level Notice -func Notice(args ...interface{}) { +func Notice(args ...any) { std.log(NoticeLevel, args) } // Noticef logs a message at level Notice -func Noticef(format string, args ...interface{}) { +func Noticef(format string, args ...any) { std.logf(NoticeLevel, format, args) } // Warn logs a message at level Warn -func Warn(args ...interface{}) { +func Warn(args ...any) { std.log(WarnLevel, args) } // Warnf logs a message at level Warn -func Warnf(format string, args ...interface{}) { +func Warnf(format string, args ...any) { std.logf(WarnLevel, format, args) } // Error logs a message at level Error -func Error(args ...interface{}) { +func Error(args ...any) { std.log(ErrorLevel, args) } // ErrorT logs a error type at level Error func ErrorT(err error) { if err != nil { - std.log(ErrorLevel, []interface{}{err}) + std.log(ErrorLevel, []any{err}) } } // Errorf logs a message at level Error -func Errorf(format string, args ...interface{}) { +func Errorf(format string, args ...any) { std.logf(ErrorLevel, format, args) } // Debug logs a message at level Debug -func Debug(args ...interface{}) { +func Debug(args ...any) { std.log(DebugLevel, args) } // Debugf logs a message at level Debug -func Debugf(format string, args ...interface{}) { +func Debugf(format string, args ...any) { std.logf(DebugLevel, format, args) } // Fatal logs a message at level Fatal -func Fatal(args ...interface{}) { +func Fatal(args ...any) { std.log(FatalLevel, args) } // Fatalf logs a message at level Fatal -func Fatalf(format string, args ...interface{}) { +func Fatalf(format string, args ...any) { std.logf(FatalLevel, format, args) } // FatalErr logs a message at level Fatal on err is not nil func FatalErr(err error) { if err != nil { - std.log(FatalLevel, []interface{}{err}) + std.log(FatalLevel, []any{err}) } } // Panic logs a message at level Panic -func Panic(args ...interface{}) { +func Panic(args ...any) { std.log(PanicLevel, args) } // Panicf logs a message at level Panic -func Panicf(format string, args ...interface{}) { +func Panicf(format string, args ...any) { std.logf(PanicLevel, format, args) } // PanicErr logs a message at level Panic on err is not nil func PanicErr(err error) { if err != nil { - std.log(PanicLevel, []interface{}{err}) + std.log(PanicLevel, []any{err}) } } diff --git a/util.go b/util.go index 910a6d3..d6f919e 100644 --- a/util.go +++ b/util.go @@ -73,7 +73,7 @@ func formatCaller(rf *runtime.Frame, flag uint8) (cs string) { } // it like Println, will add spaces for each argument -func formatArgsWithSpaces(vs []interface{}) string { +func formatArgsWithSpaces(vs []any) string { ln := len(vs) if ln == 0 { return "" @@ -107,15 +107,15 @@ func formatArgsWithSpaces(vs []interface{}) string { } // EncodeToString data to string -func EncodeToString(v interface{}) string { - if mp, ok := v.(map[string]interface{}); ok { +func EncodeToString(v any) string { + if mp, ok := v.(map[string]any); ok { return mapToString(mp) } return stdutil.ToString(v) } -func mapToString(mp map[string]interface{}) string { +func mapToString(mp map[string]any) string { ln := len(mp) if ln == 0 { return "{}" @@ -159,6 +159,6 @@ func parseTemplateToFields(tplStr string) []string { return vars } -func printlnStderr(args ...interface{}) { +func printlnStderr(args ...any) { _, _ = fmt.Fprintln(os.Stderr, args...) }