diff --git a/weed/glog/glog_file.go b/weed/glog/glog_file.go index 5acafa03a..782a6e048 100644 --- a/weed/glog/glog_file.go +++ b/weed/glog/glog_file.go @@ -25,6 +25,7 @@ import ( "os" "os/user" "path/filepath" + "sort" "strings" "sync" "time" @@ -32,6 +33,7 @@ import ( // MaxSize is the maximum size of a log file in bytes. var MaxSize uint64 = 1024 * 1024 * 1800 +var MaxFileCount = 5 // logDirs lists the candidate directories for new log files. var logDirs []string @@ -43,8 +45,9 @@ var logDir = flag.String("logdir", "", "If non-empty, write log files in this di func createLogDirs() { if *logDir != "" { logDirs = append(logDirs, *logDir) + } else { + logDirs = append(logDirs, os.TempDir()) } - logDirs = append(logDirs, os.TempDir()) } var ( @@ -96,6 +99,15 @@ func logName(tag string, t time.Time) (name, link string) { return name, program + "." + tag } +func prefix(tag string) string { + return fmt.Sprintf("%s.%s.%s.log.%s.", + program, + host, + userName, + tag, + ) +} + var onceLogDirs sync.Once // create creates a new log file and returns the file and its filename, which @@ -108,8 +120,29 @@ func create(tag string, t time.Time) (f *os.File, filename string, err error) { return nil, "", errors.New("log: no log dirs") } name, link := logName(tag, t) + logPrefix := prefix(tag) var lastErr error for _, dir := range logDirs { + + // remove old logs + entries, _ := os.ReadDir(dir) + var previousLogs []string + for _, entry := range entries { + if strings.HasPrefix(entry.Name(), logPrefix) { + previousLogs = append(previousLogs, entry.Name()) + } + } + if len(previousLogs) >= MaxFileCount { + sort.Strings(previousLogs) + for i, entry := range previousLogs { + if i > len(previousLogs)-MaxFileCount { + break + } + os.Remove(filepath.Join(dir, entry)) + } + } + + // create new log file fname := filepath.Join(dir, name) f, err := os.Create(fname) if err == nil { diff --git a/weed/weed.go b/weed/weed.go index a91338315..60d6bf683 100644 --- a/weed/weed.go +++ b/weed/weed.go @@ -46,7 +46,8 @@ func init() { } func main() { - glog.MaxSize = 1024 * 1024 * 32 + glog.MaxSize = 1024 * 1024 * 10 + glog.MaxFileCount = 5 rand.Seed(time.Now().UnixNano()) flag.Usage = usage