Skip to content

Commit

Permalink
Merge pull request #479 from shogo82148/xrayslog-see-debug-mode-env
Browse files Browse the repository at this point in the history
NewXRayLogger now see the environment variable AWS_XRAY_LOG_LEVEL.
  • Loading branch information
shogo82148 authored Sep 25, 2023
2 parents 67e1311 + 6b2529e commit 68ba743
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 5 deletions.
3 changes: 3 additions & 0 deletions xray/xraylog/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ const (

// LogLevelError is error level.
LogLevelError

// LogLevelSilent disables the log of the xray tracer.
LogLevelSilent
)

func (ll LogLevel) String() string {
Expand Down
50 changes: 46 additions & 4 deletions xray/xrayslog/xrayslog.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,44 @@
//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 (
"context"
"fmt"
"log/slog"
"os"
"runtime"
"strings"
"time"

"github.com/shogo82148/aws-xray-yasdk-go/xray"
"github.com/shogo82148/aws-xray-yasdk-go/xray/xraylog"
)

func getLogLevelFromEnv() xraylog.LogLevel {
level := xraylog.LogLevelInfo
if os.Getenv("AWS_XRAY_DEBUG_MODE") != "" {
level = xraylog.LogLevelDebug
} else if env := os.Getenv("AWS_XRAY_LOG_LEVEL"); env != "" {
env = strings.ToLower(env)
switch env {
case "debug":
level = xraylog.LogLevelDebug
case "info":
level = xraylog.LogLevelInfo
case "warn":
level = xraylog.LogLevelWarn
case "error":
level = xraylog.LogLevelError
case "silent":
return xraylog.LogLevelSilent
}
}
return level
}

var _ slog.Handler = (*handler)(nil)

type handler struct {
Expand Down Expand Up @@ -76,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 @@ -85,16 +109,34 @@ func NewHandler(parent slog.Handler, traceIDKey string) slog.Handler {
}

type xrayLogger struct {
h slog.Handler
h slog.Handler
minLevel xraylog.LogLevel
}

// 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 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 &xrayLogger{h}
return NewXRayLoggerWithMinLevel(h, getLogLevelFromEnv())
}

// NewXRayLoggerWithMinLevel 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.
func NewXRayLoggerWithMinLevel(h slog.Handler, minLogLevel xraylog.LogLevel) xraylog.Logger {
if minLogLevel == xraylog.LogLevelSilent {
return xraylog.NullLogger{}
}
return &xrayLogger{h, minLogLevel}
}

func (l *xrayLogger) Log(ctx context.Context, level xraylog.LogLevel, msg fmt.Stringer) {
if level < l.minLevel {
return
}

lv := xraylogLevelToSlog(level)
if !l.h.Enabled(ctx, lv) {
return
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 68ba743

Please sign in to comment.