mirror of
https://github.com/caddyserver/caddy.git
synced 2026-05-13 09:06:41 +00:00
logging: Buffer the logs before config is loaded (#7245)
This commit is contained in:
parent
d9cc24f3df
commit
012b4b3d40
3 changed files with 107 additions and 1 deletions
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()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue