Skip to content

Commit

Permalink
Rename levels and Err field constructor (#77)
Browse files Browse the repository at this point in the history
Rename all the `Level` constants from `Foo` to `FooLevel`, which allows
us to rename the `Err` field constructor to `Error`. This is a large bag
of changes, but they were all created with `gorename`.

Fixes #47.
  • Loading branch information
akshayjshah committed Jun 6, 2016
1 parent 6f80dbd commit e73195f
Show file tree
Hide file tree
Showing 21 changed files with 172 additions and 171 deletions.
4 changes: 2 additions & 2 deletions benchmarks/logrus_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func BenchmarkLogrusAddingFields(b *testing.B) {
}

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

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

func BenchmarkZapStandardizeWithoutFields(b *testing.B) {
logger, err := zwrap.Standardize(
zap.NewJSON(zap.All, zap.Output(zap.Discard)),
zap.Info,
zap.NewJSON(zap.AllLevel, zap.Output(zap.Discard)),
zap.InfoLevel,
)
if err != nil {
panic("Failed to Standardize a zap.Logger.")
Expand Down
30 changes: 15 additions & 15 deletions benchmarks/zap_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func fakeFields() []zap.Field {
zap.String("string", "four!"),
zap.Bool("bool", true),
zap.Time("time", time.Unix(0, 0)),
zap.Err(errExample),
zap.Error(errExample),
zap.Duration("duration", time.Second),
zap.Marshaler("user-defined type", _jane),
zap.String("another string", "done!"),
Expand All @@ -75,7 +75,7 @@ func fakeMessages(n int) []string {
}

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

func BenchmarkZapDisabledLevelsAccumulatedContext(b *testing.B) {
context := fakeFields()
logger := zap.NewJSON(zap.Error, zap.Output(zap.Discard), zap.Fields(context...))
logger := zap.NewJSON(zap.ErrorLevel, zap.Output(zap.Discard), zap.Fields(context...))
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
Expand All @@ -96,7 +96,7 @@ func BenchmarkZapDisabledLevelsAccumulatedContext(b *testing.B) {
}

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

func BenchmarkZapDisabledLevelsCheckAddingFields(b *testing.B) {
logger := zap.NewJSON(zap.Error, zap.Output(zap.Discard))
logger := zap.NewJSON(zap.ErrorLevel, zap.Output(zap.Discard))
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
if m := logger.Check(zap.Info, "Should be discarded."); m.OK() {
if m := logger.Check(zap.InfoLevel, "Should be discarded."); m.OK() {
m.Write(fakeFields()...)
}
}
})
}

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

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

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

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

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

func BenchmarkZapSampleCheckWithoutFields(b *testing.B) {
messages := fakeMessages(1000)
base := zap.NewJSON(zap.All, zap.Output(zap.Discard))
base := zap.NewJSON(zap.AllLevel, zap.Output(zap.Discard))
logger := zwrap.Sample(base, time.Second, 10, 10000)
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
i := 0
for pb.Next() {
i++
if cm := logger.Check(zap.Info, messages[i%1000]); cm.OK() {
if cm := logger.Check(zap.InfoLevel, messages[i%1000]); cm.OK() {
cm.Write()
}
}
Expand All @@ -194,14 +194,14 @@ func BenchmarkZapSampleCheckWithoutFields(b *testing.B) {

func BenchmarkZapSampleCheckAddingFields(b *testing.B) {
messages := fakeMessages(1000)
base := zap.NewJSON(zap.All, zap.Output(zap.Discard))
base := zap.NewJSON(zap.AllLevel, zap.Output(zap.Discard))
logger := zwrap.Sample(base, time.Second, 10, 10000)
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
i := 0
for pb.Next() {
i++
if m := logger.Check(zap.Info, messages[i%1000]); m.OK() {
if m := logger.Check(zap.InfoLevel, messages[i%1000]); m.OK() {
m.Write(fakeFields()...)
}
}
Expand Down
8 changes: 4 additions & 4 deletions checked_message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ import (
)

func TestJSONLoggerCheck(t *testing.T) {
withJSONLogger(t, opts(Info), func(jl *jsonLogger, output func() []string) {
assert.False(t, jl.Check(Debug, "Debug.").OK(), "Expected CheckedMessage to be not OK at disabled levels.")
withJSONLogger(t, opts(InfoLevel), func(jl *jsonLogger, output func() []string) {
assert.False(t, jl.Check(DebugLevel, "Debug.").OK(), "Expected CheckedMessage to be not OK at disabled levels.")

cm := jl.Check(Info, "Info.")
cm := jl.Check(InfoLevel, "Info.")
require.True(t, cm.OK(), "Expected CheckedMessage to be OK at enabled levels.")
cm.Write(Int("magic", 42))
assert.Equal(
Expand All @@ -49,7 +49,7 @@ func TestCheckedMessageIsSingleUse(t *testing.T) {
`{"msg":"Shouldn't re-use a CheckedMessage.","level":"error","ts":0,"fields":{"original":"Single-use."}}`,
}
withJSONLogger(t, nil, func(jl *jsonLogger, output func() []string) {
cm := jl.Check(Info, "Single-use.")
cm := jl.Check(InfoLevel, "Single-use.")
cm.Write() // ok
cm.Write() // first re-use logs error
cm.Write() // second re-use is silently ignored
Expand Down
6 changes: 3 additions & 3 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ 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(
zap.Debug,
zap.DebugLevel,
zap.Fields(zap.Int("count", 1)),
)
// Stub the current time in tests.
Expand All @@ -107,12 +107,12 @@ func ExampleCheckedMessage() {
// logger.Debug will still allocate a slice to hold any passed fields.
// Particularly performance-sensitive applications can avoid paying this
// penalty by using checked messages.
if cm := logger.Check(zap.Debug, "This is a debug log."); cm.OK() {
if cm := logger.Check(zap.DebugLevel, "This is a debug log."); cm.OK() {
// Debug-level logging is disabled, so we won't get here.
cm.Write(zap.Int("foo", 42), zap.Stack())
}

if cm := logger.Check(zap.Info, "This is an info log."); cm.OK() {
if cm := logger.Check(zap.InfoLevel, "This is an info log."); cm.OK() {
// Since info-level logging is enabled, we expect to write out this message.
cm.Write()
}
Expand Down
4 changes: 2 additions & 2 deletions field.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,10 @@ func Time(key string, val time.Time) Field {
return Int64(key, val.UnixNano())
}

// Err constructs a Field that stores err.Error() under the key "error". This is
// Error constructs a Field that stores err.Error() under the key "error". This is
// just a convenient shortcut for a common pattern - apart from saving a few
// keystrokes, it's no different from using zap.String.
func Err(err error) Field {
func Error(err error) Field {
return String("error", err.Error())
}

Expand Down
4 changes: 2 additions & 2 deletions field_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ func TestTimeField(t *testing.T) {
}

func TestErrField(t *testing.T) {
assertFieldJSON(t, `"error":"fail"`, Err(errors.New("fail")))
assertCanBeReused(t, Err(errors.New("fail")))
assertFieldJSON(t, `"error":"fail"`, Error(errors.New("fail")))
assertCanBeReused(t, Error(errors.New("fail")))
}

func TestDurationField(t *testing.T) {
Expand Down
6 changes: 3 additions & 3 deletions flag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ func TestLevelFlag(t *testing.T) {
}{
{
args: nil,
wantLevel: Info,
wantLevel: InfoLevel,
},
{
args: []string{"--level", "unknown"},
wantErr: true,
},
{
args: []string{"--level", "error"},
wantLevel: Error,
wantLevel: ErrorLevel,
},
}

Expand All @@ -54,7 +54,7 @@ func TestLevelFlag(t *testing.T) {
for _, tt := range tests {
flag.CommandLine = flag.NewFlagSet("test", flag.ContinueOnError)
flag.CommandLine.SetOutput(ioutil.Discard)
level := LevelFlag("level", Info, "")
level := LevelFlag("level", InfoLevel, "")

err := flag.CommandLine.Parse(tt.args)
if tt.wantErr {
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 := newTestBuffer()
logger := NewJSON(All, Output(buf), AddCaller())
logger := NewJSON(AllLevel, 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(All, Output(buf), ErrorOutput(errBuf), AddCaller())
logger := NewJSON(AllLevel, 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 := newTestBuffer()
logger := NewJSON(All, Output(buf), AddStacks(Info))
logger := NewJSON(AllLevel, Output(buf), AddStacks(InfoLevel))

logger.Info("Stacks.")
output := buf.String()
Expand Down
67 changes: 34 additions & 33 deletions level.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,45 +35,46 @@ var errMarshalNilLevel = errors.New("can't marshal a nil *Level to text")
type Level int32

const (
// Debug logs are typically voluminous, and are usually disabled in
// DebugLevel logs are typically voluminous, and are usually disabled in
// production.
Debug Level = iota - 1
// Info is the default logging priority.
Info
// Warn logs are more important than Info, but don't need individual human review.
Warn
// Error logs are high-priority. If an application is running smoothly, it
// shouldn't generate any error-level logs.
Error
// Panic logs a message, then panics.
Panic
// Fatal logs a message, then calls os.Exit(1).
Fatal
DebugLevel Level = iota - 1
// InfoLevel is the default logging priority.
InfoLevel
// WarnLevel logs are more important than Info, but don't need individual
// human review.
WarnLevel
// ErrorLevel logs are high-priority. If an application is running smoothly,
// it shouldn't generate any error-level logs.
ErrorLevel
// PanicLevel logs a message, then panics.
PanicLevel
// FatalLevel logs a message, then calls os.Exit(1).
FatalLevel

// All logs everything.
All Level = math.MinInt32
// None silences logging completely.
None Level = math.MaxInt32
// AllLevel logs everything.
AllLevel Level = math.MinInt32
// NoneLevel silences logging completely.
NoneLevel Level = math.MaxInt32
)

// String returns a lower-case ASCII representation of the log level.
func (l Level) String() string {
switch l {
case All:
case AllLevel:
return "all"
case Debug:
case DebugLevel:
return "debug"
case Info:
case InfoLevel:
return "info"
case Warn:
case WarnLevel:
return "warn"
case Error:
case ErrorLevel:
return "error"
case Panic:
case PanicLevel:
return "panic"
case Fatal:
case FatalLevel:
return "fatal"
case None:
case NoneLevel:
return "none"
default:
return fmt.Sprintf("Level(%d)", l)
Expand All @@ -95,21 +96,21 @@ func (l *Level) MarshalText() ([]byte, error) {
func (l *Level) UnmarshalText(text []byte) error {
switch string(text) {
case "all":
*l = All
*l = AllLevel
case "debug":
*l = Debug
*l = DebugLevel
case "info":
*l = Info
*l = InfoLevel
case "warn":
*l = Warn
*l = WarnLevel
case "error":
*l = Error
*l = ErrorLevel
case "panic":
*l = Panic
*l = PanicLevel
case "fatal":
*l = Fatal
*l = FatalLevel
case "none":
*l = None
*l = NoneLevel
default:
return fmt.Errorf("unrecognized level: %v", string(text))
}
Expand Down
Loading

0 comments on commit e73195f

Please sign in to comment.