Skip to content

Commit

Permalink
Move handleroptions.Config to logging package
Browse files Browse the repository at this point in the history
  • Loading branch information
Rieb, Elias committed Dec 13, 2023
1 parent f3d1356 commit d2c51fc
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
15 changes: 8 additions & 7 deletions pkg/handleroptions/config.go → pkg/logging/config.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package handleroptions
package level

import (
"encoding/json"
"log/slog"
"time"

"github.com/Roshick/go-autumn-slog/pkg/level"

auconfigapi "github.com/StephanHCB/go-autumn-config-api"
)

Expand All @@ -23,14 +21,18 @@ type Config struct {
vTimestampTransformer TimestampTransformer
}

func NewDefaultConfig() *Config {
func NewConfig() *Config {
return &Config{
vTimestampTransformer: func(timestamp time.Time) time.Time {
return timestamp.UTC()
},
}
}

func (c *Config) LogLevel() slog.Level {
return c.vLogLevel
}

func (c *Config) SetTimestampTransformer(transformer TimestampTransformer) {
c.vTimestampTransformer = transformer
}
Expand All @@ -42,8 +44,7 @@ func (c *Config) HandlerOptions() *slog.HandlerOptions {
}
if attr.Key == slog.LevelKey {
logLevel := attr.Value.Any().(slog.Level)
attr.Value = slog.StringValue(level.LevelToString(logLevel))

attr.Value = slog.StringValue(LevelToString(logLevel))
}
if mappedKey, ok := c.vLogAttributeKeyMappings[attr.Key]; ok {
attr.Key = mappedKey
Expand Down Expand Up @@ -77,7 +78,7 @@ func (c *Config) ConfigItems() []auconfigapi.ConfigItem {
}

func (c *Config) ObtainValues(getter func(string) string) error {
if vLogLevel, err := level.ParseLogLevel(getter(DefaultKeyLogLevel)); err != nil {
if vLogLevel, err := ParseLogLevel(getter(DefaultKeyLogLevel)); err != nil {
return err
} else {
c.vLogLevel = vLogLevel
Expand Down
16 changes: 8 additions & 8 deletions pkg/handleroptions/config_test.go → pkg/logging/config_test.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
package handleroptions_test
package level_test

import (
"bytes"
"github.com/Roshick/go-autumn-slog/pkg/level"
"log/slog"
"testing"
"time"

"github.com/Roshick/go-autumn-slog/pkg/handleroptions"
"github.com/Roshick/go-autumn-slog/pkg/level"
"github.com/stretchr/testify/assert"
)

func getter(key string) string {
values := map[string]string{
handleroptions.DefaultKeyLogLevel: "FATAL",
handleroptions.DefaultKeyLogAttributeKeyMappings: `
level.DefaultKeyLogLevel: "FATAL",
level.DefaultKeyLogAttributeKeyMappings: `
{
"time": "@timestamp"
}
Expand All @@ -28,15 +28,15 @@ func TestObtainDefaultConfig_TextHandler(t *testing.T) {

err := config.ObtainValues(getter)
assert.NoError(t, err)
assert.Equal(t, level.Fatal, config.HandlerOptions().Level.Level())
assert.Equal(t, Fatal, config.HandlerOptions().Level.Level())

config.SetTimestampTransformer(func(timestamp time.Time) time.Time {
return time.Time{}
})
result := bytes.NewBuffer(nil)
handler := slog.NewTextHandler(result, config.HandlerOptions())
logger := slog.New(handler)
logger.Log(nil, level.Panic, "this is a test")
logger.Log(nil, Panic, "this is a test")
assert.Equal(t, "@timestamp=0001-01-01T00:00:00.000Z level=PANIC msg=\"this is a test\"\n", result.String())
}

Expand All @@ -45,14 +45,14 @@ func TestObtainDefaultConfig_JSONHandler(t *testing.T) {

err := config.ObtainValues(getter)
assert.NoError(t, err)
assert.Equal(t, level.Fatal, config.HandlerOptions().Level.Level())
assert.Equal(t, Fatal, config.HandlerOptions().Level.Level())

config.SetTimestampTransformer(func(timestamp time.Time) time.Time {
return time.Time{}
})
result := bytes.NewBuffer(nil)
handler := slog.NewJSONHandler(result, config.HandlerOptions())
logger := slog.New(handler)
logger.Log(nil, level.Panic, "this is a test")
logger.Log(nil, Panic, "this is a test")
assert.Equal(t, "{\"@timestamp\":\"0001-01-01T00:00:00Z\",\"level\":\"PANIC\",\"msg\":\"this is a test\"}\n", result.String())
}

0 comments on commit d2c51fc

Please sign in to comment.