Skip to content

Commit

Permalink
feat: add trace level support (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
nrwiersma authored May 21, 2024
1 parent 5e201f2 commit 1d510e9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
10 changes: 10 additions & 0 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const (
Warn
Info
Debug
Trace
)

// Level represents the predefined log level.
Expand All @@ -35,6 +36,8 @@ type Level int
// LevelFromString converts a string to Level.
func LevelFromString(lvl string) (Level, error) {
switch lvl {
case "trace", "trce":
return Trace, nil
case "debug", "dbug":
return Debug, nil
case "info":
Expand All @@ -53,6 +56,8 @@ func LevelFromString(lvl string) (Level, error) {
// String returns the string representation of the level.
func (l Level) String() string {
switch l {
case Trace:
return "trce"
case Debug:
return "dbug"
case Info:
Expand Down Expand Up @@ -153,6 +158,11 @@ func (l *Logger) With(ctx ...Field) *Logger {
}
}

// Trace logs a trace message, intended for fine grained debug messages.
func (l *Logger) Trace(msg string, ctx ...Field) {
l.write(msg, Trace, ctx)
}

// Debug logs a debug message.
func (l *Logger) Debug(msg string, ctx ...Field) {
l.write(msg, Debug, ctx)
Expand Down
19 changes: 17 additions & 2 deletions logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ func TestLevelFromString(t *testing.T) {
want logger.Level
wantError bool
}{
{
lvl: "trce",
want: logger.Trace,
wantError: false,
},
{
lvl: "trace",
want: logger.Trace,
wantError: false,
},
{
lvl: "dbug",
want: logger.Debug,
Expand Down Expand Up @@ -127,6 +137,11 @@ func TestLogger(t *testing.T) {
fn func(l *logger.Logger)
want string
}{
{
name: "Trace",
fn: func(l *logger.Logger) { l.Trace("debug", ctx.Str("level", "trace")) },
want: "lvl=trce msg=debug level=trace\n",
},
{
name: "Debug",
fn: func(l *logger.Logger) { l.Debug("debug", ctx.Str("level", "debug")) },
Expand Down Expand Up @@ -161,7 +176,7 @@ func TestLogger(t *testing.T) {
t.Parallel()

var buf bytes.Buffer
log := logger.New(&buf, logger.LogfmtFormat(), logger.Debug)
log := logger.New(&buf, logger.LogfmtFormat(), logger.Trace)

test.fn(log)

Expand Down Expand Up @@ -229,7 +244,7 @@ func TestLogger_Stack(t *testing.T) {

log.Info("some message", ctx.Stack("stack"))

want := `lvl=info msg="some message" stack=[github.com/hamba/logger/logger/logger_test.go:230]` + "\n"
want := `lvl=info msg="some message" stack=[github.com/hamba/logger/logger/logger_test.go:245]` + "\n"
assert.Equal(t, want, buf.String())
}

Expand Down

0 comments on commit 1d510e9

Please sign in to comment.