From 3575446b68929530c675935fd0271da0d2038bec Mon Sep 17 00:00:00 2001 From: Adrian Cable Date: Thu, 19 May 2022 17:48:57 -0700 Subject: [PATCH] Log to stderr by default, not stdout Go's log package writes to stderr by default (like all/most loggers). However pion's logger by default writes to stdout by default, even though pion's packages that use the logger override this to stderr. So projects using both will end up with a mix of log messages to stdout and stderr, which isn't desirable when trying to separate out the log output. The unit test for the pion logger also writes to stdout, which is inconsistent with pion's actual usage whereby the default output is overridden with stderr. This PR changes the default logging fd to stderr, and also changes the logger test to be consistent with pion's usage in writing to stderr. --- logger.go | 4 ++-- logging_test.go | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/logger.go b/logger.go index 5a78107..f655e52 100644 --- a/logger.go +++ b/logger.go @@ -156,7 +156,7 @@ func (ll *DefaultLeveledLogger) Errorf(format string, args ...interface{}) { // NewDefaultLeveledLoggerForScope returns a configured LeveledLogger func NewDefaultLeveledLoggerForScope(scope string, level LogLevel, writer io.Writer) *DefaultLeveledLogger { if writer == nil { - writer = os.Stdout + writer = os.Stderr } logger := &DefaultLeveledLogger{ writer: &loggerWriter{output: writer}, @@ -182,7 +182,7 @@ func NewDefaultLoggerFactory() *DefaultLoggerFactory { factory := DefaultLoggerFactory{} factory.DefaultLogLevel = LogLevelError factory.ScopeLevels = make(map[string]LogLevel) - factory.Writer = os.Stdout + factory.Writer = os.Stderr logLevels := map[string]LogLevel{ "DISABLE": LogLevelDisabled, diff --git a/logging_test.go b/logging_test.go index a7b40cd..daf5302 100644 --- a/logging_test.go +++ b/logging_test.go @@ -73,7 +73,7 @@ func testErrorLevel(t *testing.T, logger *logging.DefaultLeveledLogger) { func TestDefaultLoggerFactory(t *testing.T) { f := logging.DefaultLoggerFactory{ - Writer: os.Stdout, + Writer: os.Stderr, DefaultLogLevel: logging.LogLevelWarn, ScopeLevels: map[string]logging.LogLevel{ "foo": logging.LogLevelDebug, @@ -100,7 +100,7 @@ func TestDefaultLoggerFactory(t *testing.T) { func TestDefaultLogger(t *testing.T) { logger := logging. - NewDefaultLeveledLoggerForScope("test1", logging.LogLevelWarn, os.Stdout) + NewDefaultLeveledLoggerForScope("test1", logging.LogLevelWarn, os.Stderr) testNoDebugLevel(t, logger) testWarnLevel(t, logger) @@ -109,7 +109,7 @@ func TestDefaultLogger(t *testing.T) { func TestSetLevel(t *testing.T) { logger := logging. - NewDefaultLeveledLoggerForScope("testSetLevel", logging.LogLevelWarn, os.Stdout) + NewDefaultLeveledLoggerForScope("testSetLevel", logging.LogLevelWarn, os.Stderr) testNoDebugLevel(t, logger) logger.SetLevel(logging.LogLevelDebug)