Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2021-01-12 10:23:17 +03:00
parent bec4b6f98c
commit 5becd73213
2 changed files with 9 additions and 17 deletions

View File

@ -205,7 +205,6 @@ func (l *zeroLogger) String() string {
} }
func (l *zeroLogger) Options() logger.Options { func (l *zeroLogger) Options() logger.Options {
// FIXME: How to return full opts?
return l.opts.Options return l.opts.Options
} }
@ -213,12 +212,7 @@ func (l *zeroLogger) Options() logger.Options {
func NewLogger(opts ...logger.Option) logger.Logger { func NewLogger(opts ...logger.Option) logger.Logger {
// Default options // Default options
options := Options{ options := Options{
Options: logger.Options{ Options: logger.NewOptions(opts...),
Level: 100,
Fields: make(map[string]interface{}),
Out: os.Stderr,
Context: context.Background(),
},
ReportCaller: false, ReportCaller: false,
UseAsDefault: false, UseAsDefault: false,
Mode: Production, Mode: Production,
@ -226,7 +220,6 @@ func NewLogger(opts ...logger.Option) logger.Logger {
} }
l := &zeroLogger{opts: options} l := &zeroLogger{opts: options}
_ = l.Init(opts...)
return l return l
} }

View File

@ -13,7 +13,7 @@ import (
func TestName(t *testing.T) { func TestName(t *testing.T) {
l := NewLogger() l := NewLogger()
l.Init()
if l.String() != "zerolog" { if l.String() != "zerolog" {
t.Errorf("error: name expected 'zerolog' actual: %s", l.String()) t.Errorf("error: name expected 'zerolog' actual: %s", l.String())
} }
@ -23,13 +23,12 @@ func TestName(t *testing.T) {
func TestWithOutput(t *testing.T) { func TestWithOutput(t *testing.T) {
logger.DefaultLogger = NewLogger(logger.WithOutput(os.Stdout)) logger.DefaultLogger = NewLogger(logger.WithOutput(os.Stdout))
logger.DefaultLogger.Init()
logger.Infof(context.TODO(), "testing: %s", "WithOutput") logger.Infof(context.TODO(), "testing: %s", "WithOutput")
} }
func TestSetLevel(t *testing.T) { func TestSetLevel(t *testing.T) {
logger.DefaultLogger = NewLogger() logger.DefaultLogger = NewLogger()
logger.Init(logger.WithLevel(logger.DebugLevel)) logger.Init(logger.WithLevel(logger.DebugLevel))
logger.Debugf(context.TODO(), "test show debug: %s", "debug msg") logger.Debugf(context.TODO(), "test show debug: %s", "debug msg")
@ -39,25 +38,25 @@ func TestSetLevel(t *testing.T) {
func TestWithReportCaller(t *testing.T) { func TestWithReportCaller(t *testing.T) {
logger.DefaultLogger = NewLogger(ReportCaller()) logger.DefaultLogger = NewLogger(ReportCaller())
logger.DefaultLogger.Init()
logger.Infof(context.TODO(), "testing: %s", "WithReportCaller") logger.Infof(context.TODO(), "testing: %s", "WithReportCaller")
} }
func TestWithOut(t *testing.T) { func TestWithOut(t *testing.T) {
logger.DefaultLogger = NewLogger(logger.WithOutput(os.Stdout)) logger.DefaultLogger = NewLogger(logger.WithOutput(os.Stdout))
logger.DefaultLogger.Init()
logger.Infof(context.TODO(), "testing: %s", "WithOut") logger.Infof(context.TODO(), "testing: %s", "WithOut")
} }
func TestWithDevelopmentMode(t *testing.T) { func TestWithDevelopmentMode(t *testing.T) {
logger.DefaultLogger = NewLogger(WithDevelopmentMode(), WithTimeFormat(time.Kitchen)) logger.DefaultLogger = NewLogger(WithDevelopmentMode(), WithTimeFormat(time.Kitchen))
logger.DefaultLogger.Init()
logger.Infof(context.TODO(), "testing: %s", "DevelopmentMode") logger.Infof(context.TODO(), "testing: %s", "DevelopmentMode")
} }
func TestWithFields(t *testing.T) { func TestWithFields(t *testing.T) {
logger.DefaultLogger = NewLogger() logger.DefaultLogger = NewLogger()
logger.DefaultLogger.Init()
logger.Fields(map[string]interface{}{ logger.Fields(map[string]interface{}{
"sumo": "demo", "sumo": "demo",
"human": true, "human": true,
@ -67,7 +66,7 @@ func TestWithFields(t *testing.T) {
func TestWithError(t *testing.T) { func TestWithError(t *testing.T) {
logger.DefaultLogger = NewLogger() logger.DefaultLogger = NewLogger()
logger.DefaultLogger.Init()
logger.Fields(map[string]interface{}{"error": errors.New("I am Error")}).Errorf(context.TODO(), "testing: %s", "WithError") logger.Fields(map[string]interface{}{"error": errors.New("I am Error")}).Errorf(context.TODO(), "testing: %s", "WithError")
} }
@ -78,6 +77,6 @@ func TestWithHooks(t *testing.T) {
}) })
logger.DefaultLogger = NewLogger(WithHooks([]zerolog.Hook{simpleHook})) logger.DefaultLogger = NewLogger(WithHooks([]zerolog.Hook{simpleHook}))
logger.DefaultLogger.Init()
logger.Infof(context.TODO(), "testing: %s", "WithHooks") logger.Infof(context.TODO(), "testing: %s", "WithHooks")
} }