Skip to content

Commit

Permalink
Merge pull request #10 from Roshick/handleroptions
Browse files Browse the repository at this point in the history
Move handleroptions.Config to logging package
  • Loading branch information
Roshick authored Dec 13, 2023
2 parents 691175a + fe48b72 commit 19197ae
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 28 deletions.
31 changes: 15 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ every existing [handler](https://pkg.go.dev/log/slog#hdr-Writing_a_handler).

## Usage

The library is divided into four largely independent areas: 'logging', 'level', 'handlers' and 'handleroptions'.
The library is divided into three largely independent areas: 'logging', 'level' and 'handlers'.

### Logging

Expand All @@ -35,6 +35,20 @@ the system can be established in the following order of priority:
2. By setting the logger as the default for a new instance through `mySubLogging := myLogging.WithLogger(myLogger)`.
3. By setting the logger as slog's default logger via `slog.SetDefault(myLogger)`.

#### Resources via ConfigLoader

The package also provides a configuration-based instantiation (compatible with but not limited
to [go-autumn-configloader](https://github.com/Roshick/go-autumn-configloader)) of different slog resources.

One of the currently supported resources is slog.HandlerOptions, a feature utilized by slog.TextHandler,
slog.JSONHandler, and various third-party handlers. Users can leverage these options to
define log levels for handlers and manipulate the attributes of each passing record. This flexibility is especially
beneficial in scenarios demanding standardized log fields, as demonstrated by
the [Elastic Common Schema](https://www.elastic.co/guide/en/ecs/current/index.html).
Additionally, the supplied slog.HandlerOptions map the new log levels to their respective correct string
values. This proves crucial, given that log/slog defaults to mapping them in relation to the default values —
illustrated by, for instance, 'PANIC' being emitted as 'ERROR+8'.

### Level

This library expands the default slog.Levels to include the following severity levels, arranged in ascending order:
Expand Down Expand Up @@ -65,21 +79,6 @@ particularly useful for adding context values to every record, such as tracing i
This handler performs no operations and is employed in situations where neither the context, the logging system, nor
slog has any logger configured.

### HandlerOptions

This package provides configuration-based instantiation (compatible with but not limited
to [go-autumn-configloader](https://github.com/Roshick/go-autumn-configloader)) of slog.HandlerOptions, a feature
utilized by slog.TextHandler,
slog.JSONHandler, and various third-party handlers. Users can leverage these options to define log levels for handlers
and manipulate the attributes of each passing record.

This flexibility is especially beneficial in scenarios demanding standardized log fields, as demonstrated by
the [Elastic Common Schema](https://www.elastic.co/guide/en/ecs/current/index.html). Additionally, the supplied
slog.HandlerOptions map the new log levels to their respective correct string
values. This proves crucial, given that log/slog defaults to mapping them in relation to the default values —
illustrated
by, for instance, 'PANIC' being emitted as 'ERROR+8'.

## Examples

This section delves into practical use cases to aid the integration of the library into various applications.
Expand Down
12 changes: 7 additions & 5 deletions pkg/handleroptions/config.go → pkg/logging/config.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package handleroptions
package logging

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

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

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

Expand All @@ -23,14 +22,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 @@ -43,7 +46,6 @@ func (c *Config) HandlerOptions() *slog.HandlerOptions {
if attr.Key == slog.LevelKey {
logLevel := attr.Value.Any().(slog.Level)
attr.Value = slog.StringValue(level.LevelToString(logLevel))

}
if mappedKey, ok := c.vLogAttributeKeyMappings[attr.Key]; ok {
attr.Key = mappedKey
Expand Down
14 changes: 7 additions & 7 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 logging_test

import (
"bytes"
"github.com/Roshick/go-autumn-slog/pkg/level"
"github.com/Roshick/go-autumn-slog/pkg/logging"
"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: `
logging.DefaultKeyLogLevel: "FATAL",
logging.DefaultKeyLogAttributeKeyMappings: `
{
"time": "@timestamp"
}
Expand All @@ -24,7 +24,7 @@ func getter(key string) string {
}

func TestObtainDefaultConfig_TextHandler(t *testing.T) {
config := handleroptions.NewDefaultConfig()
config := logging.NewConfig()

err := config.ObtainValues(getter)
assert.NoError(t, err)
Expand All @@ -41,7 +41,7 @@ func TestObtainDefaultConfig_TextHandler(t *testing.T) {
}

func TestObtainDefaultConfig_JSONHandler(t *testing.T) {
config := handleroptions.NewDefaultConfig()
config := logging.NewConfig()

err := config.ObtainValues(getter)
assert.NoError(t, err)
Expand Down

0 comments on commit 19197ae

Please sign in to comment.