From 582c4eb263c29fe6ab0285d49e156cbd81a97634 Mon Sep 17 00:00:00 2001 From: yzj Date: Mon, 8 Aug 2022 15:29:31 +0800 Subject: [PATCH] add log configuration And ingore linter G302 --- .github/workflows/actions.yml | 2 +- coverage.txt | 247 ++++++++++++++++++++++++++++++++++ log/logger.go | 202 +++++++++++++++++++++++++++ log/logger_test.go | 56 ++++++++ profile.out | 1 + 5 files changed, 507 insertions(+), 1 deletion(-) create mode 100644 coverage.txt create mode 100644 profile.out diff --git a/.github/workflows/actions.yml b/.github/workflows/actions.yml index 82c035f..8ab4c7d 100644 --- a/.github/workflows/actions.yml +++ b/.github/workflows/actions.yml @@ -14,7 +14,7 @@ jobs: - name: golangci-lint uses: reviewdog/action-golangci-lint@v1 with: - golangci_lint_flags: "--enable-all --timeout=10m --exclude-use-default=false --tests=false --disable=gochecknoinits,gochecknoglobals,exhaustive,nakedret,wrapcheck" + golangci_lint_flags: "--enable-all --timeout=10m --exclude-use-default=false --tests=false --disable=gochecknoinits,gochecknoglobals,exhaustive,nakedret,wrapcheck -D G302" test: name: test diff --git a/coverage.txt b/coverage.txt new file mode 100644 index 0000000..cd1e24a --- /dev/null +++ b/coverage.txt @@ -0,0 +1,247 @@ + +mode: atomic +mosn.io/pkg/binding/default_validator.go:22.66,25.30 3 21 +mosn.io/pkg/binding/default_validator.go:28.2,28.33 1 21 +mosn.io/pkg/binding/default_validator.go:34.2,34.12 1 20 +mosn.io/pkg/binding/default_validator.go:25.30,27.3 1 13 +mosn.io/pkg/binding/default_validator.go:28.33,30.48 2 13 +mosn.io/pkg/binding/default_validator.go:30.48,32.4 1 1 +mosn.io/pkg/binding/default_validator.go:41.49,44.2 2 1 +mosn.io/pkg/binding/default_validator.go:46.39,47.19 1 14 +mosn.io/pkg/binding/default_validator.go:47.19,50.3 2 1 +mosn.io/pkg/binding/header.go:11.36,13.2 1 0 +mosn.io/pkg/binding/header.go:15.69,17.51 1 0 +mosn.io/pkg/binding/header.go:21.2,21.22 1 0 +mosn.io/pkg/binding/header.go:17.51,19.3 1 0 +mosn.io/pkg/binding/header.go:24.62,26.2 1 0 +mosn.io/pkg/binding/header.go:32.139,34.2 1 0 +mosn.io/pkg/binding/multipart_form_mapping.go:19.138,20.57 1 13 +mosn.io/pkg/binding/multipart_form_mapping.go:24.2,24.65 1 0 +mosn.io/pkg/binding/multipart_form_mapping.go:20.57,22.3 1 13 +mosn.io/pkg/binding/multipart_form_mapping.go:27.135,28.22 1 26 +mosn.io/pkg/binding/multipart_form_mapping.go:52.2,52.77 1 2 +mosn.io/pkg/binding/multipart_form_mapping.go:29.19,30.35 1 6 +mosn.io/pkg/binding/multipart_form_mapping.go:35.22,36.35 1 8 +mosn.io/pkg/binding/multipart_form_mapping.go:41.21,44.30 3 5 +mosn.io/pkg/binding/multipart_form_mapping.go:47.3,48.19 2 4 +mosn.io/pkg/binding/multipart_form_mapping.go:49.21,50.59 1 5 +mosn.io/pkg/binding/multipart_form_mapping.go:31.30,33.20 2 6 +mosn.io/pkg/binding/multipart_form_mapping.go:37.29,39.20 2 8 +mosn.io/pkg/binding/multipart_form_mapping.go:44.30,46.4 1 1 +mosn.io/pkg/binding/multipart_form_mapping.go:55.141,56.31 1 10 +mosn.io/pkg/binding/multipart_form_mapping.go:59.2,59.23 1 9 +mosn.io/pkg/binding/multipart_form_mapping.go:65.2,65.18 1 8 +mosn.io/pkg/binding/multipart_form_mapping.go:56.31,58.3 1 1 +mosn.io/pkg/binding/multipart_form_mapping.go:59.23,61.28 2 13 +mosn.io/pkg/binding/multipart_form_mapping.go:61.28,63.4 1 1 +mosn.io/pkg/binding/protobuf.go:16.38,18.2 1 0 +mosn.io/pkg/binding/protobuf.go:20.73,22.16 2 0 +mosn.io/pkg/binding/protobuf.go:25.2,25.29 1 0 +mosn.io/pkg/binding/protobuf.go:22.16,24.3 1 0 +mosn.io/pkg/binding/protobuf.go:28.69,29.67 1 0 +mosn.io/pkg/binding/protobuf.go:34.2,34.12 1 0 +mosn.io/pkg/binding/protobuf.go:29.67,31.3 1 0 +mosn.io/pkg/binding/uri.go:9.33,11.2 1 0 +mosn.io/pkg/binding/uri.go:13.73,14.39 1 0 +mosn.io/pkg/binding/uri.go:17.2,17.22 1 0 +mosn.io/pkg/binding/uri.go:14.39,16.3 1 0 +mosn.io/pkg/binding/yaml.go:17.34,19.2 1 0 +mosn.io/pkg/binding/yaml.go:21.67,23.2 1 0 +mosn.io/pkg/binding/yaml.go:25.65,27.2 1 1 +mosn.io/pkg/binding/yaml.go:29.53,31.44 2 1 +mosn.io/pkg/binding/yaml.go:34.2,34.22 1 1 +mosn.io/pkg/binding/yaml.go:31.44,33.3 1 0 +mosn.io/pkg/binding/binding.go:88.50,89.30 1 0 +mosn.io/pkg/binding/binding.go:93.2,93.21 1 0 +mosn.io/pkg/binding/binding.go:89.30,91.3 1 0 +mosn.io/pkg/binding/binding.go:94.16,95.14 1 0 +mosn.io/pkg/binding/binding.go:96.25,97.13 1 0 +mosn.io/pkg/binding/binding.go:98.20,99.18 1 0 +mosn.io/pkg/binding/binding.go:100.33,101.17 1 0 +mosn.io/pkg/binding/binding.go:102.16,103.14 1 0 +mosn.io/pkg/binding/binding.go:104.29,105.23 1 0 +mosn.io/pkg/binding/binding.go:106.10,107.14 1 0 +mosn.io/pkg/binding/binding.go:111.38,112.22 1 21 +mosn.io/pkg/binding/binding.go:115.2,115.38 1 21 +mosn.io/pkg/binding/binding.go:112.22,114.3 1 0 +mosn.io/pkg/binding/form.go:17.34,19.2 1 0 +mosn.io/pkg/binding/form.go:21.67,22.40 1 0 +mosn.io/pkg/binding/form.go:25.2,25.62 1 0 +mosn.io/pkg/binding/form.go:30.2,30.47 1 0 +mosn.io/pkg/binding/form.go:33.2,33.22 1 0 +mosn.io/pkg/binding/form.go:22.40,24.3 1 0 +mosn.io/pkg/binding/form.go:25.62,26.34 1 0 +mosn.io/pkg/binding/form.go:26.34,28.4 1 0 +mosn.io/pkg/binding/form.go:30.47,32.3 1 0 +mosn.io/pkg/binding/form.go:36.38,38.2 1 0 +mosn.io/pkg/binding/form.go:40.71,41.40 1 0 +mosn.io/pkg/binding/form.go:44.2,44.51 1 0 +mosn.io/pkg/binding/form.go:47.2,47.22 1 0 +mosn.io/pkg/binding/form.go:41.40,43.3 1 0 +mosn.io/pkg/binding/form.go:44.51,46.3 1 0 +mosn.io/pkg/binding/form.go:50.43,52.2 1 0 +mosn.io/pkg/binding/form.go:54.76,55.62 1 5 +mosn.io/pkg/binding/form.go:58.2,58.76 1 5 +mosn.io/pkg/binding/form.go:62.2,62.22 1 2 +mosn.io/pkg/binding/form.go:55.62,57.3 1 0 +mosn.io/pkg/binding/form.go:58.76,60.3 1 3 +mosn.io/pkg/binding/form_mapping.go:21.59,23.2 1 1 +mosn.io/pkg/binding/form_mapping.go:25.63,27.2 1 4 +mosn.io/pkg/binding/form_mapping.go:31.80,33.2 1 5 +mosn.io/pkg/binding/form_mapping.go:45.139,47.2 1 46 +mosn.io/pkg/binding/form_mapping.go:49.69,52.2 2 28 +mosn.io/pkg/binding/form_mapping.go:54.103,55.31 1 157 +mosn.io/pkg/binding/form_mapping.go:59.2,61.26 2 155 +mosn.io/pkg/binding/form_mapping.go:78.2,78.49 1 106 +mosn.io/pkg/binding/form_mapping.go:88.2,88.29 1 48 +mosn.io/pkg/binding/form_mapping.go:105.2,105.19 1 1 +mosn.io/pkg/binding/form_mapping.go:55.31,57.3 1 2 +mosn.io/pkg/binding/form_mapping.go:61.26,64.20 3 49 +mosn.io/pkg/binding/form_mapping.go:68.3,69.17 2 49 +mosn.io/pkg/binding/form_mapping.go:72.3,72.24 1 38 +mosn.io/pkg/binding/form_mapping.go:75.3,75.23 1 38 +mosn.io/pkg/binding/form_mapping.go:64.20,67.4 2 2 +mosn.io/pkg/binding/form_mapping.go:69.17,71.4 1 11 +mosn.io/pkg/binding/form_mapping.go:72.24,74.4 1 2 +mosn.io/pkg/binding/form_mapping.go:78.49,80.17 2 106 +mosn.io/pkg/binding/form_mapping.go:83.3,83.9 1 95 +mosn.io/pkg/binding/form_mapping.go:80.17,82.4 1 11 +mosn.io/pkg/binding/form_mapping.go:83.9,85.4 1 47 +mosn.io/pkg/binding/form_mapping.go:88.29,92.41 3 47 +mosn.io/pkg/binding/form_mapping.go:103.3,103.23 1 36 +mosn.io/pkg/binding/form_mapping.go:92.41,94.41 2 63 +mosn.io/pkg/binding/form_mapping.go:97.4,98.18 2 61 +mosn.io/pkg/binding/form_mapping.go:101.4,101.29 1 50 +mosn.io/pkg/binding/form_mapping.go:94.41,95.13 1 2 +mosn.io/pkg/binding/form_mapping.go:98.18,100.5 1 11 +mosn.io/pkg/binding/form_mapping.go:113.109,120.20 5 106 +mosn.io/pkg/binding/form_mapping.go:123.2,123.20 1 106 +mosn.io/pkg/binding/form_mapping.go:127.2,128.20 2 59 +mosn.io/pkg/binding/form_mapping.go:137.2,137.54 1 59 +mosn.io/pkg/binding/form_mapping.go:120.20,122.3 1 82 +mosn.io/pkg/binding/form_mapping.go:123.20,125.3 1 47 +mosn.io/pkg/binding/form_mapping.go:128.20,131.45 2 10 +mosn.io/pkg/binding/form_mapping.go:131.45,134.4 2 10 +mosn.io/pkg/binding/form_mapping.go:140.150,142.33 2 46 +mosn.io/pkg/binding/form_mapping.go:146.2,146.22 1 45 +mosn.io/pkg/binding/form_mapping.go:142.33,144.3 1 1 +mosn.io/pkg/binding/form_mapping.go:147.21,148.10 1 4 +mosn.io/pkg/binding/form_mapping.go:151.3,151.42 1 4 +mosn.io/pkg/binding/form_mapping.go:152.21,153.10 1 5 +mosn.io/pkg/binding/form_mapping.go:156.3,156.29 1 5 +mosn.io/pkg/binding/form_mapping.go:159.3,159.42 1 2 +mosn.io/pkg/binding/form_mapping.go:160.10,162.10 2 36 +mosn.io/pkg/binding/form_mapping.go:166.3,166.18 1 36 +mosn.io/pkg/binding/form_mapping.go:169.3,169.52 1 36 +mosn.io/pkg/binding/form_mapping.go:148.10,150.4 1 2 +mosn.io/pkg/binding/form_mapping.go:153.10,155.4 1 2 +mosn.io/pkg/binding/form_mapping.go:156.29,158.4 1 3 +mosn.io/pkg/binding/form_mapping.go:162.10,164.4 1 1 +mosn.io/pkg/binding/form_mapping.go:166.18,168.4 1 34 +mosn.io/pkg/binding/form_mapping.go:173.90,174.22 1 44 +mosn.io/pkg/binding/form_mapping.go:218.2,218.12 1 1 +mosn.io/pkg/binding/form_mapping.go:175.19,176.36 1 16 +mosn.io/pkg/binding/form_mapping.go:177.20,178.36 1 1 +mosn.io/pkg/binding/form_mapping.go:179.21,180.37 1 1 +mosn.io/pkg/binding/form_mapping.go:181.21,182.37 1 1 +mosn.io/pkg/binding/form_mapping.go:183.21,184.35 1 3 +mosn.io/pkg/binding/form_mapping.go:188.3,188.37 1 1 +mosn.io/pkg/binding/form_mapping.go:189.20,190.37 1 2 +mosn.io/pkg/binding/form_mapping.go:191.21,192.37 1 1 +mosn.io/pkg/binding/form_mapping.go:193.22,194.38 1 1 +mosn.io/pkg/binding/form_mapping.go:195.22,196.38 1 1 +mosn.io/pkg/binding/form_mapping.go:197.22,198.38 1 1 +mosn.io/pkg/binding/form_mapping.go:199.20,200.34 1 2 +mosn.io/pkg/binding/form_mapping.go:201.23,202.39 1 2 +mosn.io/pkg/binding/form_mapping.go:203.23,204.39 1 1 +mosn.io/pkg/binding/form_mapping.go:205.22,206.23 1 1 +mosn.io/pkg/binding/form_mapping.go:207.22,208.35 1 8 +mosn.io/pkg/binding/form_mapping.go:212.3,212.80 1 1 +mosn.io/pkg/binding/form_mapping.go:213.19,214.80 1 1 +mosn.io/pkg/binding/form_mapping.go:215.10,216.24 1 1 +mosn.io/pkg/binding/form_mapping.go:185.22,186.45 1 2 +mosn.io/pkg/binding/form_mapping.go:209.18,210.42 1 7 +mosn.io/pkg/binding/form_mapping.go:221.70,222.15 1 20 +mosn.io/pkg/binding/form_mapping.go:225.2,226.16 2 20 +mosn.io/pkg/binding/form_mapping.go:229.2,229.12 1 20 +mosn.io/pkg/binding/form_mapping.go:222.15,224.3 1 1 +mosn.io/pkg/binding/form_mapping.go:226.16,228.3 1 19 +mosn.io/pkg/binding/form_mapping.go:232.71,233.15 1 6 +mosn.io/pkg/binding/form_mapping.go:236.2,237.16 2 6 +mosn.io/pkg/binding/form_mapping.go:240.2,240.12 1 6 +mosn.io/pkg/binding/form_mapping.go:233.15,235.3 1 1 +mosn.io/pkg/binding/form_mapping.go:237.16,239.3 1 6 +mosn.io/pkg/binding/form_mapping.go:243.58,244.15 1 2 +mosn.io/pkg/binding/form_mapping.go:247.2,248.16 2 2 +mosn.io/pkg/binding/form_mapping.go:251.2,251.12 1 2 +mosn.io/pkg/binding/form_mapping.go:244.15,246.3 1 1 +mosn.io/pkg/binding/form_mapping.go:248.16,250.3 1 2 +mosn.io/pkg/binding/form_mapping.go:254.72,255.15 1 3 +mosn.io/pkg/binding/form_mapping.go:258.2,259.16 2 3 +mosn.io/pkg/binding/form_mapping.go:262.2,262.12 1 3 +mosn.io/pkg/binding/form_mapping.go:255.15,257.3 1 1 +mosn.io/pkg/binding/form_mapping.go:259.16,261.3 1 3 +mosn.io/pkg/binding/form_mapping.go:265.91,267.22 2 7 +mosn.io/pkg/binding/form_mapping.go:271.2,271.47 1 7 +mosn.io/pkg/binding/form_mapping.go:289.2,289.15 1 7 +mosn.io/pkg/binding/form_mapping.go:294.2,295.75 2 6 +mosn.io/pkg/binding/form_mapping.go:299.2,299.66 1 6 +mosn.io/pkg/binding/form_mapping.go:307.2,308.16 2 5 +mosn.io/pkg/binding/form_mapping.go:312.2,313.12 2 4 +mosn.io/pkg/binding/form_mapping.go:267.22,269.3 1 4 +mosn.io/pkg/binding/form_mapping.go:272.26,274.17 2 0 +mosn.io/pkg/binding/form_mapping.go:278.3,279.23 2 0 +mosn.io/pkg/binding/form_mapping.go:283.3,285.13 3 0 +mosn.io/pkg/binding/form_mapping.go:274.17,276.4 1 0 +mosn.io/pkg/binding/form_mapping.go:279.23,281.4 1 0 +mosn.io/pkg/binding/form_mapping.go:289.15,292.3 2 1 +mosn.io/pkg/binding/form_mapping.go:295.75,297.3 1 1 +mosn.io/pkg/binding/form_mapping.go:299.66,301.17 2 2 +mosn.io/pkg/binding/form_mapping.go:304.3,304.10 1 1 +mosn.io/pkg/binding/form_mapping.go:301.17,303.4 1 1 +mosn.io/pkg/binding/form_mapping.go:308.16,310.3 1 1 +mosn.io/pkg/binding/form_mapping.go:316.84,317.25 1 6 +mosn.io/pkg/binding/form_mapping.go:323.2,323.12 1 5 +mosn.io/pkg/binding/form_mapping.go:317.25,319.17 2 8 +mosn.io/pkg/binding/form_mapping.go:319.17,321.4 1 1 +mosn.io/pkg/binding/form_mapping.go:326.84,329.16 3 4 +mosn.io/pkg/binding/form_mapping.go:332.2,333.12 2 3 +mosn.io/pkg/binding/form_mapping.go:329.16,331.3 1 1 +mosn.io/pkg/binding/form_mapping.go:336.88,338.16 2 2 +mosn.io/pkg/binding/form_mapping.go:341.2,342.12 2 1 +mosn.io/pkg/binding/form_mapping.go:338.16,340.3 1 1 +mosn.io/pkg/binding/form_mapping.go:345.55,347.13 2 126 +mosn.io/pkg/binding/form_mapping.go:350.2,350.38 1 20 +mosn.io/pkg/binding/form_mapping.go:347.13,349.3 1 106 +mosn.io/pkg/binding/json.go:29.34,31.2 1 0 +mosn.io/pkg/binding/json.go:33.67,34.35 1 0 +mosn.io/pkg/binding/json.go:37.2,37.34 1 0 +mosn.io/pkg/binding/json.go:34.35,36.3 1 0 +mosn.io/pkg/binding/json.go:40.65,42.2 1 1 +mosn.io/pkg/binding/json.go:44.53,46.28 2 1 +mosn.io/pkg/binding/json.go:49.2,49.40 1 1 +mosn.io/pkg/binding/json.go:52.2,52.44 1 1 +mosn.io/pkg/binding/json.go:55.2,55.22 1 1 +mosn.io/pkg/binding/json.go:46.28,48.3 1 0 +mosn.io/pkg/binding/json.go:49.40,51.3 1 0 +mosn.io/pkg/binding/json.go:52.44,54.3 1 0 +mosn.io/pkg/binding/msgpack.go:19.37,21.2 1 0 +mosn.io/pkg/binding/msgpack.go:23.70,25.2 1 0 +mosn.io/pkg/binding/msgpack.go:27.68,29.2 1 1 +mosn.io/pkg/binding/msgpack.go:31.56,33.62 2 1 +mosn.io/pkg/binding/msgpack.go:36.2,36.22 1 1 +mosn.io/pkg/binding/msgpack.go:33.62,35.3 1 0 +mosn.io/pkg/binding/query.go:11.35,13.2 1 0 +mosn.io/pkg/binding/query.go:15.68,17.45 2 0 +mosn.io/pkg/binding/query.go:20.2,20.22 1 0 +mosn.io/pkg/binding/query.go:17.45,19.3 1 0 +mosn.io/pkg/binding/xml.go:16.33,18.2 1 0 +mosn.io/pkg/binding/xml.go:20.66,22.2 1 0 +mosn.io/pkg/binding/xml.go:24.64,26.2 1 1 +mosn.io/pkg/binding/xml.go:27.52,29.44 2 1 +mosn.io/pkg/binding/xml.go:32.2,32.22 1 1 +mosn.io/pkg/binding/xml.go:29.44,31.3 1 0 +mode: atomic +mosn.io/pkg/binding/internal/bytesconv/bytesconv.go:9.41,14.2 4 100 +mosn.io/pkg/binding/internal/bytesconv/bytesconv.go:17.37,19.2 1 100 +mode: atomic diff --git a/log/logger.go b/log/logger.go index c004df8..ef7f15e 100644 --- a/log/logger.go +++ b/log/logger.go @@ -18,12 +18,16 @@ package log import ( + "compress/gzip" "errors" "fmt" "io" + "io/ioutil" "os" "path/filepath" "runtime/debug" + "sort" + "strconv" "strings" "sync" "time" @@ -205,6 +209,7 @@ func (l *Logger) start() error { l.create = time.Now() } l.writer = file + l.mill() l.once.Do(l.startRotate) // start rotate, only once } } @@ -247,6 +252,7 @@ func (l *Logger) handler() { } } case buf := <-l.writeBufferChan: + l.Write(buf.Bytes()) PutLogBuffer(buf) } @@ -471,3 +477,199 @@ func parseSyslogAddress(location string) *syslogAddress { return nil } + +const ( + compressSuffix = ".gz" +) + +// millRunOnce performs compression and removal of stale log files. +// Log files are compressed if enabled via configuration and old log +// files are removed, keeping at most l.MaxBackups files, as long as +// none of them are older than MaxAge. +func (l *Logger) millRunOnce() error { + files, err := l.oldLogFiles() + if err != nil { + return err + } + + compress, remove := l.screeningCompressFile(files) + + for _, f := range remove { + _ = os.Remove(filepath.Join(l.dir(), f.FileName)) + } + var wg sync.WaitGroup + for _, f := range compress { + var fnCompress, fileName string + fileName = f.FileName + wg.Add(1) + fnCompress, err = l.findCompressFile(fileName) + if err != nil { + return err + } + go func(fnCompress, fileName string, wg *sync.WaitGroup) { + wg.Done() + err = l.compressLogFile(fileName, fnCompress) + if err != nil { + fmt.Fprintf(os.Stderr, "logger %s compressLogFile , error: %v", l.output, err) + } + }(fnCompress, fileName, &wg) + } + wg.Wait() + return err +} + +func (l *Logger) screeningCompressFile(files []LoggerInfo) (compress, remove []LoggerInfo) { + resFiles, removeByMaxAge := l.screeningCompressFileByMaxAge(files) + resFiles, remove = l.screeningCompressFileByMaxBackups(resFiles, removeByMaxAge) + + if l.roller.Compress { + for i := range resFiles { + if !strings.HasSuffix(resFiles[i].FileName, compressSuffix) { + compress = append(compress, resFiles[i]) + } + } + } + return +} + +func (l *Logger) screeningCompressFileByMaxAge(files []LoggerInfo) (resFiles, remove []LoggerInfo) { + if l.roller.MaxAge > 0 { + diff := time.Duration(int64(maxRotateHour*time.Hour) * int64(l.roller.MaxAge)) + cutoff := time.Now().Add(-1 * diff) + + for i := range files { + if files[i].CreateTime.Before(cutoff) { + remove = append(remove, files[i]) + } else { + resFiles = append(resFiles, files[i]) + } + } + } else { + resFiles = files + } + return +} + +func (l *Logger) screeningCompressFileByMaxBackups(files, remove []LoggerInfo) (resFiles, resRemove []LoggerInfo) { + if l.roller.MaxBackups > 0 && l.roller.MaxBackups < len(files) { + preserved := make(map[string]bool) + + for i := range files { + // Only count the uncompressed log file or the + // compressed log file, not both. + fn := files[i].FileName + + preserved[strings.TrimSuffix(fn, compressSuffix)] = true + + if len(preserved) > l.roller.MaxBackups { + remove = append(remove, files[i]) + } else { + resFiles = append(resFiles, files[i]) + } + } + } else { + resFiles = files + } + resRemove = remove + return +} + +func (l *Logger) findCompressFile(fileName string) (string, error) { + num := 1 + statName := fileName + + for i := 0; i < l.roller.MaxBackups; i++ { + if _, err := os.Stat(l.dir() + statName + compressSuffix); os.IsNotExist(err) { + return statName + compressSuffix, nil + } + statName = fileName + "." + strconv.Itoa(num) + num++ + } + return fileName, errors.New("findCompressFile failed") +} + +func (l *Logger) mill() { + if l.roller.MaxBackups != 0 || l.roller.MaxAge != 0 || l.roller.Compress { + _ = l.millRunOnce() + } +} + +// oldLogFiles returns the list of backup log files stored in the same +// directory as the current log file, sorted by ModTime +func (l *Logger) oldLogFiles() ([]LoggerInfo, error) { + files, err := ioutil.ReadDir(l.dir()) + if err != nil { + return nil, err + } + logFiles := []LoggerInfo{} + + for _, f := range files { + if f.IsDir() { + continue + } + if !strings.HasPrefix(f.Name(), filepath.Base(l.output)+".") { + continue + } + logFiles = append(logFiles, LoggerInfo{*l.roller, f.Name(), f.ModTime()}) + } + sort.Sort(byFormatTime(logFiles)) + + return logFiles, nil +} + +// dir returns the directory for the current filename. +func (l *Logger) dir() string { + return filepath.Dir(l.output) +} + +// compressLogFile compresses the given log file, removing the +// uncompressed log file if successful. +func (l *Logger) compressLogFile(srcFile, dstFile string) error { + f, err := os.Open(filepath.Join(l.dir(), filepath.Clean(srcFile))) + if err != nil { + return err + } + + defer func() { + _ = f.Close() + }() + + gzf, err := os.OpenFile(filepath.Join(l.dir(), filepath.Clean(dstFile)), os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644) + if err != nil { + return err + } + + defer func() { + _ = gzf.Close() + if err != nil { + _ = os.Remove(filepath.Join(l.dir(), filepath.Clean(dstFile))) + } + }() + + gz := gzip.NewWriter(gzf) + + if _, err = io.Copy(gz, f); err != nil { + return err + } + + if err = gz.Close(); err != nil { + return err + } + + return os.Remove(filepath.Join(l.dir(), filepath.Clean(srcFile))) +} + +// byFormatTime sorts by newest time formatted in the name. +type byFormatTime []LoggerInfo + +func (b byFormatTime) Less(i, j int) bool { + return b[i].CreateTime.After(b[j].CreateTime) +} + +func (b byFormatTime) Swap(i, j int) { + b[i], b[j] = b[j], b[i] +} + +func (b byFormatTime) Len() int { + return len(b) +} diff --git a/log/logger_test.go b/log/logger_test.go index c15cc1a..932db34 100644 --- a/log/logger_test.go +++ b/log/logger_test.go @@ -400,3 +400,59 @@ WAIT: t.Logf("received %d reopens", reopens) close(l.stopRotate) } + +func TestLogRollerTimeAndCompress(t *testing.T) { + logName := "/tmp/mosn_bench/defaultCompress.log" + rollerName := logName + "." + time.Now().Format("2006-01-02_15") + os.Remove(logName) + os.Remove(rollerName) + // replace rotate interval for test + doRotate = testRotateByKeep + defer func() { + doRotate = testRotateByKeep + }() + logger, err := GetOrCreateLogger(logName, &Roller{MaxTime: 2, Handler: rollerHandler, Compress: true}) + if err != nil { + t.Fatal(err) + } + // 1111 will be rotated to rollerName + logger.Print(newLogBufferString("1111111"), false) + time.Sleep(2 * time.Second) + // 2222 will be writed in logName + logger.Print(newLogBufferString("2222222"), false) + time.Sleep(1 * time.Second) + logger.Close() // stop the rotate + + if !exists(rollerName + compressSuffix) { + t.Fatalf("compress is failed") + } +} + +func testRotateByKeep(l *Logger, interval time.Duration) { + doRotateFunc(l, 1*time.Second) +} +func TestLogRollerTimeAndKeep(t *testing.T) { + logName := "/tmp/mosn_bench/defaultKeep.log" + rollerName := logName + "." + time.Now().Format("2006-01-02_15") + os.Remove(logName) + os.Remove(rollerName) + // replace rotate interval for test + doRotate = testRotateByKeep + defer func() { + doRotate = testRotateByKeep + }() + logger, err := GetOrCreateLogger(logName, &Roller{MaxTime: 2, Handler: rollerHandler, MaxBackups: 1}) + if err != nil { + t.Fatal(err) + } + logger.Print(newLogBufferString("1111111"), false) + time.Sleep(2 * time.Second) + logger.Print(newLogBufferString("2222222"), false) + time.Sleep(2 * time.Second) + logger.Print(newLogBufferString("3333333"), false) + time.Sleep(1 * time.Second) + logger.Close() // stop the rotate + if exists(rollerName) { + t.Fatalf(" %s is exists", rollerName) + } +} diff --git a/profile.out b/profile.out new file mode 100644 index 0000000..79b28a0 --- /dev/null +++ b/profile.out @@ -0,0 +1 @@ +mode: atomic