Skip to content

Commit

Permalink
add tests for getLogLevelFromEnv
Browse files Browse the repository at this point in the history
  • Loading branch information
shogo82148 committed Sep 22, 2023
1 parent d4c0172 commit 6b2529e
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
9 changes: 6 additions & 3 deletions xray/xrayslog/xrayslog.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//go:build go1.21
// +build go1.21

// Package xrayslog provides a [log/slog.Handler] that adds trace ID to the log record.
// Package xrayslog provides utilities for interfacing with the slog package.
package xrayslog

import (
Expand Down Expand Up @@ -100,7 +100,7 @@ func (h *handler) WithGroup(name string) slog.Handler {
return &h2
}

// NewHandler returns a slog.Handler that adds trace ID to the log record.
// NewHandler returns a [slog.Handler] that adds trace ID to the log record.
func NewHandler(parent slog.Handler, traceIDKey string) slog.Handler {
return &handler{
parent: parent,
Expand All @@ -115,7 +115,10 @@ type xrayLogger struct {

// NewXRayLogger returns a new [xraylog.Logger] such that each call to its Output method dispatches a Record to the specified handler.
// The logger acts as a bridge from the older xraylog API to newer structured logging handlers.
// The log level is determined by the environment variable AWS_XRAY_LOG_LEVEL.
// The log level can be set by using either the AWS_XRAY_DEBUG_MODE or AWS_XRAY_LOG_LEVEL environment variables.
// If AWS_XRAY_DEBUG_MODE is set, the log level is set to the debug level.
// AWS_XRAY_LOG_LEVEL may be set to debug, info, warn, error or silent.
// This value is ignored if AWS_XRAY_DEBUG_MODE is set.
func NewXRayLogger(h slog.Handler) xraylog.Logger {
return NewXRayLoggerWithMinLevel(h, getLogLevelFromEnv())
}
Expand Down
39 changes: 38 additions & 1 deletion xray/xrayslog/xrayslog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,42 @@ import (
"github.com/shogo82148/aws-xray-yasdk-go/xray/xraylog"
)

func Test_getLogLevelFromEnv(t *testing.T) {
t.Run("AWS_XRAY_DEBUG_MODE is enable", func(t *testing.T) {
t.Setenv("AWS_XRAY_DEBUG_MODE", "1")
t.Setenv("AWS_XRAY_LOG_LEVEL", "info")
got := getLogLevelFromEnv()
want := xraylog.LogLevelDebug
if got != want {
t.Errorf("got %v; want %v", got, want)
}
})

tests := []struct {
env string
want xraylog.LogLevel
}{
{"debug", xraylog.LogLevelDebug},
{"info", xraylog.LogLevelInfo},
{"warn", xraylog.LogLevelWarn},
{"error", xraylog.LogLevelError},
{"silent", xraylog.LogLevelSilent},
}

for _, tt := range tests {
tt := tt
t.Run(tt.env, func(t *testing.T) {
t.Setenv("AWS_XRAY_DEBUG_MODE", "")
t.Setenv("AWS_XRAY_LOG_LEVEL", tt.env)
got := getLogLevelFromEnv()
want := tt.want
if got != want {
t.Errorf("got %v; want %v", got, want)
}
})
}
}

func TestHandle_WithoutTraceID(t *testing.T) {
// build the logger
w := &bytes.Buffer{}
Expand Down Expand Up @@ -145,9 +181,10 @@ func TestNewXRayLogger(t *testing.T) {

// log
ctx := context.Background()
logger := NewXRayLogger(h)
logger := NewXRayLoggerWithMinLevel(h, xraylog.LogLevelInfo)
ctx = xraylog.WithLogger(ctx, logger)
xraylog.Info(ctx, "Hello, World!")
xraylog.Debug(ctx, "Hello, It's debug log and should be ignored")

// check the result
var v struct {
Expand Down

0 comments on commit 6b2529e

Please sign in to comment.