MCPcopy
hub / github.com/keploy/keploy / AddDebugFileSink

Function AddDebugFileSink

utils/log/logger.go:471–502  ·  view source on GitHub ↗

AddDebugFileSink returns a new logger that, in addition to whatever sinks the input logger already had, writes every debug-level-or-above entry to file via a 256 KiB buffered, capBytes-capped pipeline. Used by `keploy cloud replay` to capture the full debug stream for the support bundle without lift

(logger *zap.Logger, file *os.File, capBytes int64)

Source from the content-addressed store, hash-verified

469// Caller owns `file`. Call DebugFileSink.Flush before closing the file
470// at end-of-run.
471func AddDebugFileSink(logger *zap.Logger, file *os.File, capBytes int64) (*zap.Logger, *DebugFileSink) {
472 if logger == nil || file == nil {
473 return logger, nil
474 }
475 if capBytes <= 0 {
476 capBytes = 100 << 20 // 100 MiB default
477 }
478 capped := newCappedWriteSyncer(zapcore.AddSync(file), capBytes)
479 // FlushInterval defaults to 30s in zap, which is too coarse: a
480 // short test set (e.g. 2 test cases finishing in < 30s) can write
481 // less than the 256 KiB buffer size and never trigger an
482 // auto-flush. If the agent process is then killed by
483 // docker-compose teardown before main's defer runs Flush(), the
484 // buffered bytes are LOST and the per-test-set file ends up
485 // empty. 1s keeps the buffering performance benefit while
486 // bounding data-loss-on-crash to ~1s of records.
487 buffered := &zapcore.BufferedWriteSyncer{WS: capped, Size: 256 << 10, FlushInterval: time.Second}
488 encoder := NewANSIConsoleEncoder(LogCfg.EncoderConfig)
489 debugCore := newRedactingCore(zapcore.NewCore(
490 encoder,
491 wrapWriter(buffered),
492 zap.NewAtomicLevelAt(zap.DebugLevel),
493 ))
494 newLogger := logger.WithOptions(zap.WrapCore(func(c zapcore.Core) zapcore.Core {
495 return zapcore.NewTee(c, debugCore)
496 }))
497 return newLogger, &DebugFileSink{
498 capped: capped,
499 buffered: buffered,
500 originPath: file.Name(),
501 }
502}
503
504// Swap atomically swaps the underlying file the sink writes to. The
505// caller opens newFile; the previous file is returned so the caller

Calls 5

newCappedWriteSyncerFunction · 0.85
NewANSIConsoleEncoderFunction · 0.85
newRedactingCoreFunction · 0.85
wrapWriterFunction · 0.85
NameMethod · 0.80