mirror of
https://github.com/openlibrecommunity/olcrtc.git
synced 2026-07-03 14:05:39 +02:00
refactor(logger): extract DisableNoisyPionLogs helper
This commit is contained in:
+2
-1
@@ -72,6 +72,7 @@ func run() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func runWithArgs(args []string) error {
|
func runWithArgs(args []string) error {
|
||||||
|
logger.DisableNoisyPionLogs()
|
||||||
session.RegisterDefaults()
|
session.RegisterDefaults()
|
||||||
|
|
||||||
if len(args) != 1 || args[0] == "-h" || args[0] == "--help" || args[0] == "-help" {
|
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) {
|
func configureLogging(debug bool) {
|
||||||
log.SetOutput(filteredWriter{w: os.Stderr})
|
log.SetOutput(filteredWriter{w: os.Stderr})
|
||||||
|
logger.DisableNoisyPionLogs()
|
||||||
if debug {
|
if debug {
|
||||||
logger.SetVerbose(true)
|
logger.SetVerbose(true)
|
||||||
_ = os.Setenv("PION_LOG_DISABLE", "turnc")
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
_ = os.Setenv("PION_LOG_DISABLE", "all")
|
_ = os.Setenv("PION_LOG_DISABLE", "all")
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ package logger
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
|
|
||||||
@@ -13,6 +14,72 @@ import (
|
|||||||
// verboseEnabled controls whether verbose and debug logging is enabled.
|
// verboseEnabled controls whether verbose and debug logging is enabled.
|
||||||
var verboseEnabled atomic.Bool //nolint:gochecknoglobals // package-level state intentional
|
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.
|
// SetVerbose enables or disables verbose/debug logging.
|
||||||
func SetVerbose(enabled bool) {
|
func SetVerbose(enabled bool) {
|
||||||
verboseEnabled.Store(enabled)
|
verboseEnabled.Store(enabled)
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package logger
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"log"
|
"log"
|
||||||
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
@@ -89,3 +90,18 @@ func TestPionLoggerDropsTURNRefreshNoise(t *testing.T) {
|
|||||||
t.Fatalf("expected normal warning to pass through, got %q", got)
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user