Skip to content

Commit

Permalink
Merge pull request #11 from Roshick/handleroptions
Browse files Browse the repository at this point in the history
Provide UTC and ZERO TimeTransformer via configuration
  • Loading branch information
Roshick authored Dec 13, 2023
2 parents 19197ae + 4c178cd commit 7b77eda
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 35 deletions.
71 changes: 47 additions & 24 deletions pkg/logging/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package logging

import (
"encoding/json"
"fmt"
"github.com/Roshick/go-autumn-slog/pkg/level"
"log/slog"
"time"
Expand All @@ -10,38 +11,31 @@ import (
)

const (
DefaultKeyLogLevel = "LOG_LEVEL"
DefaultKeyLogAttributeKeyMappings = "LOG_ATTRIBUTE_KEY_MAPPINGS"
DefaultConfigKeyLevel = "LOG_LEVEL"
DefaultConfigKeyTimeTransformer = "LOG_TIME_TRANSFORMER"
DefaultConfigKeyAttributeKeyMappings = "LOG_ATTRIBUTE_KEY_MAPPINGS"
)

type TimestampTransformer func(time.Time) time.Time
type TimeTransformer func(time.Time) time.Time

type Config struct {
vLogLevel slog.Level
vLogAttributeKeyMappings map[string]string
vTimestampTransformer TimestampTransformer
vTimeTransformer TimeTransformer
}

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

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

func (c *Config) SetTimestampTransformer(transformer TimestampTransformer) {
c.vTimestampTransformer = transformer
}

func (c *Config) HandlerOptions() *slog.HandlerOptions {
replaceAttr := func(_ []string, attr slog.Attr) slog.Attr {
if attr.Key == slog.TimeKey {
attr.Value = slog.TimeValue(c.vTimestampTransformer(attr.Value.Time()))
attr.Value = slog.TimeValue(c.vTimeTransformer(attr.Value.Time()))
}
if attr.Key == slog.LevelKey {
logLevel := attr.Value.Any().(slog.Level)
Expand All @@ -62,30 +56,44 @@ func (c *Config) HandlerOptions() *slog.HandlerOptions {
func (c *Config) ConfigItems() []auconfigapi.ConfigItem {
return []auconfigapi.ConfigItem{
{
Key: DefaultKeyLogLevel,
EnvName: DefaultKeyLogLevel,
Default: "INFO",
Description: "Minimum level of all logs.",
Validate: auconfigapi.ConfigNeedsNoValidation,
Key: DefaultConfigKeyLevel,
EnvName: DefaultConfigKeyLevel,
Default: "INFO",
Description: "Minimum level of all logs. \n" +
"Supported values: TRACE, DEBUG, INFO, WARN, ERROR, FATAL, PANIC, SILENT",
Validate: auconfigapi.ConfigNeedsNoValidation,
}, {
Key: DefaultConfigKeyTimeTransformer,
EnvName: DefaultConfigKeyTimeTransformer,
Default: "UTC",
Description: "Type of transformation applied to each record's timestamp. Useful for testing purposes. \n" +
"Supported values: UTC, ZERO",
Validate: auconfigapi.ConfigNeedsNoValidation,
}, {
Key: DefaultKeyLogAttributeKeyMappings,
EnvName: DefaultKeyLogAttributeKeyMappings,
Key: DefaultConfigKeyAttributeKeyMappings,
EnvName: DefaultConfigKeyAttributeKeyMappings,
Default: "{}",
Description: "Mappings for attribute keys of all logs. " +
Description: "Mappings for attribute keys of all logs. \n" +
"Example: The entry [error: error.message] maps every attribute with key \"error\" to use the key \"error.message\" instead.",
Validate: auconfigapi.ConfigNeedsNoValidation,
},
}
}

func (c *Config) ObtainValues(getter func(string) string) error {
if vLogLevel, err := level.ParseLogLevel(getter(DefaultKeyLogLevel)); err != nil {
if vLogLevel, err := level.ParseLogLevel(getter(DefaultConfigKeyLevel)); err != nil {
return err
} else {
c.vLogLevel = vLogLevel
}

if vLogAttributeKeyMappings, err := parseLogAttributeKeyMappings(getter(DefaultKeyLogAttributeKeyMappings)); err != nil {
if vTimeTransformer, err := parseTimeTransformer(getter(DefaultConfigKeyTimeTransformer)); err != nil {
return err
} else {
c.vTimeTransformer = vTimeTransformer
}

if vLogAttributeKeyMappings, err := parseLogAttributeKeyMappings(getter(DefaultConfigKeyAttributeKeyMappings)); err != nil {
return err
} else {
c.vLogAttributeKeyMappings = vLogAttributeKeyMappings
Expand All @@ -94,6 +102,21 @@ func (c *Config) ObtainValues(getter func(string) string) error {
return nil
}

func parseTimeTransformer(value string) (TimeTransformer, error) {
switch value {
case "UTC":
return func(timestamp time.Time) time.Time {
return timestamp.UTC()
}, nil
case "ZERO":
return func(_ time.Time) time.Time {
return time.Time{}
}, nil
default:
return nil, fmt.Errorf("invalid time transformer: '%s'", value)
}
}

func parseLogAttributeKeyMappings(value string) (map[string]string, error) {
var attributeKeyMappings map[string]string
if err := json.Unmarshal([]byte(value), &attributeKeyMappings); err != nil {
Expand Down
15 changes: 4 additions & 11 deletions pkg/logging/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@ import (
"bytes"
"github.com/Roshick/go-autumn-slog/pkg/level"
"github.com/Roshick/go-autumn-slog/pkg/logging"
"github.com/stretchr/testify/assert"
"log/slog"
"testing"
"time"

"github.com/stretchr/testify/assert"
)

func getter(key string) string {
values := map[string]string{
logging.DefaultKeyLogLevel: "FATAL",
logging.DefaultKeyLogAttributeKeyMappings: `
logging.DefaultConfigKeyLevel: "FATAL",
logging.DefaultConfigKeyTimeTransformer: "ZERO",
logging.DefaultConfigKeyAttributeKeyMappings: `
{
"time": "@timestamp"
}
Expand All @@ -30,9 +29,6 @@ func TestObtainDefaultConfig_TextHandler(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, level.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)
Expand All @@ -47,9 +43,6 @@ func TestObtainDefaultConfig_JSONHandler(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, level.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)
Expand Down

0 comments on commit 7b77eda

Please sign in to comment.