Skip to content

Commit

Permalink
feat(log): add format methods
Browse files Browse the repository at this point in the history
Fixes: #32
  • Loading branch information
aymanbagabas committed Mar 8, 2023
1 parent 5315620 commit 2285bce
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 0 deletions.
31 changes: 31 additions & 0 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,3 +294,34 @@ func (l *Logger) Fatal(msg interface{}, keyvals ...interface{}) {
func (l *Logger) Print(msg interface{}, keyvals ...interface{}) {
l.log(noLevel, msg, keyvals...)
}

// Debugf prints a debug message with formatting.
func (l *logger) Debugf(format string, args ...interface{}) {
l.log(DebugLevel, fmt.Sprintf(format, args...))
}

// Infof prints an info message with formatting.
func (l *logger) Infof(format string, args ...interface{}) {
l.log(InfoLevel, fmt.Sprintf(format, args...))
}

// Warnf prints a warning message with formatting.
func (l *logger) Warnf(format string, args ...interface{}) {
l.log(WarnLevel, fmt.Sprintf(format, args...))
}

// Errorf prints an error message with formatting.
func (l *logger) Errorf(format string, args ...interface{}) {
l.log(ErrorLevel, fmt.Sprintf(format, args...))
}

// Fatalf prints a fatal message with formatting and exits.
func (l *logger) Fatalf(format string, args ...interface{}) {
l.log(FatalLevel, fmt.Sprintf(format, args...))
os.Exit(1)
}

// Printf prints a message with no level and formatting.
func (l *logger) Printf(format string, args ...interface{}) {
l.log(noLevel, fmt.Sprintf(format, args...))
}
48 changes: 48 additions & 0 deletions logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,51 @@ func TestWrongLevel(t *testing.T) {
})
}
}

func TestLogFormatter(t *testing.T) {
var buf bytes.Buffer
l := New(WithOutput(&buf), WithLevel(DebugLevel))
cases := []struct {
name string
format string
args []interface{}
fun func(string, ...interface{})
expected string
}{
{
name: "info format",
format: "%s %s",
args: []interface{}{"foo", "bar"},
fun: l.Infof,
expected: "INFO foo bar\n",
},
{
name: "debug format",
format: "%s %s",
args: []interface{}{"foo", "bar"},
fun: l.Debugf,
expected: "DEBUG foo bar\n",
},
{
name: "warn format",
format: "%s %s",
args: []interface{}{"foo", "bar"},
fun: l.Warnf,
expected: "WARN foo bar\n",
},
{
name: "error format",
format: "%s %s",
args: []interface{}{"foo", "bar"},
fun: l.Errorf,
expected: "ERROR foo bar\n",
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
buf.Reset()
c.fun(c.format, "foo", "bar")
assert.Equal(t, c.expected, buf.String())
})
}
}
32 changes: 32 additions & 0 deletions pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package log

import (
"bytes"
"fmt"
"io"
"log"
"os"
Expand Down Expand Up @@ -150,6 +151,37 @@ func Print(msg interface{}, keyvals ...interface{}) {
defaultLogger.log(noLevel, msg, keyvals...)
}

// Debugf logs a debug message with formatting.
func Debugf(format string, args ...interface{}) {
defaultLogger.log(DebugLevel, fmt.Sprintf(format, args...))
}

// Infof logs an info message with formatting.
func Infof(format string, args ...interface{}) {
defaultLogger.log(InfoLevel, fmt.Sprintf(format, args...))
}

// Warnf logs a warning message with formatting.
func Warnf(format string, args ...interface{}) {
defaultLogger.log(WarnLevel, fmt.Sprintf(format, args...))
}

// Errorf logs an error message with formatting.
func Errorf(format string, args ...interface{}) {
defaultLogger.log(ErrorLevel, fmt.Sprintf(format, args...))
}

// Fatalf logs a fatal message with formatting and exit.
func Fatalf(format string, args ...interface{}) {
defaultLogger.log(FatalLevel, fmt.Sprintf(format, args...))
os.Exit(1)
}

// Printf logs a message with formatting and no level.
func Printf(format string, args ...interface{}) {
defaultLogger.log(noLevel, fmt.Sprintf(format, args...))
}

// StandardLog returns a standard logger from the default logger.
func StandardLog(opts ...StandardLogOptions) *log.Logger {
return defaultLogger.StandardLog(opts...)
Expand Down

0 comments on commit 2285bce

Please sign in to comment.