-
Notifications
You must be signed in to change notification settings - Fork 246
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5855296
commit 8fc3ded
Showing
5 changed files
with
165 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package logging | ||
|
||
import ( | ||
"context" | ||
"log/slog" | ||
|
||
"github.com/go-kit/log" | ||
"github.com/go-kit/log/level" | ||
) | ||
|
||
var _ slog.Handler = (*SlogGoKitHandler)(nil) | ||
|
||
type SlogGoKitHandler struct { | ||
logger log.Logger | ||
group string | ||
preformatted []any | ||
} | ||
|
||
func NewSlogGoKitHandler(logger log.Logger) *SlogGoKitHandler { | ||
return &SlogGoKitHandler{ | ||
logger: logger, | ||
} | ||
} | ||
|
||
func (h SlogGoKitHandler) Enabled(ctx context.Context, level slog.Level) bool { | ||
return true | ||
} | ||
|
||
func (h SlogGoKitHandler) Handle(ctx context.Context, record slog.Record) error { | ||
var logger log.Logger | ||
switch record.Level { | ||
case slog.LevelInfo: | ||
logger = level.Info(h.logger) | ||
case slog.LevelWarn: | ||
logger = level.Warn(h.logger) | ||
case slog.LevelError: | ||
logger = level.Error(h.logger) | ||
default: | ||
logger = level.Debug(h.logger) | ||
} | ||
|
||
pairs := make([]any, 0, 2*record.NumAttrs()) | ||
pairs = append(pairs, "msg", record.Message) | ||
pairs = append(pairs, h.preformatted...) | ||
|
||
record.Attrs(func(attr slog.Attr) bool { | ||
pairs = appendPair(pairs, h.group, attr) | ||
return true | ||
}) | ||
|
||
return logger.Log(pairs...) | ||
} | ||
|
||
func (h SlogGoKitHandler) WithAttrs(attrs []slog.Attr) slog.Handler { | ||
pairs := make([]any, 0, 2*len(attrs)) | ||
for _, attr := range attrs { | ||
pairs = appendPair(pairs, h.group, attr) | ||
} | ||
|
||
if h.preformatted != nil { | ||
pairs = append(h.preformatted, pairs...) | ||
} | ||
|
||
return &SlogGoKitHandler{ | ||
logger: h.logger, | ||
preformatted: pairs, | ||
group: h.group, | ||
} | ||
} | ||
|
||
func (h SlogGoKitHandler) WithGroup(name string) slog.Handler { | ||
if name == "" { | ||
return h | ||
} | ||
|
||
group := name | ||
if h.group != "" { | ||
group = h.group + "." + group | ||
} | ||
|
||
return &SlogGoKitHandler{ | ||
logger: h.logger, | ||
preformatted: h.preformatted, | ||
group: group, | ||
} | ||
} | ||
|
||
func appendPair(pairs []any, groupPrefix string, attr slog.Attr) []any { | ||
if attr.Equal(slog.Attr{}) { | ||
return pairs | ||
} | ||
|
||
switch attr.Value.Kind() { | ||
case slog.KindGroup: | ||
if attr.Key != "" { | ||
groupPrefix = groupPrefix + "." + attr.Key | ||
} | ||
for _, a := range attr.Value.Group() { | ||
pairs = appendPair(pairs, groupPrefix, a) | ||
} | ||
default: | ||
key := attr.Key | ||
if groupPrefix != "" { | ||
key = groupPrefix + "." + key | ||
} | ||
|
||
pairs = append(pairs, key, attr.Value) | ||
} | ||
|
||
return pairs | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package logging_test | ||
|
||
import ( | ||
"bytes" | ||
"log/slog" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/go-kit/log" | ||
"github.com/grafana/alloy/internal/runtime/logging" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestSlogGokitAdapter_UpdateLevel(t *testing.T) { | ||
buffer := bytes.NewBuffer(nil) | ||
baseLogger, err := logging.New(buffer, logging.Options{Level: logging.LevelInfo, Format: logging.FormatLogfmt}) | ||
require.NoError(t, err) | ||
|
||
gkLogger := log.With(baseLogger, "test", "test") | ||
gkLogger.Log("msg", "hello") | ||
require.Contains(t, buffer.String(), "ts=") | ||
noTimestamp := strings.Join(strings.Split(buffer.String(), " ")[1:], " ") | ||
require.Equal(t, "level=info msg=hello test=test\n", noTimestamp) | ||
|
||
sLogger := slog.New(logging.NewSlogGoKitHandler(gkLogger)) | ||
buffer.Reset() | ||
sLogger.Info("hello") | ||
require.Contains(t, buffer.String(), "ts=") | ||
noTimestamp = strings.Join(strings.Split(buffer.String(), " ")[1:], " ") | ||
require.Equal(t, "level=info msg=hello test=test\n", noTimestamp) | ||
|
||
buffer.Reset() | ||
sLogger.Debug("hello") | ||
require.Equal(t, "", buffer.String()) | ||
|
||
err = baseLogger.Update(logging.Options{Level: logging.LevelDebug, Format: logging.FormatLogfmt}) | ||
require.NoError(t, err) | ||
|
||
buffer.Reset() | ||
sLogger.Info("hello") | ||
require.Contains(t, buffer.String(), "ts=") | ||
noTimestamp = strings.Join(strings.Split(buffer.String(), " ")[1:], " ") | ||
require.Equal(t, "level=info msg=hello test=test\n", noTimestamp) | ||
|
||
buffer.Reset() | ||
sLogger.Debug("hello") | ||
require.Contains(t, buffer.String(), "ts=") | ||
noTimestamp = strings.Join(strings.Split(buffer.String(), " ")[1:], " ") | ||
require.Equal(t, "level=debug msg=hello test=test\n", noTimestamp) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters