Commit 20a55b5f authored by Andrei Mihu's avatar Andrei Mihu
Browse files

Log recovered panics in HTTP handler functions at error level rather than info.

parent fdc789a1
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -9,6 +9,7 @@ The format is based on [keep a changelog](http://keepachangelog.com) and this pr
- Build with Go 1.16.4 release.
- Replace Bleve gtreap in-memory store implementation with a more compact version.
- Users kicked from parties now receive a party close event.
- Log recovered panics in HTTP handler functions at error level rather than info.

### Fixed
- Ensure all members are correctly listed in party info when there are multiple concurrent successful joins.
+26 −3
Original line number Diff line number Diff line
@@ -15,6 +15,8 @@
package server

import (
	"bytes"
	"log"
	"os"
	"path/filepath"
	"strings"
@@ -70,14 +72,14 @@ func SetupLogging(tmpLogger *zap.Logger, config Config) (*zap.Logger, *zap.Logge
		multiLogger := NewMultiLogger(consoleLogger, fileLogger)

		if config.GetLogger().Stdout {
			zap.RedirectStdLog(multiLogger)
			RedirectStdLog(multiLogger)
			return multiLogger, multiLogger
		}
		zap.RedirectStdLog(fileLogger)
		RedirectStdLog(fileLogger)
		return fileLogger, multiLogger
	}

	zap.RedirectStdLog(consoleLogger)
	RedirectStdLog(consoleLogger)
	return consoleLogger, consoleLogger
}

@@ -196,3 +198,24 @@ func StackdriverLevelEncoder(l zapcore.Level, enc zapcore.PrimitiveArrayEncoder)
		enc.AppendString("DEFAULT")
	}
}

type RedirectStdLogWriter struct {
	logger *zap.Logger
}

func (r *RedirectStdLogWriter) Write(p []byte) (int, error) {
	s := string(bytes.TrimSpace(p))
	if strings.HasPrefix(s, "http: panic serving") {
		r.logger.Error(s)
	} else {
		r.logger.Info(s)
	}
	return len(s), nil
}

func RedirectStdLog(logger *zap.Logger) {
	log.SetFlags(0)
	log.SetPrefix("")
	skipLogger := logger.WithOptions(zap.AddCallerSkip(3))
	log.SetOutput(&RedirectStdLogWriter{skipLogger})
}