diff --git a/pkg/logger/logger.go b/pkg/logger/logger.go index a0303644102..f7e169e63f5 100644 --- a/pkg/logger/logger.go +++ b/pkg/logger/logger.go @@ -23,7 +23,8 @@ type Logger struct { var ( // DEBUG environment variable value, read once at initialization. - debugEnv = os.Getenv("DEBUG") + // If DEBUG is not set but ACTIONS_RUNNER_DEBUG=true, all loggers are enabled. + debugEnv = initDebugEnv() // DEBUG_COLORS environment variable to control color output. debugColors = os.Getenv("DEBUG_COLORS") != "0" @@ -51,6 +52,19 @@ var ( colorReset = "\033[0m" ) +// initDebugEnv resolves the effective debug pattern. +// If DEBUG is set, it takes precedence. Otherwise, if ACTIONS_RUNNER_DEBUG=true, +// all loggers are enabled (equivalent to DEBUG=*). +func initDebugEnv() string { + if d := os.Getenv("DEBUG"); d != "" { + return d + } + if os.Getenv("ACTIONS_RUNNER_DEBUG") == "true" { + return "*" + } + return "" +} + // New creates a new Logger for the given namespace. // The enabled state is computed at construction time based on the DEBUG environment variable. // DEBUG syntax follows https://www.npmjs.com/package/debug patterns: diff --git a/pkg/logger/logger_test.go b/pkg/logger/logger_test.go index ebf55df4852..5de982fa99e 100644 --- a/pkg/logger/logger_test.go +++ b/pkg/logger/logger_test.go @@ -332,6 +332,52 @@ func TestColorDisabling(t *testing.T) { } } +func TestInitDebugEnv(t *testing.T) { + tests := []struct { + name string + debugEnvVal string + actionsRunnerDebug string + want string + }{ + { + name: "DEBUG takes precedence over ACTIONS_RUNNER_DEBUG", + debugEnvVal: "cli:*", + actionsRunnerDebug: "true", + want: "cli:*", + }, + { + name: "ACTIONS_RUNNER_DEBUG=true enables all loggers", + debugEnvVal: "", + actionsRunnerDebug: "true", + want: "*", + }, + { + name: "ACTIONS_RUNNER_DEBUG=false does not enable loggers", + debugEnvVal: "", + actionsRunnerDebug: "false", + want: "", + }, + { + name: "neither set returns empty", + debugEnvVal: "", + actionsRunnerDebug: "", + want: "", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + t.Setenv("DEBUG", tt.debugEnvVal) + t.Setenv("ACTIONS_RUNNER_DEBUG", tt.actionsRunnerDebug) + + got := initDebugEnv() + if got != tt.want { + t.Errorf("initDebugEnv() = %q, want %q", got, tt.want) + } + }) + } +} + func TestMatchPattern(t *testing.T) { tests := []struct { name string