Skip to content

Commit

Permalink
feat: add writer and level methods (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
danteay committed Apr 17, 2024
1 parent 370a7ba commit dc0771d
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 49 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/danteay/golog
go 1.21

require (
github.com/danteay/golog/adapters/slog v0.3.1
github.com/danteay/golog/adapters/slog v0.4.0
github.com/danteay/golog/fields v0.1.0
github.com/danteay/golog/levels v0.1.1
github.com/stretchr/testify v1.9.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
github.com/danteay/golog/adapters/slog v0.3.1 h1:lwR4WafzK8TV5ppAT/YYRzVEqEDl7R2J2z0MWuS+Lrc=
github.com/danteay/golog/adapters/slog v0.3.1/go.mod h1:5UJ4IY36TkpfHMjatweHLsbv+v1uHPnNWwrNEyngLWY=
github.com/danteay/golog/adapters/slog v0.4.0 h1:55g/g0EdDFR1mTmYfGeNh1hbEko7THQSSiNbBhxEsOQ=
github.com/danteay/golog/adapters/slog v0.4.0/go.mod h1:5UJ4IY36TkpfHMjatweHLsbv+v1uHPnNWwrNEyngLWY=
github.com/danteay/golog/fields v0.1.0 h1:/W3Gh3PrVoJsY53sear+yzyLRkIMkM039lOfSNfUFj0=
github.com/danteay/golog/fields v0.1.0/go.mod h1:ACO2Sinx9OSYgwgUVTSZtWaT1q0VzHes6cDVgJtOS4Q=
github.com/danteay/golog/levels v0.1.1 h1:cG6KT6bdmfZ7I6Q4TKGtQu+/SK2SY9FJTYzC6JBjzZo=
Expand Down
60 changes: 60 additions & 0 deletions log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package golog

import (
"github.com/danteay/golog/fields"
"github.com/danteay/golog/internal/contextfields"
"github.com/danteay/golog/internal/errors"
"github.com/danteay/golog/levels"
)

// Log logs a message with the provided level, message and arguments.
func (l *Logger) Log(level levels.Level, msg string, args ...any) {
defer l.reset()

if level <= levels.Disabled {
return
}

l.fields.Merge(contextfields.Fields(l.ctx))

if l.err != nil {
l.fields.Set("stack", errors.GetStackTrace())
}

l.logger.Log(level, l.err, l.fields, msg, args...)
}

// Debug logs a message with the Debug level.
func (l *Logger) Debug(msg string, args ...any) {
l.Log(levels.Debug, msg, args...)
}

// Info logs a message with the Info level.
func (l *Logger) Info(msg string, args ...any) {
l.Log(levels.Info, msg, args...)
}

// Warn logs a message with the Warn level.
func (l *Logger) Warn(msg string, args ...any) {
l.Log(levels.Warn, msg, args...)
}

// Error logs a message with the Error level.
func (l *Logger) Error(msg string, args ...any) {
l.Log(levels.Error, msg, args...)
}

// Fatal logs a message with the Fatal level.
func (l *Logger) Fatal(msg string, args ...any) {
l.Log(levels.Fatal, msg, args...)
}

// Panic logs a message with the Panic level.
func (l *Logger) Panic(msg string, args ...any) {
l.Log(levels.Panic, msg, args...)
}

func (l *Logger) reset() {
l.fields = fields.New()
l.err = nil
}
60 changes: 15 additions & 45 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/danteay/golog/adapters/slog"
"github.com/danteay/golog/fields"
"github.com/danteay/golog/internal/contextfields"
"github.com/danteay/golog/internal/errors"
"github.com/danteay/golog/levels"
)

Expand All @@ -16,6 +15,8 @@ type Adapter interface {
Log(level levels.Level, err error, logFields *fields.Fields, msg string, args ...any)
Writer() io.Writer
SetWriter(w io.Writer)
Level() levels.Level
SetLevel(level levels.Level)
}

// Logger is the main struct that holds the logger instance.
Expand Down Expand Up @@ -89,58 +90,27 @@ func (l *Logger) Err(err error) *Logger {
return l
}

// Write user the writer configured o the adapter to write the logs.
func (l *Logger) Write(p []byte) (n int, err error) {
return l.logger.Writer().Write(p)
}

// Log logs a message with the provided level, message and arguments.
func (l *Logger) Log(level levels.Level, msg string, args ...any) {
defer l.reset()

if level <= levels.Disabled {
return
}

l.fields.Merge(contextfields.Fields(l.ctx))

if l.err != nil {
l.fields.Set("stack", errors.GetStackTrace())
}

l.logger.Log(level, l.err, l.fields, msg, args...)
}

// Debug logs a message with the Debug level.
func (l *Logger) Debug(msg string, args ...any) {
l.Log(levels.Debug, msg, args...)
}

// Info logs a message with the Info level.
func (l *Logger) Info(msg string, args ...any) {
l.Log(levels.Info, msg, args...)
}

// Warn logs a message with the Warn level.
func (l *Logger) Warn(msg string, args ...any) {
l.Log(levels.Warn, msg, args...)
}

// Error logs a message with the Error level.
func (l *Logger) Error(msg string, args ...any) {
l.Log(levels.Error, msg, args...)
// Writer returns the writer for the adapter instance.
func (l *Logger) Writer() io.Writer {
return l.logger.Writer()
}

// Fatal logs a message with the Fatal level.
func (l *Logger) Fatal(msg string, args ...any) {
l.Log(levels.Fatal, msg, args...)
// SetWriter sets the writer for the adapter instance.
func (l *Logger) SetWriter(w io.Writer) {
l.logger.SetWriter(w)
}

// Panic logs a message with the Panic level.
func (l *Logger) Panic(msg string, args ...any) {
l.Log(levels.Panic, msg, args...)
// Level returns the level for the adapter instance.
func (l *Logger) Level() levels.Level {
return l.logger.Level()
}

func (l *Logger) reset() {
l.fields = fields.New()
l.err = nil
// SetLevel sets the level for the adapter instance.
func (l *Logger) SetLevel(level levels.Level) {
l.logger.SetLevel(level)
}
3 changes: 2 additions & 1 deletion logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ func TestLoggerErr(t *testing.T) {
if logger.err == nil {
t.Error("Expected logger error to be non-nil, but it's nil")
}
if logger.err != err {

if !errors.Is(err, logger.err) {
t.Errorf("Expected logger error to be %v, but got %v", err, logger.err)
}
}
Expand Down

0 comments on commit dc0771d

Please sign in to comment.