Skip to content

Commit

Permalink
feat: add slog adapter (#3)
Browse files Browse the repository at this point in the history
* feat: add slog adapter

* fix: modify mod versions

* chore: save

* ci: modify pipelines

* fix: ci deployment

* chore: add comments
  • Loading branch information
danteay committed Apr 17, 2024
1 parent b2a1e64 commit 0da9196
Show file tree
Hide file tree
Showing 12 changed files with 63 additions and 7 deletions.
13 changes: 13 additions & 0 deletions .github/workflows/tag_adapters.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ on:
branches:
- main

permissions:
contents: write

env:
GIT_USER_EMAIL: ${{ secrets.GIT_EMAIL }}
GIT_USER_NAME: ${{ secrets.GIT_NAME }}

jobs:
tag_adapter:
if: "!startsWith(github.event.head_commit.message, 'bump:')"
Expand Down Expand Up @@ -34,6 +41,12 @@ jobs:
if: steps.check.outputs.any_changed != 'true'
run: exit 78

- name: Config Git User
run: |
git config --local user.email "$GIT_USER_EMAIL"
git config --local user.name "$GIT_USER_NAME"
git config --local pull.ff only
- name: Setup Python
uses: actions/setup-python@v5
with:
Expand Down
13 changes: 13 additions & 0 deletions .github/workflows/tag_logger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ on:
branches:
- main

permissions:
contents: write

env:
GIT_USER_EMAIL: ${{ secrets.GIT_EMAIL }}
GIT_USER_NAME: ${{ secrets.GIT_NAME }}

jobs:
tag_adapter:
if: "!startsWith(github.event.head_commit.message, 'bump:')"
Expand Down Expand Up @@ -32,6 +39,12 @@ jobs:
if: steps.check.outputs.any_changed != 'true'
run: exit 78

- name: Config Git User
run: |
git config --local user.email "$GIT_USER_EMAIL"
git config --local user.name "$GIT_USER_NAME"
git config --local pull.ff only
- name: Setup Python
uses: actions/setup-python@v5
with:
Expand Down
13 changes: 13 additions & 0 deletions .github/workflows/tag_modules.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ on:
branches:
- main

permissions:
contents: write

env:
GIT_USER_EMAIL: ${{ secrets.GIT_EMAIL }}
GIT_USER_NAME: ${{ secrets.GIT_NAME }}

jobs:
tag_adapter:
if: "!startsWith(github.event.head_commit.message, 'bump:')"
Expand Down Expand Up @@ -34,6 +41,12 @@ jobs:
if: steps.check.outputs.any_changed != 'true'
run: exit 78

- name: Config Git User
run: |
git config --local user.email "$GIT_USER_EMAIL"
git config --local user.name "$GIT_USER_NAME"
git config --local pull.ff only
- name: Setup Python
uses: actions/setup-python@v5
with:
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/test_adapters.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,5 @@ jobs:

- name: Run tests
working-directory: adapters/${{ matrix.adapter }}
run: go test -v github.com/danteay/golog/adapters/${{ matrix.adapter }}
run: go test -v -race github.com/danteay/golog/adapters/${{ matrix.adapter }}

2 changes: 1 addition & 1 deletion .github/workflows/test_logger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ jobs:
run: go mod download

- name: Run tests
run: go test -v ./...
run: go test -v -race ./...
2 changes: 1 addition & 1 deletion .github/workflows/test_modules.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ jobs:

- name: Run tests
working-directory: ${{ matrix.module }}
run: go test -v github.com/danteay/golog/${{ matrix.module }}
run: go test -v -race github.com/danteay/golog/${{ matrix.module }}
3 changes: 3 additions & 0 deletions adapters/slog/slog.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os"
)

// Adapter is an slog adapter implementation
type Adapter struct {
logger *slog.Logger
}
Expand All @@ -33,10 +34,12 @@ func New(opts ...Option) *Adapter {
}
}

// Logger returns the slog logger instance
func (a *Adapter) Logger() *slog.Logger {
return a.logger
}

// Log logs a message with the given level, error, fields, and message
func (a *Adapter) Log(level levels.Level, err error, logFields *fields.Fields, msg string, args ...any) {
lenFields := 0
if logFields != nil {
Expand Down
5 changes: 3 additions & 2 deletions adapters/zerolog/zerolog.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ import (
"io"
"os"

"github.com/rs/zerolog"

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

type Adapter struct {
Expand Down Expand Up @@ -36,10 +35,12 @@ func New(opts ...Option) *Adapter {
}
}

// Logger returns the zerolog logger instance
func (a *Adapter) Logger() zerolog.Logger {
return a.logger
}

// Log logs a message with the given level, error, fields, and message
func (a *Adapter) Log(level levels.Level, err error, logFields *fields.Fields, msg string, args ...any) {
log := a.getLog(level)

Expand Down
10 changes: 10 additions & 0 deletions fields/fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ func New() *Fields {
}
}

// Set sets a key-value pair in the fields
func (f *Fields) Set(key string, value any) *Fields {
f.mutex.Lock()
defer f.mutex.Unlock()
Expand All @@ -23,6 +24,7 @@ func (f *Fields) Set(key string, value any) *Fields {
return f
}

// SetMap sets a map of key-value pairs in the fields
func (f *Fields) SetMap(fields map[string]any) *Fields {
f.mutex.Lock()
defer f.mutex.Unlock()
Expand All @@ -34,13 +36,15 @@ func (f *Fields) SetMap(fields map[string]any) *Fields {
return f
}

// Get returns the value of a key in the fields
func (f *Fields) Get(key string) any {
f.mutex.Lock()
defer f.mutex.Unlock()

return f.data[key]
}

// Delete removes a key from the fields
func (f *Fields) Delete(key string) *Fields {
f.mutex.Lock()
defer f.mutex.Unlock()
Expand All @@ -50,6 +54,7 @@ func (f *Fields) Delete(key string) *Fields {
return f
}

// Clear removes all keys from the fields
func (f *Fields) Clear() *Fields {
f.mutex.Lock()
defer f.mutex.Unlock()
Expand All @@ -59,6 +64,7 @@ func (f *Fields) Clear() *Fields {
return f
}

// Copy returns a copy of the fields
func (f *Fields) Copy() *Fields {
f.mutex.Lock()
defer f.mutex.Unlock()
Expand All @@ -74,6 +80,7 @@ func (f *Fields) Copy() *Fields {
}
}

// Merge merges the fields with another fields
func (f *Fields) Merge(fields *Fields) *Fields {
f.mutex.Lock()
defer f.mutex.Unlock()
Expand All @@ -89,20 +96,23 @@ func (f *Fields) Merge(fields *Fields) *Fields {
return f
}

// Len returns the number of keys in the fields
func (f *Fields) Len() int {
f.mutex.Lock()
defer f.mutex.Unlock()

return len(f.data)
}

// IsEmpty returns true if the fields are empty
func (f *Fields) IsEmpty() bool {
f.mutex.Lock()
defer f.mutex.Unlock()

return len(f.data) == 0
}

// Data returns the fields as a map
func (f *Fields) Data() map[string]any {
f.mutex.Lock()
defer f.mutex.Unlock()
Expand Down
1 change: 1 addition & 0 deletions levels/levels.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const (
// Values less than TraceLevel are handled as numbers.
)

// String returns the string representation of the log level.
func (l Level) String() string {
values := map[Level]string{
TraceLevel: TraceValue,
Expand Down
2 changes: 2 additions & 0 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/danteay/golog/levels"
)

// 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)
}
Expand Down Expand Up @@ -39,6 +40,7 @@ func New(opts ...Option) *Logger {
}
}

// SetContext sets the context to be used in the logger instance to identify and group log fields by execution
func (l *Logger) SetContext(ctx context.Context) *Logger {
if ctx == nil {
return l
Expand Down
3 changes: 1 addition & 2 deletions logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ import (
"strings"
"testing"

"github.com/stretchr/testify/assert"

"github.com/danteay/golog/adapters/slog"
"github.com/danteay/golog/internal/contextfields"
"github.com/danteay/golog/levels"
"github.com/stretchr/testify/assert"
)

type testMsg struct {
Expand Down

0 comments on commit 0da9196

Please sign in to comment.