From 38da65a8486fb8f7ce7e227de433e6492c2d4762 Mon Sep 17 00:00:00 2001 From: marcobaobao Date: Wed, 18 Sep 2024 17:50:14 +0200 Subject: [PATCH] reduced log rotation memory usage --- server/logging/file_logger.go | 59 +++++++++++++++-------------------- server/server.go | 2 ++ 2 files changed, 28 insertions(+), 33 deletions(-) diff --git a/server/logging/file_logger.go b/server/logging/file_logger.go index e83a717..3bb6fb6 100644 --- a/server/logging/file_logger.go +++ b/server/logging/file_logger.go @@ -3,21 +3,23 @@ package logging import ( "compress/gzip" "io" + "log/slog" "os" + "strings" "sync" "time" ) /* - File base logger with log-rotate capabilities. - The rotate process must be initiated from an external goroutine. +implements io.Writer interface - After rotation the previous logs file are compressed with gzip algorithm. +File base logger with log-rotate capabilities. +The rotate process must be initiated from an external goroutine. - The rotated log follows this naming: [filename].UTC time.gz -*/ +After rotation the previous logs file are compressed with gzip algorithm. -// implements io.Writer interface +The rotated log follows this naming: [filename].UTC time.gz +*/ type LogRotateWriter struct { mu sync.Mutex fd *os.File @@ -40,50 +42,41 @@ func (w *LogRotateWriter) Write(b []byte) (int, error) { } func (w *LogRotateWriter) Rotate() error { - var err error + slog.Info("started log rotation") + w.mu.Lock() - gzFile, err := os.Create(w.filename + "." + time.Now().Format(time.RFC3339) + ".gz") + gzFile, err := os.Create(strings.TrimSuffix(w.filename, ".log") + "-" + time.Now().Format(time.RFC3339) + ".log.gz") if err != nil { return err } - data, err := io.ReadAll(w.fd) - if err != nil { - return err - } + zw := gzip.NewWriter(gzFile) defer func() { - w.mu.Unlock() - w.gzipLog(gzFile, &data) + zw.Close() + zw.Flush() + gzFile.Close() }() - _, err = os.Stat(w.filename) - if err != nil { + if _, err := os.Stat(w.filename); err != nil { return err } - if w.fd != nil { - err = w.fd.Close() - w.fd = nil - if err != nil { - return err - } - } + fd, _ := os.Open(w.filename) + io.Copy(zw, fd) + fd.Close() - err = os.Remove(w.filename) - if err != nil { + w.fd.Close() + + if err := os.Remove(w.filename); err != nil { return err } - w.fd, err = os.Create(w.filename) - return err -} + w.fd, _ = os.Create(w.filename) -func (w *LogRotateWriter) gzipLog(wr io.Writer, data *[]byte) error { - if _, err := gzip.NewWriter(wr).Write(*data); err != nil { - return err - } + w.mu.Unlock() + slog.Info("ended log rotation") - return nil + return err } diff --git a/server/server.go b/server/server.go index 418369c..f757d13 100644 --- a/server/server.go +++ b/server/server.go @@ -68,6 +68,8 @@ func RunBlocking(rc *RunConfig) { panic(err) } + defer logger.Rotate() + go func() { for { time.Sleep(time.Hour * 24)