Skip to content

Commit

Permalink
Factor out Meta.{Check,Enabled} (#174)
Browse files Browse the repository at this point in the history
* spy: simplify over Meta.{Check,Enabled}

* debark: simplify over Meta

* Meta: factor .Enabled out of logger

* Meta: factor .Check out of logger
  • Loading branch information
jcorbin committed Nov 10, 2016
1 parent 9c9a580 commit d8d60ff
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 69 deletions.
14 changes: 2 additions & 12 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,17 +90,7 @@ func (log *logger) With(fields ...Field) Logger {
}

func (log *logger) Check(lvl Level, msg string) *CheckedMessage {
switch lvl {
case PanicLevel, FatalLevel:
// Panic and Fatal should always cause a panic/exit, even if the level
// is disabled.
break
default:
if lvl < log.Level() {
return nil
}
}
return NewCheckedMessage(log, lvl, msg)
return log.Meta.Check(log, lvl, msg)
}

func (log *logger) Log(lvl Level, msg string, fields ...Field) {
Expand Down Expand Up @@ -142,7 +132,7 @@ func (log *logger) DFatal(msg string, fields ...Field) {
}

func (log *logger) log(lvl Level, msg string, fields []Field) {
if !(lvl >= log.Level()) {
if !log.Meta.Enabled(lvl) {
return
}

Expand Down
21 changes: 21 additions & 0 deletions meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,24 @@ func (m Meta) Clone() Meta {
m.Encoder = m.Encoder.Clone()
return m
}

// Enabled returns true if logging a message at a particular level is enabled.
func (m Meta) Enabled(lvl Level) bool {
return lvl >= m.Level()
}

// Check returns a CheckedMessage logging the given message is Enabled, nil
// otherwise.
func (m Meta) Check(log Logger, lvl Level, msg string) *CheckedMessage {
switch lvl {
case PanicLevel, FatalLevel:
// Panic and Fatal should always cause a panic/exit, even if the level
// is disabled.
break
default:
if !m.Enabled(lvl) {
return nil
}
}
return NewCheckedMessage(log, lvl, msg)
}
24 changes: 3 additions & 21 deletions spy/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,17 +94,7 @@ func (l *Logger) With(fields ...zap.Field) zap.Logger {

// Check returns a CheckedMessage if logging a particular message would succeed.
func (l *Logger) Check(lvl zap.Level, msg string) *zap.CheckedMessage {
switch lvl {
case zap.PanicLevel, zap.FatalLevel:
// Panic and Fatal should always cause a panic/exit, even if the level
// is disabled.
break
default:
if !(lvl >= l.Level()) {
return nil
}
}
return zap.NewCheckedMessage(l, lvl, msg)
return l.Meta.Check(l, lvl, msg)
}

// Log writes a message at the specified level.
Expand Down Expand Up @@ -155,17 +145,9 @@ func (l *Logger) DFatal(msg string, fields ...zap.Field) {
}

func (l *Logger) log(lvl zap.Level, msg string, fields []zap.Field) {
switch lvl {
case zap.PanicLevel, zap.FatalLevel:
// Panic and Fatal should always cause a panic/exit, even if the level
// is disabled.
break
default:
if !(lvl >= l.Level()) {
return
}
if l.Meta.Enabled(lvl) {
l.sink.WriteLog(lvl, msg, l.allFields(fields))
}
l.sink.WriteLog(lvl, msg, l.allFields(fields))
}

func (l *Logger) allFields(added []zap.Field) []zap.Field {
Expand Down
34 changes: 8 additions & 26 deletions zbark/debark.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ func Debarkify(bl bark.Logger, lvl zap.Level) zap.Logger {
return wrapper.zl
}
return &zapper{
lvl: lvl,
bl: bl,
Meta: zap.MakeMeta(nil, lvl),
bl: bl,
}
}

type zapper struct {
lvl zap.Level
bl bark.Logger
zap.Meta
bl bark.Logger
}

func (z *zapper) DFatal(msg string, fields ...zap.Field) {
Expand All @@ -58,7 +58,7 @@ func (z *zapper) Log(l zap.Level, msg string, fields ...zap.Field) {
switch l {
case zap.PanicLevel, zap.FatalLevel:
default:
if l < z.lvl {
if !z.Meta.Enabled(l) {
return
}
}
Expand All @@ -81,34 +81,16 @@ func (z *zapper) Log(l zap.Level, msg string, fields ...zap.Field) {
}
}

func (z *zapper) Level() zap.Level {
return z.lvl
}

// Change the level of this logger, as well as all its ancestors and
// descendants. This makes it easy to change the log level at runtime
// without restarting your application.
func (z *zapper) SetLevel(l zap.Level) {
z.lvl = l
}

// Create a child logger, and optionally add some context to that logger.
func (z *zapper) With(fields ...zap.Field) zap.Logger {
return &zapper{
lvl: z.lvl,
bl: z.bl.WithFields(zapToBark(fields)),
Meta: z.Meta,
bl: z.bl.WithFields(zapToBark(fields)),
}
}

func (z *zapper) Check(l zap.Level, msg string) *zap.CheckedMessage {
switch l {
case zap.PanicLevel, zap.FatalLevel:
default:
if l < z.lvl {
return nil
}
}
return zap.NewCheckedMessage(z, l, msg)
return z.Meta.Check(z, l, msg)
}

func (z *zapper) Debug(msg string, fields ...zap.Field) {
Expand Down
8 changes: 0 additions & 8 deletions zbark/debark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,6 @@ var levels = []zap.Level{
zap.ErrorLevel,
}

func TestDebark_Levels(t *testing.T) {
logger, _ := newDebark(zap.DebugLevel)
for _, l := range append(levels, zap.PanicLevel, zap.FatalLevel) {
logger.SetLevel(l)
assert.Equal(t, l, logger.Level())
}
}

func TestDebark_Check(t *testing.T) {
logger, buf := newDebark(zap.DebugLevel)
for _, l := range append(levels, zap.PanicLevel, zap.FatalLevel) {
Expand Down
3 changes: 1 addition & 2 deletions zwrap/sample_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,7 @@ func TestSamplerCheckPanicFatal(t *testing.T) {
}
}

expected := buildExpectation(level, 0, 1, 2, 3, 4)
assert.Equal(t, expected, sink.Logs(), "Unexpected output when sampling with Check.")
assert.Equal(t, []spy.Log(nil), sink.Logs(), "Unexpected output when sampling with Check.")
}
}

Expand Down

0 comments on commit d8d60ff

Please sign in to comment.