From 53d54a09b0d5905d11dc1b23c9fd9a758035d80c Mon Sep 17 00:00:00 2001 From: yusing Date: Wed, 30 Apr 2025 18:17:09 +0800 Subject: [PATCH] fix: rotate result file size, add "saved" and omit empty values --- internal/logging/accesslog/rotate.go | 36 ++++++++++++++++++---------- 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/internal/logging/accesslog/rotate.go b/internal/logging/accesslog/rotate.go index e447f13..c7a7dcb 100644 --- a/internal/logging/accesslog/rotate.go +++ b/internal/logging/accesslog/rotate.go @@ -30,17 +30,29 @@ type RotateResult struct { } func (r *RotateResult) Print(logger *zerolog.Logger) { - logger.Info(). - Str("original_size", strutils.FormatByteSize(r.OriginalSize)). - Str("bytes_read", strutils.FormatByteSize(r.NumBytesRead)). - Str("bytes_keep", strutils.FormatByteSize(r.NumBytesKeep)). - Int("lines_read", r.NumLinesRead). - Int("lines_keep", r.NumLinesKeep). - Int("lines_invalid", r.NumLinesInvalid). + event := logger.Info(). + Str("original_size", strutils.FormatByteSize(r.OriginalSize)) + if r.NumBytesRead > 0 { + event.Str("bytes_read", strutils.FormatByteSize(r.NumBytesRead)) + } + if r.NumBytesKeep > 0 { + event.Str("bytes_keep", strutils.FormatByteSize(r.NumBytesKeep)) + } + if r.NumLinesRead > 0 { + event.Int("lines_read", r.NumLinesRead) + } + if r.NumLinesKeep > 0 { + event.Int("lines_keep", r.NumLinesKeep) + } + if r.NumLinesInvalid > 0 { + event.Int("lines_invalid", r.NumLinesInvalid) + } + event.Str("saved", strutils.FormatByteSize(r.OriginalSize-r.NumBytesKeep)). Msg("log rotate result") } func (r *RotateResult) Add(other *RotateResult) { + r.OriginalSize += other.OriginalSize r.NumBytesRead += other.NumBytesRead r.NumBytesKeep += other.NumBytesKeep r.NumLinesRead += other.NumLinesRead @@ -102,16 +114,16 @@ func rotateLogFileByPolicy(file supportRotate, config *Retention) (result *Rotat return nil, err } + // nothing to rotate, return the nothing + if fileSize == 0 { + return nil, nil + } + s := NewBackScanner(file, fileSize, defaultChunkSize) result = &RotateResult{ OriginalSize: fileSize, } - // nothing to rotate, return the nothing - if result.OriginalSize == 0 { - return nil, nil - } - // Store the line positions and sizes we want to keep linesToKeep := make([]lineInfo, 0) lastLineValid := false