Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 30 additions & 43 deletions pkg/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import (
"sync"
"time"

lipgloss "charm.land/lipgloss/v2"
"github.com/github/gh-aw/pkg/styles"
"github.com/github/gh-aw/pkg/timeutil"
"github.com/github/gh-aw/pkg/tty"
)

// Logger represents a debug logger for a specific namespace.
Expand All @@ -18,7 +19,7 @@ type Logger struct {
enabled bool
lastLog time.Time
mu sync.Mutex
color string
label string
}

var (
Expand All @@ -29,27 +30,21 @@ var (
// DEBUG_COLORS environment variable to control color output.
debugColors = os.Getenv("DEBUG_COLORS") != "0"

// Check if stderr is a terminal (for color support).
isTTY = tty.IsStderrTerminal()

// Color palette - chosen to be readable on both light and dark backgrounds.
// Using ANSI 256-color codes for better compatibility.
colorPalette = []string{
"\033[38;5;33m", // Blue
"\033[38;5;35m", // Green
"\033[38;5;166m", // Orange
"\033[38;5;125m", // Purple
"\033[38;5;37m", // Cyan
"\033[38;5;161m", // Magenta
"\033[38;5;136m", // Yellow
"\033[38;5;124m", // Red
"\033[38;5;28m", // Dark green
"\033[38;5;63m", // Light blue
"\033[38;5;95m", // Brown
"\033[38;5;21m", // Dark blue
// Color palette for namespace coloring, using adaptive styles.
colorPalette = []lipgloss.Style{
lipgloss.NewStyle().Foreground(styles.ColorInfo),
lipgloss.NewStyle().Foreground(styles.ColorSuccess),
lipgloss.NewStyle().Foreground(styles.ColorWarning),
lipgloss.NewStyle().Foreground(styles.ColorPurple),
lipgloss.NewStyle().Foreground(styles.ColorYellow),
lipgloss.NewStyle().Foreground(styles.ColorError),
lipgloss.NewStyle().Foreground(styles.ColorComment),
lipgloss.NewStyle().Foreground(styles.ColorForeground),
lipgloss.NewStyle().Foreground(styles.ColorBorder),
lipgloss.NewStyle().Foreground(styles.ColorInfo),
lipgloss.NewStyle().Foreground(styles.ColorSuccess),
lipgloss.NewStyle().Foreground(styles.ColorPurple),
}

colorReset = "\033[0m"
)

// initDebugEnv resolves the effective debug pattern.
Expand All @@ -74,35 +69,35 @@ func initDebugEnv() string {
// DEBUG=ns1,ns2 - enables specific namespaces
// DEBUG=ns:*,-ns:skip - enables namespace but excludes specific patterns
//
// Colors are automatically assigned to each namespace if DEBUG_COLORS != "0" and stderr is a TTY.
// Colors are automatically assigned to each namespace if DEBUG_COLORS != "0".
func New(namespace string) *Logger {
enabled := computeEnabled(namespace)
color := selectColor(namespace)
label := selectNamespaceLabel(namespace)
return &Logger{
namespace: namespace,
enabled: enabled,
lastLog: time.Now(),
color: color,
label: label,
}
}

// selectColor selects a color for the namespace based on its hash.
func selectColor(namespace string) string {
if !debugColors || !isTTY {
return ""
// selectNamespaceLabel renders the namespace label with a hash-selected style.
func selectNamespaceLabel(namespace string) string {
if !debugColors {
return namespace
}

// Use FNV-1a hash for consistent color assignment
h := fnv.New32a()
// hash.Hash.Write never returns an error in practice, but check to satisfy gosec G104
if _, err := h.Write([]byte(namespace)); err != nil {
// Return empty string (no color) if write somehow fails
return ""
// Return plain namespace (no color) if write somehow fails
return namespace
}
hash := h.Sum32()

// Select color from palette based on hash
return colorPalette[hash%uint32(len(colorPalette))]
// Select color from palette based on hash and render namespace with it.
return colorPalette[hash%uint32(len(colorPalette))].Render(namespace)
}

// Enabled returns whether this logger is enabled
Expand All @@ -124,11 +119,7 @@ func (l *Logger) Printf(format string, args ...any) {
l.mu.Unlock()

message := fmt.Sprintf(format, args...)
if l.color != "" {
fmt.Fprintf(os.Stderr, "%s%s%s %s +%s\n", l.color, l.namespace, colorReset, message, timeutil.FormatDuration(diff))
} else {
fmt.Fprintf(os.Stderr, "%s %s +%s\n", l.namespace, message, timeutil.FormatDuration(diff))
}
lipgloss.Fprintf(os.Stderr, "%s %s +%s\n", l.label, message, timeutil.FormatDuration(diff))
}

// Print prints a message if the logger is enabled.
Expand All @@ -145,11 +136,7 @@ func (l *Logger) Print(args ...any) {
l.mu.Unlock()

message := fmt.Sprint(args...)
if l.color != "" {
fmt.Fprintf(os.Stderr, "%s%s%s %s +%s\n", l.color, l.namespace, colorReset, message, timeutil.FormatDuration(diff))
} else {
fmt.Fprintf(os.Stderr, "%s %s +%s\n", l.namespace, message, timeutil.FormatDuration(diff))
}
lipgloss.Fprintf(os.Stderr, "%s %s +%s\n", l.label, message, timeutil.FormatDuration(diff))
}

// computeEnabled computes whether a namespace matches the DEBUG patterns
Expand Down
66 changes: 28 additions & 38 deletions pkg/logger/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package logger
import (
"bytes"
"os"
"slices"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -278,57 +277,48 @@ func TestLogger_TimeDiff(t *testing.T) {
}

func TestColorSelection(t *testing.T) {
// Test that selectColor returns consistent colors for the same namespace
color1 := selectColor("test:namespace")
color2 := selectColor("test:namespace")
// Test that selectNamespaceLabel returns consistent colors for the same namespace
color1 := selectNamespaceLabel("test:namespace")
color2 := selectNamespaceLabel("test:namespace")
if color1 != color2 {
t.Errorf("selectColor should return same color for same namespace")
t.Errorf("selectNamespaceLabel should return same color for same namespace")
}

// Test that different namespaces can get different colors
// (not guaranteed but likely with our hash function)
color3 := selectColor("other:namespace")
// Just verify it's a valid color from palette or empty
found := color3 == ""
if slices.Contains(colorPalette, color3) {
found = true
if color1 == "" {
t.Error("selectNamespaceLabel should return non-empty namespace label")
}
if !strings.Contains(color1, "test:namespace") {
t.Errorf("selectNamespaceLabel should include namespace text, got %q", color1)
}
if !found {
t.Errorf("selectColor returned invalid color: %q", color3)
if !strings.Contains(color1, "\x1b[") {
t.Errorf("selectNamespaceLabel should include ANSI styling when colors are enabled, got %q", color1)
}

// When colors are disabled, selectNamespaceLabel should return plain namespace text.
origDebugColors := debugColors
debugColors = false
t.Cleanup(func() { debugColors = origDebugColors })
if got := selectNamespaceLabel("plain:namespace"); got != "plain:namespace" {
t.Errorf("selectNamespaceLabel should return plain namespace when debugColors=false, got %q", got)
}
}

func TestColorDisabling(t *testing.T) {
func TestNoColorEnvironment(t *testing.T) {
// Save original values
origDebugColors := debugColors
origIsTTY := isTTY
defer func() {
debugColors = origDebugColors
isTTY = origIsTTY
}()

// Test with colors disabled via DEBUG_COLORS
debugColors = false
isTTY = true
color := selectColor("test:namespace")
if color != "" {
t.Errorf("selectColor should return empty when debugColors=false, got %q", color)
}

// Test with TTY disabled
debugEnv = "*"
debugColors = true
isTTY = false
color = selectColor("test:namespace")
if color != "" {
t.Errorf("selectColor should return empty when isTTY=false, got %q", color)
}
t.Setenv("NO_COLOR", "1")

// Test with both enabled
debugColors = true
isTTY = true
color = selectColor("test:namespace")
if color == "" {
t.Error("selectColor should return color when both enabled")
logger := New("test:namespace")
output := captureStderr(func() {
logger.Printf("hello")
})
if strings.Contains(output, "\x1b[") {
t.Errorf("logger output should not contain ANSI color escapes when NO_COLOR is set, got %q", output)
}
}

Expand Down