mirror of
https://github.com/caddyserver/caddy.git
synced 2026-01-17 01:30:34 +00:00
logging: Buffer the logs before config is loaded (#7245)
This commit is contained in:
17
logging.go
17
logging.go
@@ -28,6 +28,8 @@ import (
|
||||
"go.uber.org/zap"
|
||||
"go.uber.org/zap/zapcore"
|
||||
"golang.org/x/term"
|
||||
|
||||
"github.com/caddyserver/caddy/v2/internal"
|
||||
)
|
||||
|
||||
func init() {
|
||||
@@ -773,6 +775,21 @@ func Log() *zap.Logger {
|
||||
return defaultLogger.logger
|
||||
}
|
||||
|
||||
// BufferedLog sets the default logger to one that buffers
|
||||
// logs before a config is loaded.
|
||||
// Returns the buffered logger, the original default logger
|
||||
// (for flushing on errors), and the buffer core so that the
|
||||
// caller can flush the logs after the config is loaded or
|
||||
// fails to load.
|
||||
func BufferedLog() (*zap.Logger, *zap.Logger, *internal.LogBufferCore) {
|
||||
defaultLoggerMu.Lock()
|
||||
defer defaultLoggerMu.Unlock()
|
||||
origLogger := defaultLogger.logger
|
||||
bufferCore := internal.NewLogBufferCore(zap.InfoLevel)
|
||||
defaultLogger.logger = zap.New(bufferCore)
|
||||
return defaultLogger.logger, origLogger, bufferCore
|
||||
}
|
||||
|
||||
var (
|
||||
coloringEnabled = os.Getenv("NO_COLOR") == "" && os.Getenv("TERM") != "xterm-mono"
|
||||
defaultLogger, _ = newDefaultProductionLog()
|
||||
|
||||
Reference in New Issue
Block a user