Skip to content
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

[v1, 4/8] Replace NewJSON constructor with NewLogger #115

Merged
merged 3 commits into from
Aug 1, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Replace NewJSON constructor with NewLogger
Since loggers and encoders are now completely separate, make the main
constructor `NewLogger` and have it take an encoder as its first
argument. This requires changing many call sites.
  • Loading branch information
Akshay Shah committed Aug 1, 2016
commit 5219a87b4c5df70d6c14b0cd61359179825e6e45
12 changes: 10 additions & 2 deletions benchmarks/logrus_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,11 @@ func BenchmarkLogrusAddingFields(b *testing.B) {
}

func BenchmarkZapBarkifyAddingFields(b *testing.B) {
logger := zbark.Barkify(zap.NewJSON(zap.DebugLevel, zap.Output(zap.Discard)))
logger := zbark.Barkify(zap.NewLogger(
zap.NewJSONEncoder(),
zap.DebugLevel,
zap.Output(zap.Discard),
))
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
Expand Down Expand Up @@ -106,7 +110,11 @@ func BenchmarkLogrusWithAccumulatedContext(b *testing.B) {
}

func BenchmarkZapBarkifyWithAccumulatedContext(b *testing.B) {
baseLogger := zbark.Barkify(zap.NewJSON(zap.DebugLevel, zap.Output(zap.Discard)))
baseLogger := zbark.Barkify(zap.NewLogger(
zap.NewJSONEncoder(),
zap.DebugLevel,
zap.Output(zap.Discard),
))
logger := baseLogger.WithFields(bark.Fields{
"int": 1,
"int64": int64(1),
Expand Down
6 changes: 4 additions & 2 deletions benchmarks/stdlib_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ func BenchmarkStandardLibraryWithoutFields(b *testing.B) {
}

func BenchmarkZapStandardizeWithoutFields(b *testing.B) {
logger, err := zwrap.Standardize(
zap.NewJSON(zap.DebugLevel, zap.Output(zap.Discard)),
logger, err := zwrap.Standardize(zap.NewLogger(
zap.NewJSONEncoder(),
zap.DebugLevel,
zap.Output(zap.Discard)),
zap.InfoLevel,
)
if err != nil {
Expand Down
64 changes: 53 additions & 11 deletions benchmarks/zap_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func fakeMessages(n int) []string {
}

func BenchmarkZapDisabledLevelsWithoutFields(b *testing.B) {
logger := zap.NewJSON(zap.ErrorLevel, zap.Output(zap.Discard))
logger := zap.NewLogger(zap.NewJSONEncoder(), zap.ErrorLevel, zap.Output(zap.Discard))
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
Expand All @@ -86,7 +86,12 @@ func BenchmarkZapDisabledLevelsWithoutFields(b *testing.B) {

func BenchmarkZapDisabledLevelsAccumulatedContext(b *testing.B) {
context := fakeFields()
logger := zap.NewJSON(zap.ErrorLevel, zap.Output(zap.Discard), zap.Fields(context...))
logger := zap.NewLogger(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional: might be worth adding a helper to create a new JSON logger with output discarded, that takes a list of additional options on top.

zap.NewJSONEncoder(),
zap.ErrorLevel,
zap.Output(zap.Discard),
zap.Fields(context...),
)
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
Expand All @@ -96,7 +101,11 @@ func BenchmarkZapDisabledLevelsAccumulatedContext(b *testing.B) {
}

func BenchmarkZapDisabledLevelsAddingFields(b *testing.B) {
logger := zap.NewJSON(zap.ErrorLevel, zap.Output(zap.Discard))
logger := zap.NewLogger(
zap.NewJSONEncoder(),
zap.ErrorLevel,
zap.Output(zap.Discard),
)
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
Expand All @@ -106,7 +115,11 @@ func BenchmarkZapDisabledLevelsAddingFields(b *testing.B) {
}

func BenchmarkZapDisabledLevelsCheckAddingFields(b *testing.B) {
logger := zap.NewJSON(zap.ErrorLevel, zap.Output(zap.Discard))
logger := zap.NewLogger(
zap.NewJSONEncoder(),
zap.ErrorLevel,
zap.Output(zap.Discard),
)
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
Expand All @@ -118,7 +131,11 @@ func BenchmarkZapDisabledLevelsCheckAddingFields(b *testing.B) {
}

func BenchmarkZapAddingFields(b *testing.B) {
logger := zap.NewJSON(zap.DebugLevel, zap.Output(zap.Discard))
logger := zap.NewLogger(
zap.NewJSONEncoder(),
zap.DebugLevel,
zap.Output(zap.Discard),
)
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
Expand All @@ -129,7 +146,12 @@ func BenchmarkZapAddingFields(b *testing.B) {

func BenchmarkZapWithAccumulatedContext(b *testing.B) {
context := fakeFields()
logger := zap.NewJSON(zap.DebugLevel, zap.Output(zap.Discard), zap.Fields(context...))
logger := zap.NewLogger(
zap.NewJSONEncoder(),
zap.DebugLevel,
zap.Output(zap.Discard),
zap.Fields(context...),
)
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
Expand All @@ -139,7 +161,11 @@ func BenchmarkZapWithAccumulatedContext(b *testing.B) {
}

func BenchmarkZapWithoutFields(b *testing.B) {
logger := zap.NewJSON(zap.DebugLevel, zap.Output(zap.Discard))
logger := zap.NewLogger(
zap.NewJSONEncoder(),
zap.DebugLevel,
zap.Output(zap.Discard),
)
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
Expand All @@ -150,7 +176,11 @@ func BenchmarkZapWithoutFields(b *testing.B) {

func BenchmarkZapSampleWithoutFields(b *testing.B) {
messages := fakeMessages(1000)
base := zap.NewJSON(zap.DebugLevel, zap.Output(zap.Discard))
base := zap.NewLogger(
zap.NewJSONEncoder(),
zap.DebugLevel,
zap.Output(zap.Discard),
)
logger := zwrap.Sample(base, time.Second, 10, 10000)
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
Expand All @@ -164,7 +194,11 @@ func BenchmarkZapSampleWithoutFields(b *testing.B) {

func BenchmarkZapSampleAddingFields(b *testing.B) {
messages := fakeMessages(1000)
base := zap.NewJSON(zap.DebugLevel, zap.Output(zap.Discard))
base := zap.NewLogger(
zap.NewJSONEncoder(),
zap.DebugLevel,
zap.Output(zap.Discard),
)
logger := zwrap.Sample(base, time.Second, 10, 10000)
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
Expand All @@ -178,7 +212,11 @@ func BenchmarkZapSampleAddingFields(b *testing.B) {

func BenchmarkZapSampleCheckWithoutFields(b *testing.B) {
messages := fakeMessages(1000)
base := zap.NewJSON(zap.DebugLevel, zap.Output(zap.Discard))
base := zap.NewLogger(
zap.NewJSONEncoder(),
zap.DebugLevel,
zap.Output(zap.Discard),
)
logger := zwrap.Sample(base, time.Second, 10, 10000)
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
Expand All @@ -194,7 +232,11 @@ func BenchmarkZapSampleCheckWithoutFields(b *testing.B) {

func BenchmarkZapSampleCheckAddingFields(b *testing.B) {
messages := fakeMessages(1000)
base := zap.NewJSON(zap.DebugLevel, zap.Output(zap.Discard))
base := zap.NewLogger(
zap.NewJSONEncoder(),
zap.DebugLevel,
zap.Output(zap.Discard),
)
logger := zwrap.Sample(base, time.Second, 10, 10000)
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
Expand Down
52 changes: 25 additions & 27 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ import (
func Example() {
// Log in JSON, using zap's reflection-free JSON encoder.
// The default options will log any Info or higher logs to standard out.
logger := zap.NewJSON()
// For repeatable tests, pretend that it's always 1970.
logger.StubTime()
logger := zap.NewLogger(
zap.NewJSONEncoder(zap.NoTime()), // drop timestamps in tests
)

logger.Warn("Log without structured data...")
logger.Warn(
Expand All @@ -51,9 +51,9 @@ func Example() {
child.Error("Oh no!")

// Output:
// {"level":"warn","ts":0,"msg":"Log without structured data..."}
// {"level":"warn","ts":0,"msg":"Or use strongly-typed wrappers to add structured context.","library":"zap","latency":1}
// {"level":"error","ts":0,"msg":"Oh no!","user":"jane@test.com","visits":42}
// {"level":"warn","msg":"Log without structured data..."}
// {"level":"warn","msg":"Or use strongly-typed wrappers to add structured context.","library":"zap","latency":1}
// {"level":"error","msg":"Oh no!","user":"jane@test.com","visits":42}
}

func Example_fileOutput() {
Expand All @@ -64,13 +64,12 @@ func Example_fileOutput() {
}
defer os.Remove(f.Name())

logger := zap.NewJSON(
logger := zap.NewLogger(
zap.NewJSONEncoder(zap.NoTime()), // drop timestamps in tests
// Write the logging output to the specified file instead of stdout.
// Any type implementing zap.WriteSyncer or zap.WriteFlusher can be used.
zap.Output(f),
)
// Stub the current time in tests.
logger.StubTime()

logger.Info("This is an info log.", zap.Int("foo", 42))

Expand All @@ -84,59 +83,58 @@ func Example_fileOutput() {

fmt.Println(string(contents))
// Output:
// {"level":"info","ts":0,"msg":"This is an info log.","foo":42}
// {"level":"info","msg":"This is an info log.","foo":42}
}

func ExampleNest() {
logger := zap.NewJSON()
// Stub the current time in tests.
logger.StubTime()
logger := zap.NewLogger(
zap.NewJSONEncoder(zap.NoTime()), // drop timestamps in tests
)

// We'd like the logging context to be {"outer":{"inner":42}}
nest := zap.Nest("outer", zap.Int("inner", 42))
logger.Info("Logging a nested field.", nest)

// Output:
// {"level":"info","ts":0,"msg":"Logging a nested field.","outer":{"inner":42}}
// {"level":"info","msg":"Logging a nested field.","outer":{"inner":42}}
}

func ExampleNewJSON() {
// The default logger outputs to standard out and only writes logs that are
// Info level or higher.
logger := zap.NewJSON()
// Stub the current time in tests.
logger.StubTime()
logger := zap.NewLogger(
zap.NewJSONEncoder(zap.NoTime()), // drop timestamps in tests
)

// The default logger does not print Debug logs.
logger.Debug("This won't be printed.")
logger.Info("This is an info log.")

// Output:
// {"level":"info","ts":0,"msg":"This is an info log."}
// {"level":"info","msg":"This is an info log."}
}

func ExampleNewJSON_options() {
// We can pass multiple options to the NewJSON method to configure
// the logging level, output location, or even the initial context.
logger := zap.NewJSON(
logger := zap.NewLogger(
zap.NewJSONEncoder(zap.NoTime()), // drop timestamps in tests
zap.DebugLevel,
zap.Fields(zap.Int("count", 1)),
)
// Stub the current time in tests.
logger.StubTime()

logger.Debug("This is a debug log.")
logger.Info("This is an info log.")

// Output:
// {"level":"debug","ts":0,"msg":"This is a debug log.","count":1}
// {"level":"info","ts":0,"msg":"This is an info log.","count":1}
// {"level":"debug","msg":"This is a debug log.","count":1}
// {"level":"info","msg":"This is an info log.","count":1}
}

func ExampleCheckedMessage() {
logger := zap.NewJSON()
// Stub the current time in tests.
logger.StubTime()
logger := zap.NewLogger(
zap.NewJSONEncoder(zap.NoTime()), // drop timestamps in tests
)

// By default, the debug logging level is disabled. However, calls to
// logger.Debug will still allocate a slice to hold any passed fields.
Expand All @@ -153,7 +151,7 @@ func ExampleCheckedMessage() {
}

// Output:
// {"level":"info","ts":0,"msg":"This is an info log."}
// {"level":"info","msg":"This is an info log."}
}

func ExampleLevel_MarshalText() {
Expand Down
6 changes: 3 additions & 3 deletions hook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (

func TestHookAddCaller(t *testing.T) {
buf := &testBuffer{}
logger := NewJSON(DebugLevel, Output(buf), AddCaller())
logger := NewLogger(NewJSONEncoder(), DebugLevel, Output(buf), AddCaller())
logger.Info("Callers.")

re := regexp.MustCompile(`"msg":"hook_test.go:[\d]+: Callers\."`)
Expand All @@ -45,15 +45,15 @@ func TestHookAddCallerFail(t *testing.T) {
_callerSkip = 1e3
defer func() { _callerSkip = originalSkip }()

logger := NewJSON(DebugLevel, Output(buf), ErrorOutput(errBuf), AddCaller())
logger := NewLogger(NewJSONEncoder(), DebugLevel, Output(buf), ErrorOutput(errBuf), AddCaller())
logger.Info("Failure.")
assert.Equal(t, "failed to get caller\n", errBuf.String(), "Didn't find expected failure message.")
assert.Contains(t, buf.String(), `"msg":"Failure."`, "Expected original message to survive failures in runtime.Caller.")
}

func TestHookAddStacks(t *testing.T) {
buf := &testBuffer{}
logger := NewJSON(DebugLevel, Output(buf), AddStacks(InfoLevel))
logger := NewLogger(NewJSONEncoder(), DebugLevel, Output(buf), AddStacks(InfoLevel))

logger.Info("Stacks.")
output := buf.String()
Expand Down
Loading