Skip to content

Commit

Permalink
feat: writer (#24)
Browse files Browse the repository at this point in the history
* feat: add zerolog writer methods

* feat: add Write method to logger instance

* deps: upgrade levels and slog
  • Loading branch information
danteay committed Apr 17, 2024
1 parent 96e7cce commit 8e43b1e
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 7 deletions.
3 changes: 2 additions & 1 deletion adapters/zerolog/zerolog.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import (
"runtime/debug"
"strings"

"github.com/rs/zerolog"

"github.com/danteay/golog/fields"
"github.com/danteay/golog/levels"
"github.com/rs/zerolog"
)

// Adapter is a zerolog adapter implementation
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ module github.com/danteay/golog
go 1.21

require (
github.com/danteay/golog/adapters/slog v0.2.1
github.com/danteay/golog/adapters/slog v0.3.1
github.com/danteay/golog/fields v0.1.0
github.com/danteay/golog/levels v0.1.0
github.com/danteay/golog/levels v0.1.1
github.com/stretchr/testify v1.9.0
)

Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
github.com/danteay/golog/adapters/slog v0.2.1 h1:Mduw+0EVFGA3fHJJ9u24nUM9ZrndWSBgWxocyJ9/Puo=
github.com/danteay/golog/adapters/slog v0.2.1/go.mod h1:PuACPq2sOwQFEo1hWwfIz7HwwsvSbQn15KMezZm/rkA=
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/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.0 h1:vJqNEKlij90z6b7PSq6bqxmV9KbKn1tj8nlkASl0r14=
github.com/danteay/golog/levels v0.1.0/go.mod h1:eWSbOC3D2TEvsl/Ngmyh1NngmX9ZNnywPTa5kVpN8Ew=
github.com/danteay/golog/levels v0.1.1 h1:cG6KT6bdmfZ7I6Q4TKGtQu+/SK2SY9FJTYzC6JBjzZo=
github.com/danteay/golog/levels v0.1.1/go.mod h1:eWSbOC3D2TEvsl/Ngmyh1NngmX9ZNnywPTa5kVpN8Ew=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down
13 changes: 13 additions & 0 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package golog

import (
"context"
"io"

"github.com/danteay/golog/adapters/slog"
"github.com/danteay/golog/fields"
Expand All @@ -13,6 +14,8 @@ import (
// Adapter is the interface that wraps the Log method.
type Adapter interface {
Log(level levels.Level, err error, logFields *fields.Fields, msg string, args ...any)
Writer() io.Writer
SetWriter(w io.Writer)
}

// Logger is the main struct that holds the logger instance.
Expand All @@ -23,6 +26,8 @@ type Logger struct {
err error
}

var _ io.Writer = (*Logger)(nil)

// New creates a new Logger instance by using the provided context and options.
// If no options are provided, the default options will be used (level: Info, colored: false).
func New(opts ...Option) *Logger {
Expand Down Expand Up @@ -84,10 +89,18 @@ func (l *Logger) Err(err error) *Logger {
return l
}

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 {
Expand Down

0 comments on commit 8e43b1e

Please sign in to comment.