Rotate closes the current log file, rotates the files and creates an empty log file.
(maxLogFiles int)
| 78 | |
| 79 | // Rotate closes the current log file, rotates the files and creates an empty log file. |
| 80 | func (l *LogFile) Rotate(maxLogFiles int) error { |
| 81 | if err := l.File.Close(); err != nil { |
| 82 | return err |
| 83 | } |
| 84 | for i := maxLogFiles - 1; i >= 0; i-- { |
| 85 | newerFile := fmt.Sprintf("%s.%d", l.Path, i-1) |
| 86 | // special case: if index is 0 we omit the suffix i.e. we expect |
| 87 | // foo foo.1 foo.2 up to foo.<maxLogFiles-1> |
| 88 | if i == 0 { |
| 89 | newerFile = l.Path |
| 90 | } |
| 91 | olderFile := fmt.Sprintf("%s.%d", l.Path, i) |
| 92 | // overwrite the olderFile with the newerFile |
| 93 | err := os.Rename(newerFile, olderFile) |
| 94 | if os.IsNotExist(err) { |
| 95 | // the newerFile does not exist |
| 96 | continue |
| 97 | } |
| 98 | if err != nil { |
| 99 | return err |
| 100 | } |
| 101 | } |
| 102 | f, err := os.Create(l.Path) |
| 103 | if err != nil { |
| 104 | return err |
| 105 | } |
| 106 | l.File = f |
| 107 | l.BytesWritten = 0 |
| 108 | return nil |
| 109 | } |
| 110 | |
| 111 | func main() { |
| 112 | socketPath := flag.String("socket", "/var/run/memlogdq.sock", "memlogd log query socket") |