Skip to content

Commit 39ace45

Browse files
logging: Adjustments to BufferedLog to keep logs in the correct order (#7257)
* logging: Adjustments to BufferedLog to keep logs in the correct order * Ignore lints
1 parent 0ba8786 commit 39ace45

File tree

3 files changed

+37
-6
lines changed

3 files changed

+37
-6
lines changed

cmd/commandfuncs.go

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,9 @@ func cmdRun(fl Flags) (int, error) {
182182

183183
undoMaxProcs := setResourceLimits(logger)
184184
defer undoMaxProcs()
185+
// release the local reference to the undo function so it can be GC'd;
186+
// the deferred call above has already captured the actual function value.
187+
undoMaxProcs = nil //nolint:ineffassign,wastedassign
185188

186189
configFlag := fl.String("config")
187190
configAdapterFlag := fl.String("adapter")
@@ -252,12 +255,16 @@ func cmdRun(fl Flags) (int, error) {
252255
logBuffer.FlushTo(defaultLogger)
253256
return caddy.ExitCodeFailedStartup, fmt.Errorf("loading initial config: %v", err)
254257
}
258+
// release the reference to the config so it can be GC'd
259+
config = nil //nolint:ineffassign,wastedassign
255260

256-
// at this stage the config will have replaced
257-
// the default logger to the configured one, so
258-
// we can now flush the buffered logs, then log
259-
// that the config is running.
260-
logBuffer.FlushTo(caddy.Log())
261+
// at this stage the config will have replaced the
262+
// default logger to the configured one, so we can
263+
// log normally, now that the config is running.
264+
// also clear our ref to the buffer so it can get GC'd
265+
logger = caddy.Log()
266+
defaultLogger = nil //nolint:ineffassign,wastedassign
267+
logBuffer = nil //nolint:wastedassign,ineffassign
261268
logger.Info("serving initial configuration")
262269

263270
// if we are to report to another process the successful start
@@ -273,12 +280,16 @@ func cmdRun(fl Flags) (int, error) {
273280
return caddy.ExitCodeFailedStartup,
274281
fmt.Errorf("dialing confirmation address: %v", err)
275282
}
276-
defer conn.Close()
277283
_, err = conn.Write(confirmationBytes)
278284
if err != nil {
279285
return caddy.ExitCodeFailedStartup,
280286
fmt.Errorf("writing confirmation bytes to %s: %v", pingbackFlag, err)
281287
}
288+
// close (non-defer because we `select {}` below)
289+
// and release references so they can be GC'd
290+
conn.Close()
291+
confirmationBytes = nil //nolint:ineffassign,wastedassign
292+
conn = nil //nolint:wastedassign,ineffassign
282293
}
283294

284295
// if enabled, reload config file automatically on changes
@@ -306,6 +317,9 @@ func cmdRun(fl Flags) (int, error) {
306317
}
307318
}
308319

320+
// release the last local logger reference
321+
logger = nil //nolint:wastedassign,ineffassign
322+
309323
select {}
310324
}
311325

internal/logbuffer.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ type LogBufferCore struct {
2929
level zapcore.LevelEnabler
3030
}
3131

32+
type LogBufferCoreInterface interface {
33+
zapcore.Core
34+
FlushTo(*zap.Logger)
35+
}
36+
3237
func NewLogBufferCore(level zapcore.LevelEnabler) *LogBufferCore {
3338
return &LogBufferCore{
3439
level: level,
@@ -70,3 +75,8 @@ func (c *LogBufferCore) FlushTo(logger *zap.Logger) {
7075
c.entries = nil
7176
c.fields = nil
7277
}
78+
79+
var (
80+
_ zapcore.Core = (*LogBufferCore)(nil)
81+
_ LogBufferCoreInterface = (*LogBufferCore)(nil)
82+
)

logging.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,13 @@ func (logging *Logging) setupNewDefault(ctx Context) error {
192192
)
193193
}
194194

195+
// if we had a buffered core, flush its contents ASAP
196+
// before we try to log anything else, so the order of
197+
// logs is preserved
198+
if oldBufferCore, ok := oldDefault.logger.Core().(*internal.LogBufferCore); ok {
199+
oldBufferCore.FlushTo(newDefault.logger)
200+
}
201+
195202
return nil
196203
}
197204

0 commit comments

Comments
 (0)