-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Use variadic function to pass parameters instead of Config struct #454
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -60,7 +60,7 @@ func TestVariousLevels(t *testing.T) { | |
| }, | ||
| } { | ||
| var buf bytes.Buffer | ||
| logger := level.New(log.NewJSONLogger(&buf), level.Config{Allowed: testcase.allowed}) | ||
| logger := level.New(log.NewJSONLogger(&buf), level.Allowed(testcase.allowed)) | ||
|
|
||
| level.Debug(logger).Log("this is", "debug log") | ||
| level.Info(logger).Log("this is", "info log") | ||
|
|
@@ -75,10 +75,11 @@ func TestVariousLevels(t *testing.T) { | |
|
|
||
| func TestErrNotAllowed(t *testing.T) { | ||
| myError := errors.New("squelched!") | ||
| logger := level.New(log.NewNopLogger(), level.Config{ | ||
| Allowed: level.AllowWarnAndAbove(), | ||
| ErrNotAllowed: myError, | ||
| }) | ||
| opts := []level.Option{ | ||
| level.Allowed(level.AllowWarnAndAbove()), | ||
| level.ErrNotAllowed(myError), | ||
| } | ||
| logger := level.New(log.NewNopLogger(), opts...) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any reason not to inline these? I'm not fussed, just curious.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No specific reason. I usually prefer to create a slice when I have multiple options because it looks more organized, especially if there's a chance the list of options will grow in the future. I'd be glad to change it if you have the opposite preference.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cool! No worries. |
||
|
|
||
| if want, have := myError, level.Info(logger).Log("foo", "bar"); want != have { | ||
| t.Errorf("want %#+v, have %#+v", want, have) | ||
|
|
@@ -93,10 +94,11 @@ func TestErrNoLevel(t *testing.T) { | |
| myError := errors.New("no level specified") | ||
|
|
||
| var buf bytes.Buffer | ||
| logger := level.New(log.NewJSONLogger(&buf), level.Config{ | ||
| SquelchNoLevel: true, | ||
| ErrNoLevel: myError, | ||
| }) | ||
| opts := []level.Option{ | ||
| level.SquelchNoLevel(true), | ||
| level.ErrNoLevel(myError), | ||
| } | ||
| logger := level.New(log.NewJSONLogger(&buf), opts...) | ||
|
|
||
| if want, have := myError, logger.Log("foo", "bar"); want != have { | ||
| t.Errorf("want %v, have %v", want, have) | ||
|
|
@@ -108,10 +110,11 @@ func TestErrNoLevel(t *testing.T) { | |
|
|
||
| func TestAllowNoLevel(t *testing.T) { | ||
| var buf bytes.Buffer | ||
| logger := level.New(log.NewJSONLogger(&buf), level.Config{ | ||
| SquelchNoLevel: false, | ||
| ErrNoLevel: errors.New("I should never be returned!"), | ||
| }) | ||
| opts := []level.Option{ | ||
| level.SquelchNoLevel(false), | ||
| level.ErrNoLevel(errors.New("I should never be returned!")), | ||
| } | ||
| logger := level.New(log.NewJSONLogger(&buf), opts...) | ||
|
|
||
| if want, have := error(nil), logger.Log("foo", "bar"); want != have { | ||
| t.Errorf("want %v, have %v", want, have) | ||
|
|
@@ -128,11 +131,11 @@ func TestLevelContext(t *testing.T) { | |
| // log.DefaultCaller as per normal. | ||
| var logger log.Logger | ||
| logger = log.NewLogfmtLogger(&buf) | ||
| logger = level.New(logger, level.Config{Allowed: level.AllowAll()}) | ||
| logger = level.New(logger, level.Allowed(level.AllowAll())) | ||
| logger = log.NewContext(logger).With("caller", log.DefaultCaller) | ||
|
|
||
| level.Info(logger).Log("foo", "bar") | ||
| if want, have := `level=info caller=level_test.go:134 foo=bar`, strings.TrimSpace(buf.String()); want != have { | ||
| if want, have := `level=info caller=level_test.go:137 foo=bar`, strings.TrimSpace(buf.String()); want != have { | ||
| t.Errorf("want %q, have %q", want, have) | ||
| } | ||
| } | ||
|
|
@@ -145,10 +148,10 @@ func TestContextLevel(t *testing.T) { | |
| var logger log.Logger | ||
| logger = log.NewLogfmtLogger(&buf) | ||
| logger = log.NewContext(logger).With("caller", log.Caller(5)) | ||
| logger = level.New(logger, level.Config{Allowed: level.AllowAll()}) | ||
| logger = level.New(logger, level.Allowed(level.AllowAll())) | ||
|
|
||
| level.Info(logger).Log("foo", "bar") | ||
| if want, have := `caller=level_test.go:150 level=info foo=bar`, strings.TrimSpace(buf.String()); want != have { | ||
| if want, have := `caller=level_test.go:153 level=info foo=bar`, strings.TrimSpace(buf.String()); want != have { | ||
| t.Errorf("want %q, have %q", want, have) | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would you mind adding an extra sentence of commentary describing the default behavior with no options set?