diff --git a/logger.go b/logger.go index 3ebdaea..ce7edd1 100644 --- a/logger.go +++ b/logger.go @@ -57,23 +57,7 @@ func (l Level) String() string { // Field is a context field. type Field func(*Event) -// Logger represents a log writer. -type Logger interface { - // With returns a logger with context. - With(ctx ...Field) Logger - // Debug logs a debug message. - Debug(msg string, ctx ...Field) - // Info logs an informational message. - Info(msg string, ctx ...Field) - // Warn logs a warning message. - Warn(msg string, ctx ...Field) - // Error logs an error message. - Error(msg string, ctx ...Field) - // Crit logs a critical message. - Crit(msg string, ctx ...Field) -} - -type logger struct { +type Logger struct { w io.Writer fmtr Formatter lvl Level @@ -81,16 +65,16 @@ type logger struct { } // New creates a new Logger. -func New(w io.Writer, fmtr Formatter, lvl Level) Logger { - return &logger{ +func New(w io.Writer, fmtr Formatter, lvl Level) *Logger { + return &Logger{ w: w, fmtr: fmtr, lvl: lvl, } } -// With returns a new logger with the given context. -func (l *logger) With(ctx ...Field) Logger { +// With returns a new Logger with the given context. +func (l *Logger) With(ctx ...Field) *Logger { e := newEvent(l.fmtr) defer putEvent(e) @@ -103,7 +87,7 @@ func (l *logger) With(ctx ...Field) Logger { b := make([]byte, e.buf.Len()) copy(b, e.buf.Bytes()) - return &logger{ + return &Logger{ w: l.w, fmtr: l.fmtr, lvl: l.lvl, @@ -112,31 +96,31 @@ func (l *logger) With(ctx ...Field) Logger { } // Debug logs a debug message. -func (l *logger) Debug(msg string, ctx ...Field) { +func (l *Logger) Debug(msg string, ctx ...Field) { l.write(msg, Debug, ctx) } // Info logs an informational message. -func (l *logger) Info(msg string, ctx ...Field) { +func (l *Logger) Info(msg string, ctx ...Field) { l.write(msg, Info, ctx) } // Warn logs a warning message. -func (l *logger) Warn(msg string, ctx ...Field) { +func (l *Logger) Warn(msg string, ctx ...Field) { l.write(msg, Warn, ctx) } // Error logs an error message. -func (l *logger) Error(msg string, ctx ...Field) { +func (l *Logger) Error(msg string, ctx ...Field) { l.write(msg, Error, ctx) } // Crit logs a critical message. -func (l *logger) Crit(msg string, ctx ...Field) { +func (l *Logger) Crit(msg string, ctx ...Field) { l.write(msg, Crit, ctx) } -func (l *logger) write(msg string, lvl Level, ctx []Field) { +func (l *Logger) write(msg string, lvl Level, ctx []Field) { if lvl > l.lvl { return } diff --git a/logger_test.go b/logger_test.go index cabc6f1..0ed99ec 100644 --- a/logger_test.go +++ b/logger_test.go @@ -114,38 +114,38 @@ func TestLevel_String(t *testing.T) { func TestNew(t *testing.T) { log := logger.New(io.Discard, logger.LogfmtFormat(), logger.Debug) - assert.Implements(t, (*logger.Logger)(nil), log) + assert.IsType(t, &logger.Logger{}, log) } func TestLogger(t *testing.T) { tests := []struct { name string - fn func(l logger.Logger) + fn func(l *logger.Logger) want string }{ { name: "Debug", - fn: func(l logger.Logger) { l.Debug("debug", ctx.Str("level", "debug")) }, + fn: func(l *logger.Logger) { l.Debug("debug", ctx.Str("level", "debug")) }, want: "lvl=dbug msg=debug level=debug\n", }, { name: "Info", - fn: func(l logger.Logger) { l.Info("info", ctx.Str("level", "info")) }, + fn: func(l *logger.Logger) { l.Info("info", ctx.Str("level", "info")) }, want: "lvl=info msg=info level=info\n", }, { name: "Warn", - fn: func(l logger.Logger) { l.Warn("warn", ctx.Str("level", "warn")) }, + fn: func(l *logger.Logger) { l.Warn("warn", ctx.Str("level", "warn")) }, want: "lvl=warn msg=warn level=warn\n", }, { name: "Error", - fn: func(l logger.Logger) { l.Error("error", ctx.Str("level", "error")) }, + fn: func(l *logger.Logger) { l.Error("error", ctx.Str("level", "error")) }, want: "lvl=eror msg=error level=error\n", }, { name: "Crit", - fn: func(l logger.Logger) { l.Crit("critical", ctx.Str("level", "critical")) }, + fn: func(l *logger.Logger) { l.Crit("critical", ctx.Str("level", "critical")) }, want: "lvl=crit msg=critical level=critical\n", }, }