diff --git a/internal/log/glg/glg.go b/internal/log/glg/glg.go index 45f32f5a870..b17376f0daa 100644 --- a/internal/log/glg/glg.go +++ b/internal/log/glg/glg.go @@ -73,6 +73,10 @@ func (l *logger) setLogFormat(fmt format.Format) *logger { return l } +func (l *logger) Close() error { + return nil +} + func (l *logger) Info(vals ...interface{}) { l.retry.Out(l.glg.Info, vals...) } @@ -81,6 +85,10 @@ func (l *logger) Infof(format string, vals ...interface{}) { l.retry.Outf(l.glg.Infof, format, vals...) } +func (l *logger) Infod(msg string, details ...interface{}) { + l.Infof(msg+"\tdetails: %#v", details) +} + func (l *logger) Debug(vals ...interface{}) { l.retry.Out(l.glg.Debug, vals...) } @@ -89,6 +97,10 @@ func (l *logger) Debugf(format string, vals ...interface{}) { l.retry.Outf(l.glg.Debugf, format, vals...) } +func (l *logger) Debugd(msg string, details ...interface{}) { + l.Debugf(msg+"\tdetails: %#v", details) +} + func (l *logger) Warn(vals ...interface{}) { l.retry.Out(l.glg.Warn, vals...) } @@ -97,6 +109,10 @@ func (l *logger) Warnf(format string, vals ...interface{}) { l.retry.Outf(l.glg.Warnf, format, vals...) } +func (l *logger) Warnd(msg string, details ...interface{}) { + l.Warnf(msg+"\tdetails: %#v", details) +} + func (l *logger) Error(vals ...interface{}) { l.retry.Out(l.glg.Error, vals...) } @@ -105,6 +121,10 @@ func (l *logger) Errorf(format string, vals ...interface{}) { l.retry.Outf(l.glg.Errorf, format, vals...) } +func (l *logger) Errord(msg string, details ...interface{}) { + l.Errorf(msg+"\tdetails: %#v", details) +} + func (l *logger) Fatal(vals ...interface{}) { l.glg.Fatal(vals...) } @@ -112,3 +132,7 @@ func (l *logger) Fatal(vals ...interface{}) { func (l *logger) Fatalf(format string, vals ...interface{}) { l.glg.Fatalf(format, vals...) } + +func (l *logger) Fatald(msg string, details ...interface{}) { + l.Fatalf(msg+"\tdetails: %#v", details) +} diff --git a/internal/log/glg/glg_test.go b/internal/log/glg/glg_test.go index 113aa7d741b..4e781a6b6a3 100644 --- a/internal/log/glg/glg_test.go +++ b/internal/log/glg/glg_test.go @@ -2016,3 +2016,577 @@ func Test_logger_Fatalf(t *testing.T) { }) } } + +func Test_logger_Infod(t *testing.T) { + t.Parallel() + type args struct { + msg string + details []interface{} + } + type fields struct { + format format.Format + level level.Level + retry retry.Retry + glg *glg.Glg + } + type want struct { + } + type test struct { + name string + args args + fields fields + want want + checkFunc func(want) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want) error { + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + args: args { + msg: "", + details: nil, + }, + fields: fields { + format: nil, + level: nil, + retry: nil, + glg: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + msg: "", + details: nil, + }, + fields: fields { + format: nil, + level: nil, + retry: nil, + glg: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + for _, tc := range tests { + test := tc + t.Run(test.name, func(tt *testing.T) { + tt.Parallel() + defer goleak.VerifyNone(tt, goleak.IgnoreCurrent()) + if test.beforeFunc != nil { + test.beforeFunc(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + l := &logger{ + format: test.fields.format, + level: test.fields.level, + retry: test.fields.retry, + glg: test.fields.glg, + } + + l.Infod(test.args.msg, test.args.details...) + if err := test.checkFunc(test.want); err != nil { + tt.Errorf("error = %v", err) + } + }) + } +} + +func Test_logger_Debugd(t *testing.T) { + t.Parallel() + type args struct { + msg string + details []interface{} + } + type fields struct { + format format.Format + level level.Level + retry retry.Retry + glg *glg.Glg + } + type want struct { + } + type test struct { + name string + args args + fields fields + want want + checkFunc func(want) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want) error { + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + args: args { + msg: "", + details: nil, + }, + fields: fields { + format: nil, + level: nil, + retry: nil, + glg: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + msg: "", + details: nil, + }, + fields: fields { + format: nil, + level: nil, + retry: nil, + glg: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + for _, tc := range tests { + test := tc + t.Run(test.name, func(tt *testing.T) { + tt.Parallel() + defer goleak.VerifyNone(tt, goleak.IgnoreCurrent()) + if test.beforeFunc != nil { + test.beforeFunc(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + l := &logger{ + format: test.fields.format, + level: test.fields.level, + retry: test.fields.retry, + glg: test.fields.glg, + } + + l.Debugd(test.args.msg, test.args.details...) + if err := test.checkFunc(test.want); err != nil { + tt.Errorf("error = %v", err) + } + }) + } +} + +func Test_logger_Warnd(t *testing.T) { + t.Parallel() + type args struct { + msg string + details []interface{} + } + type fields struct { + format format.Format + level level.Level + retry retry.Retry + glg *glg.Glg + } + type want struct { + } + type test struct { + name string + args args + fields fields + want want + checkFunc func(want) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want) error { + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + args: args { + msg: "", + details: nil, + }, + fields: fields { + format: nil, + level: nil, + retry: nil, + glg: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + msg: "", + details: nil, + }, + fields: fields { + format: nil, + level: nil, + retry: nil, + glg: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + for _, tc := range tests { + test := tc + t.Run(test.name, func(tt *testing.T) { + tt.Parallel() + defer goleak.VerifyNone(tt, goleak.IgnoreCurrent()) + if test.beforeFunc != nil { + test.beforeFunc(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + l := &logger{ + format: test.fields.format, + level: test.fields.level, + retry: test.fields.retry, + glg: test.fields.glg, + } + + l.Warnd(test.args.msg, test.args.details...) + if err := test.checkFunc(test.want); err != nil { + tt.Errorf("error = %v", err) + } + }) + } +} + +func Test_logger_Errord(t *testing.T) { + t.Parallel() + type args struct { + msg string + details []interface{} + } + type fields struct { + format format.Format + level level.Level + retry retry.Retry + glg *glg.Glg + } + type want struct { + } + type test struct { + name string + args args + fields fields + want want + checkFunc func(want) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want) error { + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + args: args { + msg: "", + details: nil, + }, + fields: fields { + format: nil, + level: nil, + retry: nil, + glg: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + msg: "", + details: nil, + }, + fields: fields { + format: nil, + level: nil, + retry: nil, + glg: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + for _, tc := range tests { + test := tc + t.Run(test.name, func(tt *testing.T) { + tt.Parallel() + defer goleak.VerifyNone(tt, goleak.IgnoreCurrent()) + if test.beforeFunc != nil { + test.beforeFunc(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + l := &logger{ + format: test.fields.format, + level: test.fields.level, + retry: test.fields.retry, + glg: test.fields.glg, + } + + l.Errord(test.args.msg, test.args.details...) + if err := test.checkFunc(test.want); err != nil { + tt.Errorf("error = %v", err) + } + }) + } +} + +func Test_logger_Fatald(t *testing.T) { + t.Parallel() + type args struct { + msg string + details []interface{} + } + type fields struct { + format format.Format + level level.Level + retry retry.Retry + glg *glg.Glg + } + type want struct { + } + type test struct { + name string + args args + fields fields + want want + checkFunc func(want) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want) error { + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + args: args { + msg: "", + details: nil, + }, + fields: fields { + format: nil, + level: nil, + retry: nil, + glg: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + msg: "", + details: nil, + }, + fields: fields { + format: nil, + level: nil, + retry: nil, + glg: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + for _, tc := range tests { + test := tc + t.Run(test.name, func(tt *testing.T) { + tt.Parallel() + defer goleak.VerifyNone(tt, goleak.IgnoreCurrent()) + if test.beforeFunc != nil { + test.beforeFunc(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + l := &logger{ + format: test.fields.format, + level: test.fields.level, + retry: test.fields.retry, + glg: test.fields.glg, + } + + l.Fatald(test.args.msg, test.args.details...) + if err := test.checkFunc(test.want); err != nil { + tt.Errorf("error = %v", err) + } + }) + } +} + +func Test_logger_Close(t *testing.T) { + t.Parallel() + type fields struct { + format format.Format + level level.Level + retry retry.Retry + glg *glg.Glg + } + type want struct { + err error + } + type test struct { + name string + fields fields + want want + checkFunc func(want, error) error + beforeFunc func() + afterFunc func() + } + defaultCheckFunc := func(w want, err error) error { + if !errors.Is(err, w.err) { + return errors.Errorf("got_error: \"%#v\",\n\t\t\t\twant: \"%#v\"", err, w.err) + } + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + fields: fields { + format: nil, + level: nil, + retry: nil, + glg: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + fields: fields { + format: nil, + level: nil, + retry: nil, + glg: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + for _, tc := range tests { + test := tc + t.Run(test.name, func(tt *testing.T) { + tt.Parallel() + defer goleak.VerifyNone(tt, goleak.IgnoreCurrent()) + if test.beforeFunc != nil { + test.beforeFunc() + } + if test.afterFunc != nil { + defer test.afterFunc() + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + l := &logger{ + format: test.fields.format, + level: test.fields.level, + retry: test.fields.retry, + glg: test.fields.glg, + } + + err := l.Close() + if err := test.checkFunc(test.want, err); err != nil { + tt.Errorf("error = %v", err) + } + + }) + } +} diff --git a/internal/log/log.go b/internal/log/log.go index 487f1a29883..c9583cf6bf4 100644 --- a/internal/log/log.go +++ b/internal/log/log.go @@ -39,6 +39,10 @@ func Init(opts ...Option) { }) } +func Close() error { + return l.Close() +} + func getLogger(o *option) logger.Logger { switch o.logType { case logger.ZAP: @@ -75,6 +79,10 @@ func Debugf(format string, vals ...interface{}) { l.Debugf(format, vals...) } +func Debugd(msg string, details ...interface{}) { + l.Debugd(msg, details...) +} + func Info(vals ...interface{}) { l.Info(vals...) } @@ -83,6 +91,10 @@ func Infof(format string, vals ...interface{}) { l.Infof(format, vals...) } +func Infod(msg string, details ...interface{}) { + l.Infod(msg, details...) +} + func Warn(vals ...interface{}) { l.Warn(vals...) } @@ -91,6 +103,10 @@ func Warnf(format string, vals ...interface{}) { l.Warnf(format, vals...) } +func Warnd(msg string, details ...interface{}) { + l.Warnd(msg, details...) +} + func Error(vals ...interface{}) { l.Error(vals...) } @@ -99,6 +115,10 @@ func Errorf(format string, vals ...interface{}) { l.Errorf(format, vals...) } +func Errord(msg string, details ...interface{}) { + l.Errord(msg, details...) +} + func Fatal(vals ...interface{}) { l.Fatal(vals...) } @@ -106,3 +126,7 @@ func Fatal(vals ...interface{}) { func Fatalf(format string, vals ...interface{}) { l.Fatalf(format, vals...) } + +func Fatald(msg string, details ...interface{}) { + l.Fatald(msg, details...) +} diff --git a/internal/log/log_test.go b/internal/log/log_test.go index e3ad9b5648d..28a16011b6a 100644 --- a/internal/log/log_test.go +++ b/internal/log/log_test.go @@ -25,6 +25,7 @@ import ( "github.com/vdaas/vald/internal/log/glg" "github.com/vdaas/vald/internal/log/level" "github.com/vdaas/vald/internal/log/logger" + logger "github.com/vdaas/vald/internal/log/logger" "github.com/vdaas/vald/internal/log/mock" "go.uber.org/goleak" ) @@ -1012,3 +1013,427 @@ func TestFatalf(t *testing.T) { }) } } + +func TestDebugd(t *testing.T) { + t.Parallel() + type args struct { + msg string + details []interface{} + } + type want struct { + } + type test struct { + name string + args args + want want + checkFunc func(want) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want) error { + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + args: args { + msg: "", + details: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + msg: "", + details: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + for _, tc := range tests { + test := tc + t.Run(test.name, func(tt *testing.T) { + tt.Parallel() + defer goleak.VerifyNone(tt, goleak.IgnoreCurrent()) + if test.beforeFunc != nil { + test.beforeFunc(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + + Debugd(test.args.msg, test.args.details...) + if err := test.checkFunc(test.want); err != nil { + tt.Errorf("error = %v", err) + } + }) + } +} + +func TestInfod(t *testing.T) { + t.Parallel() + type args struct { + msg string + details []interface{} + } + type want struct { + } + type test struct { + name string + args args + want want + checkFunc func(want) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want) error { + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + args: args { + msg: "", + details: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + msg: "", + details: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + for _, tc := range tests { + test := tc + t.Run(test.name, func(tt *testing.T) { + tt.Parallel() + defer goleak.VerifyNone(tt, goleak.IgnoreCurrent()) + if test.beforeFunc != nil { + test.beforeFunc(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + + Infod(test.args.msg, test.args.details...) + if err := test.checkFunc(test.want); err != nil { + tt.Errorf("error = %v", err) + } + }) + } +} + +func TestWarnd(t *testing.T) { + t.Parallel() + type args struct { + msg string + details []interface{} + } + type want struct { + } + type test struct { + name string + args args + want want + checkFunc func(want) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want) error { + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + args: args { + msg: "", + details: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + msg: "", + details: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + for _, tc := range tests { + test := tc + t.Run(test.name, func(tt *testing.T) { + tt.Parallel() + defer goleak.VerifyNone(tt, goleak.IgnoreCurrent()) + if test.beforeFunc != nil { + test.beforeFunc(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + + Warnd(test.args.msg, test.args.details...) + if err := test.checkFunc(test.want); err != nil { + tt.Errorf("error = %v", err) + } + }) + } +} + +func TestErrord(t *testing.T) { + t.Parallel() + type args struct { + msg string + details []interface{} + } + type want struct { + } + type test struct { + name string + args args + want want + checkFunc func(want) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want) error { + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + args: args { + msg: "", + details: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + msg: "", + details: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + for _, tc := range tests { + test := tc + t.Run(test.name, func(tt *testing.T) { + tt.Parallel() + defer goleak.VerifyNone(tt, goleak.IgnoreCurrent()) + if test.beforeFunc != nil { + test.beforeFunc(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + + Errord(test.args.msg, test.args.details...) + if err := test.checkFunc(test.want); err != nil { + tt.Errorf("error = %v", err) + } + }) + } +} + +func TestFatald(t *testing.T) { + t.Parallel() + type args struct { + msg string + details []interface{} + } + type want struct { + } + type test struct { + name string + args args + want want + checkFunc func(want) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want) error { + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + args: args { + msg: "", + details: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + msg: "", + details: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + for _, tc := range tests { + test := tc + t.Run(test.name, func(tt *testing.T) { + tt.Parallel() + defer goleak.VerifyNone(tt, goleak.IgnoreCurrent()) + if test.beforeFunc != nil { + test.beforeFunc(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + + Fatald(test.args.msg, test.args.details...) + if err := test.checkFunc(test.want); err != nil { + tt.Errorf("error = %v", err) + } + }) + } +} + +func TestClose(t *testing.T) { + t.Parallel() + type want struct { + err error + } + type test struct { + name string + want want + checkFunc func(want, error) error + beforeFunc func() + afterFunc func() + } + defaultCheckFunc := func(w want, err error) error { + if !errors.Is(err, w.err) { + return errors.Errorf("got_error: \"%#v\",\n\t\t\t\twant: \"%#v\"", err, w.err) + } + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + for _, tc := range tests { + test := tc + t.Run(test.name, func(tt *testing.T) { + tt.Parallel() + defer goleak.VerifyNone(tt, goleak.IgnoreCurrent()) + if test.beforeFunc != nil { + test.beforeFunc() + } + if test.afterFunc != nil { + defer test.afterFunc() + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + + err := Close() + if err := test.checkFunc(test.want, err); err != nil { + tt.Errorf("error = %v", err) + } + + }) + } +} diff --git a/internal/log/logger/iface.go b/internal/log/logger/iface.go index 972d2a15aaf..2472446c67c 100644 --- a/internal/log/logger/iface.go +++ b/internal/log/logger/iface.go @@ -17,14 +17,51 @@ package logger type Logger interface { + // Debug logs the vals at Debug level. Debug(vals ...interface{}) + + // Debugf logs the formatted message at Debug level. Debugf(format string, vals ...interface{}) + + // Debugd logs the message with details at Debug level. + Debugd(msg string, details ...interface{}) + + // Info logs the vals at Info level. Info(vals ...interface{}) + + // Infof logs the formatted message at Info level. Infof(format string, vals ...interface{}) + + // Infod logs the message with details at Info level. + Infod(msg string, details ...interface{}) + + // Warn logs the vals at Warn level. Warn(vals ...interface{}) + + // Warnf logs the formatted message at Warn level. Warnf(format string, vals ...interface{}) + + // Warnd logs the message with details at Warn level. + Warnd(msg string, details ...interface{}) + + // Error logs the vals at Error level. Error(vals ...interface{}) + + // Errorf logs the formatted message at Error level. Errorf(format string, vals ...interface{}) + + // Errord logs the message with details at Error level. + Errord(msg string, details ...interface{}) + + // Fatal logs the vals at Fatal level, then calls os.Exit(1). Fatal(vals ...interface{}) + + // Fatalf logs the formatted message at Fatal level, then calls os.Exit(1). Fatalf(format string, vals ...interface{}) + + // Fatald logs the message with details at Fatal level, then calls os.Exit(1). + Fatald(msg string, details ...interface{}) + + // Close calls finalizer of logger implementations. + Close() error } diff --git a/internal/log/mock/logger.go b/internal/log/mock/logger.go index ba98fcf9042..efdb040fa41 100644 --- a/internal/log/mock/logger.go +++ b/internal/log/mock/logger.go @@ -39,6 +39,11 @@ func (l *Logger) Debugf(format string, vals ...interface{}) { l.DebugfFunc(format, vals...) } +// Debugd calls DebugfFunc of Logger. +func (l *Logger) Debugd(msg string, details ...interface{}) { + l.DebugfFunc(msg, details...) +} + // Info calls InfoFunc of Logger. func (l *Logger) Info(vals ...interface{}) { l.InfoFunc(vals...) @@ -49,6 +54,11 @@ func (l *Logger) Infof(format string, vals ...interface{}) { l.InfofFunc(format, vals...) } +// Infod calls InfofFunc of Logger. +func (l *Logger) Infod(msg string, details ...interface{}) { + l.InfofFunc(msg, details...) +} + // Warn calls WarnFunc of Logger. func (l *Logger) Warn(vals ...interface{}) { l.WarnFunc(vals...) @@ -59,6 +69,11 @@ func (l *Logger) Warnf(format string, vals ...interface{}) { l.WarnfFunc(format, vals...) } +// Warnd calls WarnfFunc of Logger. +func (l *Logger) Warnd(msg string, details ...interface{}) { + l.WarnfFunc(msg, details...) +} + // Error calls ErrorFunc of Logger. func (l *Logger) Error(vals ...interface{}) { l.ErrorFunc(vals...) @@ -69,6 +84,11 @@ func (l *Logger) Errorf(format string, vals ...interface{}) { l.ErrorfFunc(format, vals...) } +// Errord calls ErrorfFunc of Logger. +func (l *Logger) Errord(msg string, details ...interface{}) { + l.ErrorfFunc(msg, details...) +} + // Fatal calls FatalFunc of Logger. func (l *Logger) Fatal(vals ...interface{}) { l.FatalFunc(vals...) @@ -78,3 +98,12 @@ func (l *Logger) Fatal(vals ...interface{}) { func (l *Logger) Fatalf(format string, vals ...interface{}) { l.FatalfFunc(format, vals...) } + +// Fatald calls FatalfFunc of Logger. +func (l *Logger) Fatald(msg string, details ...interface{}) { + l.FatalfFunc(msg, details...) +} + +func (l *Logger) Close() error { + return nil +} diff --git a/internal/log/mock/logger_test.go b/internal/log/mock/logger_test.go index d5209556dd3..5013ff6aec0 100644 --- a/internal/log/mock/logger_test.go +++ b/internal/log/mock/logger_test.go @@ -807,3 +807,721 @@ func TestLogger_Fatalf(t *testing.T) { }) } } + +func TestLogger_Debugd(t *testing.T) { + t.Parallel() + type args struct { + msg string + details []interface{} + } + type fields struct { + DebugFunc func(vals ...interface{}) + DebugfFunc func(format string, vals ...interface{}) + InfoFunc func(vals ...interface{}) + InfofFunc func(format string, vals ...interface{}) + WarnFunc func(vals ...interface{}) + WarnfFunc func(format string, vals ...interface{}) + ErrorFunc func(vals ...interface{}) + ErrorfFunc func(format string, vals ...interface{}) + FatalFunc func(vals ...interface{}) + FatalfFunc func(format string, vals ...interface{}) + } + type want struct { + } + type test struct { + name string + args args + fields fields + want want + checkFunc func(want) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want) error { + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + args: args { + msg: "", + details: nil, + }, + fields: fields { + DebugFunc: nil, + DebugfFunc: nil, + InfoFunc: nil, + InfofFunc: nil, + WarnFunc: nil, + WarnfFunc: nil, + ErrorFunc: nil, + ErrorfFunc: nil, + FatalFunc: nil, + FatalfFunc: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + msg: "", + details: nil, + }, + fields: fields { + DebugFunc: nil, + DebugfFunc: nil, + InfoFunc: nil, + InfofFunc: nil, + WarnFunc: nil, + WarnfFunc: nil, + ErrorFunc: nil, + ErrorfFunc: nil, + FatalFunc: nil, + FatalfFunc: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + for _, tc := range tests { + test := tc + t.Run(test.name, func(tt *testing.T) { + tt.Parallel() + defer goleak.VerifyNone(tt, goleak.IgnoreCurrent()) + if test.beforeFunc != nil { + test.beforeFunc(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + l := &Logger{ + DebugFunc: test.fields.DebugFunc, + DebugfFunc: test.fields.DebugfFunc, + InfoFunc: test.fields.InfoFunc, + InfofFunc: test.fields.InfofFunc, + WarnFunc: test.fields.WarnFunc, + WarnfFunc: test.fields.WarnfFunc, + ErrorFunc: test.fields.ErrorFunc, + ErrorfFunc: test.fields.ErrorfFunc, + FatalFunc: test.fields.FatalFunc, + FatalfFunc: test.fields.FatalfFunc, + } + + l.Debugd(test.args.msg, test.args.details...) + if err := test.checkFunc(test.want); err != nil { + tt.Errorf("error = %v", err) + } + }) + } +} + +func TestLogger_Infod(t *testing.T) { + t.Parallel() + type args struct { + msg string + details []interface{} + } + type fields struct { + DebugFunc func(vals ...interface{}) + DebugfFunc func(format string, vals ...interface{}) + InfoFunc func(vals ...interface{}) + InfofFunc func(format string, vals ...interface{}) + WarnFunc func(vals ...interface{}) + WarnfFunc func(format string, vals ...interface{}) + ErrorFunc func(vals ...interface{}) + ErrorfFunc func(format string, vals ...interface{}) + FatalFunc func(vals ...interface{}) + FatalfFunc func(format string, vals ...interface{}) + } + type want struct { + } + type test struct { + name string + args args + fields fields + want want + checkFunc func(want) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want) error { + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + args: args { + msg: "", + details: nil, + }, + fields: fields { + DebugFunc: nil, + DebugfFunc: nil, + InfoFunc: nil, + InfofFunc: nil, + WarnFunc: nil, + WarnfFunc: nil, + ErrorFunc: nil, + ErrorfFunc: nil, + FatalFunc: nil, + FatalfFunc: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + msg: "", + details: nil, + }, + fields: fields { + DebugFunc: nil, + DebugfFunc: nil, + InfoFunc: nil, + InfofFunc: nil, + WarnFunc: nil, + WarnfFunc: nil, + ErrorFunc: nil, + ErrorfFunc: nil, + FatalFunc: nil, + FatalfFunc: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + for _, tc := range tests { + test := tc + t.Run(test.name, func(tt *testing.T) { + tt.Parallel() + defer goleak.VerifyNone(tt, goleak.IgnoreCurrent()) + if test.beforeFunc != nil { + test.beforeFunc(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + l := &Logger{ + DebugFunc: test.fields.DebugFunc, + DebugfFunc: test.fields.DebugfFunc, + InfoFunc: test.fields.InfoFunc, + InfofFunc: test.fields.InfofFunc, + WarnFunc: test.fields.WarnFunc, + WarnfFunc: test.fields.WarnfFunc, + ErrorFunc: test.fields.ErrorFunc, + ErrorfFunc: test.fields.ErrorfFunc, + FatalFunc: test.fields.FatalFunc, + FatalfFunc: test.fields.FatalfFunc, + } + + l.Infod(test.args.msg, test.args.details...) + if err := test.checkFunc(test.want); err != nil { + tt.Errorf("error = %v", err) + } + }) + } +} + +func TestLogger_Warnd(t *testing.T) { + t.Parallel() + type args struct { + msg string + details []interface{} + } + type fields struct { + DebugFunc func(vals ...interface{}) + DebugfFunc func(format string, vals ...interface{}) + InfoFunc func(vals ...interface{}) + InfofFunc func(format string, vals ...interface{}) + WarnFunc func(vals ...interface{}) + WarnfFunc func(format string, vals ...interface{}) + ErrorFunc func(vals ...interface{}) + ErrorfFunc func(format string, vals ...interface{}) + FatalFunc func(vals ...interface{}) + FatalfFunc func(format string, vals ...interface{}) + } + type want struct { + } + type test struct { + name string + args args + fields fields + want want + checkFunc func(want) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want) error { + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + args: args { + msg: "", + details: nil, + }, + fields: fields { + DebugFunc: nil, + DebugfFunc: nil, + InfoFunc: nil, + InfofFunc: nil, + WarnFunc: nil, + WarnfFunc: nil, + ErrorFunc: nil, + ErrorfFunc: nil, + FatalFunc: nil, + FatalfFunc: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + msg: "", + details: nil, + }, + fields: fields { + DebugFunc: nil, + DebugfFunc: nil, + InfoFunc: nil, + InfofFunc: nil, + WarnFunc: nil, + WarnfFunc: nil, + ErrorFunc: nil, + ErrorfFunc: nil, + FatalFunc: nil, + FatalfFunc: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + for _, tc := range tests { + test := tc + t.Run(test.name, func(tt *testing.T) { + tt.Parallel() + defer goleak.VerifyNone(tt, goleak.IgnoreCurrent()) + if test.beforeFunc != nil { + test.beforeFunc(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + l := &Logger{ + DebugFunc: test.fields.DebugFunc, + DebugfFunc: test.fields.DebugfFunc, + InfoFunc: test.fields.InfoFunc, + InfofFunc: test.fields.InfofFunc, + WarnFunc: test.fields.WarnFunc, + WarnfFunc: test.fields.WarnfFunc, + ErrorFunc: test.fields.ErrorFunc, + ErrorfFunc: test.fields.ErrorfFunc, + FatalFunc: test.fields.FatalFunc, + FatalfFunc: test.fields.FatalfFunc, + } + + l.Warnd(test.args.msg, test.args.details...) + if err := test.checkFunc(test.want); err != nil { + tt.Errorf("error = %v", err) + } + }) + } +} + +func TestLogger_Errord(t *testing.T) { + t.Parallel() + type args struct { + msg string + details []interface{} + } + type fields struct { + DebugFunc func(vals ...interface{}) + DebugfFunc func(format string, vals ...interface{}) + InfoFunc func(vals ...interface{}) + InfofFunc func(format string, vals ...interface{}) + WarnFunc func(vals ...interface{}) + WarnfFunc func(format string, vals ...interface{}) + ErrorFunc func(vals ...interface{}) + ErrorfFunc func(format string, vals ...interface{}) + FatalFunc func(vals ...interface{}) + FatalfFunc func(format string, vals ...interface{}) + } + type want struct { + } + type test struct { + name string + args args + fields fields + want want + checkFunc func(want) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want) error { + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + args: args { + msg: "", + details: nil, + }, + fields: fields { + DebugFunc: nil, + DebugfFunc: nil, + InfoFunc: nil, + InfofFunc: nil, + WarnFunc: nil, + WarnfFunc: nil, + ErrorFunc: nil, + ErrorfFunc: nil, + FatalFunc: nil, + FatalfFunc: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + msg: "", + details: nil, + }, + fields: fields { + DebugFunc: nil, + DebugfFunc: nil, + InfoFunc: nil, + InfofFunc: nil, + WarnFunc: nil, + WarnfFunc: nil, + ErrorFunc: nil, + ErrorfFunc: nil, + FatalFunc: nil, + FatalfFunc: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + for _, tc := range tests { + test := tc + t.Run(test.name, func(tt *testing.T) { + tt.Parallel() + defer goleak.VerifyNone(tt, goleak.IgnoreCurrent()) + if test.beforeFunc != nil { + test.beforeFunc(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + l := &Logger{ + DebugFunc: test.fields.DebugFunc, + DebugfFunc: test.fields.DebugfFunc, + InfoFunc: test.fields.InfoFunc, + InfofFunc: test.fields.InfofFunc, + WarnFunc: test.fields.WarnFunc, + WarnfFunc: test.fields.WarnfFunc, + ErrorFunc: test.fields.ErrorFunc, + ErrorfFunc: test.fields.ErrorfFunc, + FatalFunc: test.fields.FatalFunc, + FatalfFunc: test.fields.FatalfFunc, + } + + l.Errord(test.args.msg, test.args.details...) + if err := test.checkFunc(test.want); err != nil { + tt.Errorf("error = %v", err) + } + }) + } +} + +func TestLogger_Fatald(t *testing.T) { + t.Parallel() + type args struct { + msg string + details []interface{} + } + type fields struct { + DebugFunc func(vals ...interface{}) + DebugfFunc func(format string, vals ...interface{}) + InfoFunc func(vals ...interface{}) + InfofFunc func(format string, vals ...interface{}) + WarnFunc func(vals ...interface{}) + WarnfFunc func(format string, vals ...interface{}) + ErrorFunc func(vals ...interface{}) + ErrorfFunc func(format string, vals ...interface{}) + FatalFunc func(vals ...interface{}) + FatalfFunc func(format string, vals ...interface{}) + } + type want struct { + } + type test struct { + name string + args args + fields fields + want want + checkFunc func(want) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want) error { + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + args: args { + msg: "", + details: nil, + }, + fields: fields { + DebugFunc: nil, + DebugfFunc: nil, + InfoFunc: nil, + InfofFunc: nil, + WarnFunc: nil, + WarnfFunc: nil, + ErrorFunc: nil, + ErrorfFunc: nil, + FatalFunc: nil, + FatalfFunc: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + msg: "", + details: nil, + }, + fields: fields { + DebugFunc: nil, + DebugfFunc: nil, + InfoFunc: nil, + InfofFunc: nil, + WarnFunc: nil, + WarnfFunc: nil, + ErrorFunc: nil, + ErrorfFunc: nil, + FatalFunc: nil, + FatalfFunc: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + for _, tc := range tests { + test := tc + t.Run(test.name, func(tt *testing.T) { + tt.Parallel() + defer goleak.VerifyNone(tt, goleak.IgnoreCurrent()) + if test.beforeFunc != nil { + test.beforeFunc(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + l := &Logger{ + DebugFunc: test.fields.DebugFunc, + DebugfFunc: test.fields.DebugfFunc, + InfoFunc: test.fields.InfoFunc, + InfofFunc: test.fields.InfofFunc, + WarnFunc: test.fields.WarnFunc, + WarnfFunc: test.fields.WarnfFunc, + ErrorFunc: test.fields.ErrorFunc, + ErrorfFunc: test.fields.ErrorfFunc, + FatalFunc: test.fields.FatalFunc, + FatalfFunc: test.fields.FatalfFunc, + } + + l.Fatald(test.args.msg, test.args.details...) + if err := test.checkFunc(test.want); err != nil { + tt.Errorf("error = %v", err) + } + }) + } +} + +func TestLogger_Close(t *testing.T) { + t.Parallel() + type fields struct { + DebugFunc func(vals ...interface{}) + DebugfFunc func(format string, vals ...interface{}) + InfoFunc func(vals ...interface{}) + InfofFunc func(format string, vals ...interface{}) + WarnFunc func(vals ...interface{}) + WarnfFunc func(format string, vals ...interface{}) + ErrorFunc func(vals ...interface{}) + ErrorfFunc func(format string, vals ...interface{}) + FatalFunc func(vals ...interface{}) + FatalfFunc func(format string, vals ...interface{}) + } + type want struct { + err error + } + type test struct { + name string + fields fields + want want + checkFunc func(want, error) error + beforeFunc func() + afterFunc func() + } + defaultCheckFunc := func(w want, err error) error { + if !errors.Is(err, w.err) { + return errors.Errorf("got_error: \"%#v\",\n\t\t\t\twant: \"%#v\"", err, w.err) + } + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + fields: fields { + DebugFunc: nil, + DebugfFunc: nil, + InfoFunc: nil, + InfofFunc: nil, + WarnFunc: nil, + WarnfFunc: nil, + ErrorFunc: nil, + ErrorfFunc: nil, + FatalFunc: nil, + FatalfFunc: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + fields: fields { + DebugFunc: nil, + DebugfFunc: nil, + InfoFunc: nil, + InfofFunc: nil, + WarnFunc: nil, + WarnfFunc: nil, + ErrorFunc: nil, + ErrorfFunc: nil, + FatalFunc: nil, + FatalfFunc: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + for _, tc := range tests { + test := tc + t.Run(test.name, func(tt *testing.T) { + tt.Parallel() + defer goleak.VerifyNone(tt, goleak.IgnoreCurrent()) + if test.beforeFunc != nil { + test.beforeFunc() + } + if test.afterFunc != nil { + defer test.afterFunc() + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + l := &Logger{ + DebugFunc: test.fields.DebugFunc, + DebugfFunc: test.fields.DebugfFunc, + InfoFunc: test.fields.InfoFunc, + InfofFunc: test.fields.InfofFunc, + WarnFunc: test.fields.WarnFunc, + WarnfFunc: test.fields.WarnfFunc, + ErrorFunc: test.fields.ErrorFunc, + ErrorfFunc: test.fields.ErrorfFunc, + FatalFunc: test.fields.FatalFunc, + FatalfFunc: test.fields.FatalfFunc, + } + + err := l.Close() + if err := test.checkFunc(test.want, err); err != nil { + tt.Errorf("error = %v", err) + } + + }) + } +} diff --git a/internal/log/zap/zap.go b/internal/log/zap/zap.go index 2cec455374d..16e096f2393 100644 --- a/internal/log/zap/zap.go +++ b/internal/log/zap/zap.go @@ -16,6 +16,7 @@ package zap import ( + "github.com/vdaas/vald/internal/errors" "github.com/vdaas/vald/internal/log/format" "github.com/vdaas/vald/internal/log/level" @@ -92,6 +93,15 @@ func (l *logger) initialize(sinkPath, errSinkPath string) (err error) { return nil } +func (l *logger) Close() error { + err := l.logger.Sync() + if err != nil { + return errors.Wrap(l.sugar.Sync(), err.Error()) + } + + return l.sugar.Sync() +} + func toZapLevel(lv level.Level) zapcore.Level { switch lv { case level.DEBUG: diff --git a/internal/log/zap/zap_test.go b/internal/log/zap/zap_test.go index 1e5154b2a4e..69542907ec5 100644 --- a/internal/log/zap/zap_test.go +++ b/internal/log/zap/zap_test.go @@ -1658,3 +1658,96 @@ func Test_logger_Fatald(t *testing.T) { }) } } + +func Test_logger_Close(t *testing.T) { + t.Parallel() + type fields struct { + format format.Format + level level.Level + enableCaller bool + logger *zap.Logger + sugar *zap.SugaredLogger + } + type want struct { + err error + } + type test struct { + name string + fields fields + want want + checkFunc func(want, error) error + beforeFunc func() + afterFunc func() + } + defaultCheckFunc := func(w want, err error) error { + if !errors.Is(err, w.err) { + return errors.Errorf("got_error: \"%#v\",\n\t\t\t\twant: \"%#v\"", err, w.err) + } + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + fields: fields { + format: nil, + level: nil, + enableCaller: false, + logger: nil, + sugar: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + fields: fields { + format: nil, + level: nil, + enableCaller: false, + logger: nil, + sugar: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + for _, tc := range tests { + test := tc + t.Run(test.name, func(tt *testing.T) { + tt.Parallel() + defer goleak.VerifyNone(tt, goleak.IgnoreCurrent()) + if test.beforeFunc != nil { + test.beforeFunc() + } + if test.afterFunc != nil { + defer test.afterFunc() + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + l := &logger{ + format: test.fields.format, + level: test.fields.level, + enableCaller: test.fields.enableCaller, + logger: test.fields.logger, + sugar: test.fields.sugar, + } + + err := l.Close() + if err := test.checkFunc(test.want, err); err != nil { + tt.Errorf("error = %v", err) + } + + }) + } +} diff --git a/internal/runner/runner.go b/internal/runner/runner.go index a7646bf55d8..86a7180dca3 100644 --- a/internal/runner/runner.go +++ b/internal/runner/runner.go @@ -71,11 +71,13 @@ func Do(ctx context.Context, opts ...Option) error { if isHelp || err != nil { log.Init(log.WithLevel(level.FATAL.String())) + defer log.Close() return err } if p.ShowVersion() { log.Init(log.WithLevel(level.INFO.String())) + defer log.Close() log.Info(info.String()) return nil } @@ -83,6 +85,7 @@ func Do(ctx context.Context, opts ...Option) error { cfg, ccfg, err := r.loadConfig(p.ConfigFilePath()) if err != nil { log.Init(log.WithLevel(level.FATAL.String())) + defer log.Close() return err } @@ -95,6 +98,7 @@ func Do(ctx context.Context, opts ...Option) error { } else { log.Init() } + defer log.Close() log.Debugf("version info:\t\t%s\n\nconfiguration:\t\t%s\n\n", func() string {