Procházet zdrojové kódy

fix: support gc logs when started (#2046)

* support gc logs

Signed-off-by: yisaer <disxiaofei@163.com>

* add test

Signed-off-by: yisaer <disxiaofei@163.com>

---------

Signed-off-by: yisaer <disxiaofei@163.com>
Song Gao před 1 rokem
rodič
revize
e25cb8e390
2 změnil soubory, kde provedl 90 přidání a 0 odebrání
  1. 38 0
      internal/conf/conf.go
  2. 52 0
      internal/conf/log_test.go

+ 38 - 0
internal/conf/conf.go

@@ -20,6 +20,7 @@ import (
 	"io"
 	"io"
 	"os"
 	"os"
 	"path"
 	"path"
+	"strings"
 	"time"
 	"time"
 
 
 	"github.com/lestrrat-go/file-rotatelogs"
 	"github.com/lestrrat-go/file-rotatelogs"
@@ -222,6 +223,7 @@ func InitConf() {
 		} else if !Config.Basic.ConsoleLog {
 		} else if !Config.Basic.ConsoleLog {
 			Log.SetOutput(logWriter)
 			Log.SetOutput(logWriter)
 		}
 		}
+		gcOutdatedLog(logDir, time.Hour*time.Duration(Config.Basic.MaxAge))
 	} else if Config.Basic.ConsoleLog {
 	} else if Config.Basic.ConsoleLog {
 		Log.SetOutput(os.Stdout)
 		Log.SetOutput(os.Stdout)
 	}
 	}
@@ -309,3 +311,39 @@ func init() {
 	InitLogger()
 	InitLogger()
 	InitClock()
 	InitClock()
 }
 }
+
+func gcOutdatedLog(filePath string, maxDuration time.Duration) {
+	entries, err := os.ReadDir(filePath)
+	if err != nil {
+		Log.Errorf("gc outdated logs when started failed, err:%v", err)
+	}
+	now := time.Now()
+	for _, entry := range entries {
+		if entry.IsDir() {
+			continue
+		}
+		if isLogOutdated(entry.Name(), now, maxDuration) {
+			err := os.Remove(path.Join(filePath, entry.Name()))
+			if err != nil {
+				Log.Errorf("remove outdated log %v failed, err:%v", entry.Name(), err)
+			}
+		}
+	}
+}
+
+func isLogOutdated(name string, now time.Time, maxDuration time.Duration) bool {
+	prefix := fmt.Sprintf("%s.", logFileName)
+	layout := "2006-01-02_15-04-05"
+	if strings.HasPrefix(name, prefix) {
+		logDate := name[len(prefix):]
+		t, err := time.Parse(layout, logDate)
+		if err != nil {
+			Log.Errorf("parse log %v datetime failed, err:%v", name, err)
+			return false
+		}
+		if int64(now.Sub(t))-int64(maxDuration) > 0 {
+			return true
+		}
+	}
+	return false
+}

+ 52 - 0
internal/conf/log_test.go

@@ -0,0 +1,52 @@
+// Copyright 2023 EMQ Technologies Co., Ltd.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package conf
+
+import (
+	"testing"
+	"time"
+
+	"github.com/stretchr/testify/require"
+)
+
+func TestLogOutdated(t *testing.T) {
+	now, err := time.Parse("2006-01-02_15-04-05", "2023-06-29_12-00-00")
+	require.NoError(t, err)
+	maxDuration := 24 * time.Hour
+	testcases := []struct {
+		name   string
+		remove bool
+	}{
+		{
+			name:   "stream.log",
+			remove: false,
+		},
+		{
+			name:   "stream.log.2023-06-20_00-00-00",
+			remove: true,
+		},
+		{
+			name:   "stream.log.2023-06-29_00-00-00",
+			remove: false,
+		},
+		{
+			name:   "stream.log.2023-error",
+			remove: false,
+		},
+	}
+	for _, tc := range testcases {
+		require.Equal(t, tc.remove, isLogOutdated(tc.name, now, maxDuration))
+	}
+}