refactor(logger): extract DisableNoisyPionLogs helper

This commit is contained in:
zarazaex69
2026-05-18 05:41:04 +03:00
parent 143f6dd8a6
commit 4ce5d0356e
3 changed files with 85 additions and 1 deletions

View File

@@ -72,6 +72,7 @@ func run() error {
}
func runWithArgs(args []string) error {
logger.DisableNoisyPionLogs()
session.RegisterDefaults()
if len(args) != 1 || args[0] == "-h" || args[0] == "--help" || args[0] == "-help" {
@@ -335,9 +336,9 @@ func (f filteredWriter) Write(p []byte) (int, error) {
func configureLogging(debug bool) {
log.SetOutput(filteredWriter{w: os.Stderr})
logger.DisableNoisyPionLogs()
if debug {
logger.SetVerbose(true)
_ = os.Setenv("PION_LOG_DISABLE", "turnc")
return
}
_ = os.Setenv("PION_LOG_DISABLE", "all")

View File

@@ -4,6 +4,7 @@ package logger
import (
"fmt"
"log"
"os"
"strings"
"sync/atomic"
@@ -13,6 +14,72 @@ import (
// verboseEnabled controls whether verbose and debug logging is enabled.
var verboseEnabled atomic.Bool //nolint:gochecknoglobals // package-level state intentional
// DisableNoisyPionLogs suppresses Pion scopes that are known to emit
// high-volume non-actionable background noise.
func DisableNoisyPionLogs() {
mergePionLogDisable("turnc")
removePionLogScopes([]string{"turnc"}, "ERROR", "WARN", "INFO", "DEBUG", "TRACE")
}
func mergePionLogDisable(scopes ...string) {
const envKey = "PION_LOG_DISABLE"
current := strings.TrimSpace(os.Getenv(envKey))
if strings.EqualFold(current, "all") {
return
}
seen := make(map[string]struct{})
var merged []string
for _, scope := range strings.Split(current, ",") {
scope = strings.TrimSpace(strings.ToLower(scope))
if scope == "" {
continue
}
seen[scope] = struct{}{}
merged = append(merged, scope)
}
for _, scope := range scopes {
scope = strings.TrimSpace(strings.ToLower(scope))
if scope == "" {
continue
}
if _, ok := seen[scope]; ok {
continue
}
seen[scope] = struct{}{}
merged = append(merged, scope)
}
_ = os.Setenv(envKey, strings.Join(merged, ","))
}
func removePionLogScopes(scopes []string, levels ...string) {
remove := make(map[string]struct{}, len(scopes))
for _, scope := range scopes {
scope = strings.TrimSpace(strings.ToLower(scope))
if scope != "" {
remove[scope] = struct{}{}
}
}
for _, level := range levels {
envKey := "PION_LOG_" + level
current := strings.TrimSpace(os.Getenv(envKey))
if current == "" || strings.EqualFold(current, "all") {
continue
}
var kept []string
for _, scope := range strings.Split(current, ",") {
scope = strings.TrimSpace(strings.ToLower(scope))
if scope == "" {
continue
}
if _, drop := remove[scope]; drop {
continue
}
kept = append(kept, scope)
}
_ = os.Setenv(envKey, strings.Join(kept, ","))
}
}
// SetVerbose enables or disables verbose/debug logging.
func SetVerbose(enabled bool) {
verboseEnabled.Store(enabled)

View File

@@ -3,6 +3,7 @@ package logger
import (
"bytes"
"log"
"os"
"strings"
"testing"
)
@@ -89,3 +90,18 @@ func TestPionLoggerDropsTURNRefreshNoise(t *testing.T) {
t.Fatalf("expected normal warning to pass through, got %q", got)
}
}
func TestDisableNoisyPionLogsMergesTurncScope(t *testing.T) {
t.Setenv("PION_LOG_DISABLE", "ice")
t.Setenv("PION_LOG_ERROR", "turnc,ice")
DisableNoisyPionLogs()
got := os.Getenv("PION_LOG_DISABLE")
if !strings.Contains(got, "ice") || !strings.Contains(got, "turnc") {
t.Fatalf("PION_LOG_DISABLE = %q, want ice and turnc", got)
}
if got := os.Getenv("PION_LOG_ERROR"); got != "ice" {
t.Fatalf("PION_LOG_ERROR = %q, want ice", got)
}
}