Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add level get an set methods #29

Merged
merged 1 commit into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions adapters/slog/slog.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@ func (a *Adapter) Writer() io.Writer {

func (a *Adapter) SetWriter(w io.Writer) {
a.writer = w
a.logger = getSlogInstance(a.level, a.writer)
}

func (a *Adapter) Level() levels.Level {
return a.level
}

func (a *Adapter) SetLevel(level levels.Level) {
a.level = level
a.logger = getSlogInstance(level, a.writer)
}

// Logger returns the slog logger instance
Expand Down
103 changes: 98 additions & 5 deletions adapters/slog/slog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ import (
)

type testMsg struct {
Level string `json:"level"`
Message string `json:"msg"`
Error string `json:"error"`
Key1 string `json:"key1"`
Key2 int `json:"key2"`
Level string `json:"level"`
Message string `json:"msg"`
Error string `json:"error"`
Key1 string `json:"key1"`
Key2 int `json:"key2"`
Stack []string `json:"stack"`
}

func TestAdapter_Log(t *testing.T) {
Expand Down Expand Up @@ -136,6 +137,98 @@ func TestAdapter_Log(t *testing.T) {

assert.Equal(t, "", logOutput.String())
})

t.Run("should log message with trace", func(t *testing.T) {
var logOutput bytes.Buffer

logger := New(WithLevel(levels.TraceLevel), WithWriter(&logOutput), WithTrace())

logFields := fields.New().SetMap(map[string]any{
"key1": "value1",
"key2": 42,
})

msg := "Test message"
err := errors.New("test error")

logger.Log(levels.Debug, err, logFields, msg)

res := testMsg{}

if errUnmarshall := json.Unmarshal(logOutput.Bytes(), &res); errUnmarshall != nil {
t.Fatal(errUnmarshall)
}

assert.Equal(t, levels.Debug.String(), strings.ToLower(res.Level))
assert.Equal(t, msg, res.Message)
assert.Equal(t, logFields.Get("key1"), res.Key1)
assert.Equal(t, logFields.Get("key2"), res.Key2)
assert.Equal(t, err.Error(), res.Error)
assert.NotEmpty(t, res.Stack)
})

t.Run("should change level", func(t *testing.T) {
var logOutput bytes.Buffer

logger := New(WithLevel(levels.Info), WithWriter(&logOutput))

logFields := fields.New().SetMap(map[string]any{
"key1": "value1",
"key2": 42,
})

msg := "Test message"
err := errors.New("test error")

logger.Log(levels.Debug, err, logFields, msg)

assert.Equal(t, "", logOutput.String())

logger.SetLevel(levels.Debug)

logger.Log(levels.Debug, err, logFields, msg)

res := testMsg{}

if errUnmarshall := json.Unmarshal(logOutput.Bytes(), &res); errUnmarshall != nil {
t.Fatal(errUnmarshall)
}

assert.Equal(t, levels.Debug.String(), strings.ToLower(res.Level))
assert.Equal(t, msg, res.Message)
assert.Equal(t, logFields.Get("key1"), res.Key1)
assert.Equal(t, logFields.Get("key2"), res.Key2)
assert.Equal(t, err.Error(), res.Error)
})

t.Run("should change writer", func(t *testing.T) {
var logOutput bytes.Buffer

logger := New(WithLevel(levels.Debug), WithWriter(&logOutput))

logFields := fields.New().SetMap(map[string]any{
"key1": "value1",
"key2": 42,
})

msg := "Test message"
err := errors.New("test error")

logger.Log(levels.Debug, err, logFields, msg)

assert.NotEqual(t, "", logOutput.String())

logOutput.Reset()

var newLogOutput bytes.Buffer

logger.SetWriter(&newLogOutput)

logger.Log(levels.Debug, err, logFields, msg)

assert.NotEqual(t, "", newLogOutput.String())
assert.Equal(t, "", logOutput.String())
})
}

func TestGetLevels(t *testing.T) {
Expand Down