Skip to content

Commit

Permalink
Log to stderr by default, not stdout
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
adriancable authored and Sean-Der committed Mar 28, 2024
1 parent 6d08bc6 commit 3575446
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
4 changes: 2 additions & 2 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Check warning on line 159 in logger.go

View check run for this annotation

Codecov / codecov/patch

logger.go#L159

Added line #L159 was not covered by tests
}
logger := &DefaultLeveledLogger{
writer: &loggerWriter{output: writer},
Expand All @@ -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

Check warning on line 185 in logger.go

View check run for this annotation

Codecov / codecov/patch

logger.go#L185

Added line #L185 was not covered by tests

logLevels := map[string]LogLevel{
"DISABLE": LogLevelDisabled,
Expand Down
6 changes: 3 additions & 3 deletions logging_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 3575446

Please sign in to comment.