Skip to content

Commit

Permalink
feat: remove interface (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
nrwiersma authored May 17, 2021
1 parent 55c4a45 commit 6a218c2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 35 deletions.
40 changes: 12 additions & 28 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,40 +57,24 @@ 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
ctx []byte
}

// 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)

Expand All @@ -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,
Expand All @@ -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
}
Expand Down
14 changes: 7 additions & 7 deletions logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
}
Expand Down

0 comments on commit 6a218c2

Please sign in to comment.