lumberjack.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. // Package lumberjack provides a rolling logger.
  2. //
  3. // Note that this is v2.0 of lumberjack, and should be imported using gopkg.in
  4. // thusly:
  5. //
  6. // import "gopkg.in/natefinch/lumberjack.v2"
  7. //
  8. // The package name remains simply lumberjack, and the code resides at
  9. // https://github.com/natefinch/lumberjack under the v2.0 branch.
  10. //
  11. // Lumberjack is intended to be one part of a logging infrastructure.
  12. // It is not an all-in-one solution, but instead is a pluggable
  13. // component at the bottom of the logging stack that simply controls the files
  14. // to which logs are written.
  15. //
  16. // Lumberjack plays well with any logging package that can write to an
  17. // io.Writer, including the standard library's log package.
  18. //
  19. // Lumberjack assumes that only one process is writing to the output files.
  20. // Using the same lumberjack configuration from multiple processes on the same
  21. // machine will result in improper behavior.
  22. package lumberjack
  23. import (
  24. "compress/gzip"
  25. "errors"
  26. "fmt"
  27. "io"
  28. "io/ioutil"
  29. "os"
  30. "path/filepath"
  31. "sort"
  32. "strings"
  33. "sync"
  34. "time"
  35. )
  36. const (
  37. backupTimeFormat = "2006-01-02T15-04-05.000"
  38. compressSuffix = ".gz"
  39. defaultMaxSize = 100
  40. )
  41. // ensure we always implement io.WriteCloser
  42. var _ io.WriteCloser = (*Logger)(nil)
  43. // Logger is an io.WriteCloser that writes to the specified filename.
  44. //
  45. // Logger opens or creates the logfile on first Write. If the file exists and
  46. // is less than MaxSize megabytes, lumberjack will open and append to that file.
  47. // If the file exists and its size is >= MaxSize megabytes, the file is renamed
  48. // by putting the current time in a timestamp in the name immediately before the
  49. // file's extension (or the end of the filename if there's no extension). A new
  50. // log file is then created using original filename.
  51. //
  52. // Whenever a write would cause the current log file exceed MaxSize megabytes,
  53. // the current file is closed, renamed, and a new log file created with the
  54. // original name. Thus, the filename you give Logger is always the "current" log
  55. // file.
  56. //
  57. // Backups use the log file name given to Logger, in the form
  58. // `name-timestamp.ext` where name is the filename without the extension,
  59. // timestamp is the time at which the log was rotated formatted with the
  60. // time.Time format of `2006-01-02T15-04-05.000` and the extension is the
  61. // original extension. For example, if your Logger.Filename is
  62. // `/var/log/foo/server.log`, a backup created at 6:30pm on Nov 11 2016 would
  63. // use the filename `/var/log/foo/server-2016-11-04T18-30-00.000.log`
  64. //
  65. // # Cleaning Up Old Log Files
  66. //
  67. // Whenever a new logfile gets created, old log files may be deleted. The most
  68. // recent files according to the encoded timestamp will be retained, up to a
  69. // number equal to MaxBackups (or all of them if MaxBackups is 0). Any files
  70. // with an encoded timestamp older than MaxAge days are deleted, regardless of
  71. // MaxBackups. Note that the time encoded in the timestamp is the rotation
  72. // time, which may differ from the last time that file was written to.
  73. //
  74. // If MaxBackups and MaxAge are both 0, no old log files will be deleted.
  75. type Logger struct {
  76. // Filename is the file to write logs to. Backup log files will be retained
  77. // in the same directory. It uses <processname>-lumberjack.log in
  78. // os.TempDir() if empty.
  79. Filename string `json:"filename" yaml:"filename"`
  80. // MaxSize is the maximum size in megabytes of the log file before it gets
  81. // rotated. It defaults to 100 megabytes.
  82. MaxSize int `json:"maxsize" yaml:"maxsize"`
  83. // MaxAge is the maximum number of days to retain old log files based on the
  84. // timestamp encoded in their filename. Note that a day is defined as 24
  85. // hours and may not exactly correspond to calendar days due to daylight
  86. // savings, leap seconds, etc. The default is not to remove old log files
  87. // based on age.
  88. MaxAge int `json:"maxage" yaml:"maxage"`
  89. // MaxBackups is the maximum number of old log files to retain. The default
  90. // is to retain all old log files (though MaxAge may still cause them to get
  91. // deleted.)
  92. MaxBackups int `json:"maxbackups" yaml:"maxbackups"`
  93. // LocalTime determines if the time used for formatting the timestamps in
  94. // backup files is the computer's local time. The default is to use UTC
  95. // time.
  96. LocalTime bool `json:"localtime" yaml:"localtime"`
  97. // Compress determines if the rotated log files should be compressed
  98. // using gzip. The default is not to perform compression.
  99. Compress bool `json:"compress" yaml:"compress"`
  100. size int64
  101. file *os.File
  102. mu sync.Mutex
  103. millCh chan bool
  104. startMill sync.Once
  105. }
  106. var (
  107. // currentTime exists so it can be mocked out by tests.
  108. currentTime = time.Now
  109. // os_Stat exists so it can be mocked out by tests.
  110. osStat = os.Stat
  111. // megabyte is the conversion factor between MaxSize and bytes. It is a
  112. // variable so tests can mock it out and not need to write megabytes of data
  113. // to disk.
  114. megabyte = 1024 * 1024
  115. )
  116. // Write implements io.Writer. If a write would cause the log file to be larger
  117. // than MaxSize, the file is closed, renamed to include a timestamp of the
  118. // current time, and a new log file is created using the original log file name.
  119. // If the length of the write is greater than MaxSize, an error is returned.
  120. func (l *Logger) Write(p []byte) (n int, err error) {
  121. l.mu.Lock()
  122. defer l.mu.Unlock()
  123. writeLen := int64(len(p))
  124. if writeLen > l.max() {
  125. return 0, fmt.Errorf(
  126. "write length %d exceeds maximum file size %d", writeLen, l.max(),
  127. )
  128. }
  129. if l.file == nil {
  130. if err = l.openExistingOrNew(len(p)); err != nil {
  131. return 0, err
  132. }
  133. }
  134. if l.size+writeLen > l.max() {
  135. if err := l.rotate(); err != nil {
  136. return 0, err
  137. }
  138. }
  139. n, err = l.file.Write(p)
  140. l.size += int64(n)
  141. return n, err
  142. }
  143. // Close implements io.Closer, and closes the current logfile.
  144. func (l *Logger) Close() error {
  145. l.mu.Lock()
  146. defer l.mu.Unlock()
  147. return l.close()
  148. }
  149. // close closes the file if it is open.
  150. func (l *Logger) close() error {
  151. if l.file == nil {
  152. return nil
  153. }
  154. l.file.Sync() // flush changes to disk; ignore error (by niujiuru, 2025-10-21)
  155. time.Sleep(500 * time.Millisecond) // wait for all data to be written to disk (by niujiuru, 2025-10-21)
  156. err := l.file.Close()
  157. l.file = nil
  158. return err
  159. }
  160. // Rotate causes Logger to close the existing log file and immediately create a
  161. // new one. This is a helper function for applications that want to initiate
  162. // rotations outside of the normal rotation rules, such as in response to
  163. // SIGHUP. After rotating, this initiates compression and removal of old log
  164. // files according to the configuration.
  165. func (l *Logger) Rotate() error {
  166. l.mu.Lock()
  167. defer l.mu.Unlock()
  168. return l.rotate()
  169. }
  170. // rotate closes the current file, moves it aside with a timestamp in the name,
  171. // (if it exists), opens a new file with the original filename, and then runs
  172. // post-rotation processing and removal.
  173. func (l *Logger) rotate() error {
  174. if err := l.close(); err != nil {
  175. return err
  176. }
  177. if err := l.openNew(); err != nil {
  178. return err
  179. }
  180. l.mill()
  181. return nil
  182. }
  183. // openNew opens a new log file for writing, moving any old log file out of the
  184. // way. This methods assumes the file has already been closed.
  185. func (l *Logger) openNew() error {
  186. err := os.MkdirAll(l.dir(), 0755)
  187. if err != nil {
  188. return fmt.Errorf("can't make directories for new logfile: %s", err)
  189. }
  190. name := l.filename()
  191. mode := os.FileMode(0600)
  192. info, err := osStat(name)
  193. if err == nil {
  194. // Copy the mode off the old logfile.
  195. mode = info.Mode()
  196. // move the existing file
  197. newname := backupName(name, l.LocalTime)
  198. if err := os.Rename(name, newname); err != nil {
  199. return fmt.Errorf("can't rename log file: %s", err)
  200. }
  201. // this is a no-op anywhere but linux
  202. if err := chown(name, info); err != nil {
  203. return err
  204. }
  205. }
  206. // we use truncate here because this should only get called when we've moved
  207. // the file ourselves. if someone else creates the file in the meantime,
  208. // just wipe out the contents.
  209. f, err := os.OpenFile(name, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, mode)
  210. if err != nil {
  211. return fmt.Errorf("can't open new logfile: %s", err)
  212. }
  213. l.file = f
  214. l.size = 0
  215. return nil
  216. }
  217. // backupName creates a new filename from the given name, inserting a timestamp
  218. // between the filename and the extension, using the local time if requested
  219. // (otherwise UTC).
  220. func backupName(name string, local bool) string {
  221. dir := filepath.Dir(name)
  222. filename := filepath.Base(name)
  223. ext := filepath.Ext(filename)
  224. prefix := filename[:len(filename)-len(ext)]
  225. t := currentTime()
  226. if !local {
  227. t = t.UTC()
  228. }
  229. timestamp := t.Format(backupTimeFormat)
  230. return filepath.Join(dir, fmt.Sprintf("%s-%s%s", prefix, timestamp, ext))
  231. }
  232. // openExistingOrNew opens the logfile if it exists and if the current write
  233. // would not put it over MaxSize. If there is no such file or the write would
  234. // put it over the MaxSize, a new file is created.
  235. func (l *Logger) openExistingOrNew(writeLen int) error {
  236. l.mill()
  237. filename := l.filename()
  238. info, err := osStat(filename)
  239. if os.IsNotExist(err) {
  240. return l.openNew()
  241. }
  242. if err != nil {
  243. return fmt.Errorf("error getting log file info: %s", err)
  244. }
  245. if info.Size()+int64(writeLen) >= l.max() {
  246. return l.rotate()
  247. }
  248. file, err := os.OpenFile(filename, os.O_APPEND|os.O_WRONLY, 0644)
  249. if err != nil {
  250. // if we fail to open the old log file for some reason, just ignore
  251. // it and open a new log file.
  252. return l.openNew()
  253. }
  254. l.file = file
  255. l.size = info.Size()
  256. return nil
  257. }
  258. // filename generates the name of the logfile from the current time.
  259. func (l *Logger) filename() string {
  260. if l.Filename != "" {
  261. return l.Filename
  262. }
  263. name := filepath.Base(os.Args[0]) + "-lumberjack.log"
  264. return filepath.Join(os.TempDir(), name)
  265. }
  266. // millRunOnce performs compression and removal of stale log files.
  267. // Log files are compressed if enabled via configuration and old log
  268. // files are removed, keeping at most l.MaxBackups files, as long as
  269. // none of them are older than MaxAge.
  270. func (l *Logger) millRunOnce() error {
  271. if l.MaxBackups == 0 && l.MaxAge == 0 && !l.Compress {
  272. return nil
  273. }
  274. files, err := l.oldLogFiles()
  275. if err != nil {
  276. return err
  277. }
  278. var compress, remove []logInfo
  279. if l.MaxBackups > 0 && l.MaxBackups < len(files) {
  280. preserved := make(map[string]bool)
  281. var remaining []logInfo
  282. for _, f := range files {
  283. // Only count the uncompressed log file or the
  284. // compressed log file, not both.
  285. fn := f.Name()
  286. if strings.HasSuffix(fn, compressSuffix) {
  287. fn = fn[:len(fn)-len(compressSuffix)]
  288. }
  289. preserved[fn] = true
  290. if len(preserved) > l.MaxBackups {
  291. remove = append(remove, f)
  292. } else {
  293. remaining = append(remaining, f)
  294. }
  295. }
  296. files = remaining
  297. }
  298. if l.MaxAge > 0 {
  299. diff := time.Duration(int64(24*time.Hour) * int64(l.MaxAge))
  300. cutoff := currentTime().Add(-1 * diff)
  301. var remaining []logInfo
  302. for _, f := range files {
  303. if f.timestamp.Before(cutoff) {
  304. remove = append(remove, f)
  305. } else {
  306. remaining = append(remaining, f)
  307. }
  308. }
  309. files = remaining
  310. }
  311. if l.Compress {
  312. for _, f := range files {
  313. if !strings.HasSuffix(f.Name(), compressSuffix) {
  314. compress = append(compress, f)
  315. }
  316. }
  317. }
  318. for _, f := range remove {
  319. errRemove := os.Remove(filepath.Join(l.dir(), f.Name()))
  320. if err == nil && errRemove != nil {
  321. err = errRemove
  322. }
  323. }
  324. for _, f := range compress {
  325. fn := filepath.Join(l.dir(), f.Name())
  326. errCompress := compressLogFile(fn, fn+compressSuffix)
  327. if err == nil && errCompress != nil {
  328. err = errCompress
  329. }
  330. }
  331. return err
  332. }
  333. // millRun runs in a goroutine to manage post-rotation compression and removal
  334. // of old log files.
  335. func (l *Logger) millRun() {
  336. for range l.millCh {
  337. // what am I going to do, log this?
  338. _ = l.millRunOnce()
  339. }
  340. }
  341. // mill performs post-rotation compression and removal of stale log files,
  342. // starting the mill goroutine if necessary.
  343. func (l *Logger) mill() {
  344. l.startMill.Do(func() {
  345. l.millCh = make(chan bool, 1)
  346. go l.millRun()
  347. })
  348. select {
  349. case l.millCh <- true:
  350. default:
  351. }
  352. }
  353. // oldLogFiles returns the list of backup log files stored in the same
  354. // directory as the current log file, sorted by ModTime
  355. func (l *Logger) oldLogFiles() ([]logInfo, error) {
  356. files, err := ioutil.ReadDir(l.dir())
  357. if err != nil {
  358. return nil, fmt.Errorf("can't read log file directory: %s", err)
  359. }
  360. logFiles := []logInfo{}
  361. prefix, ext := l.prefixAndExt()
  362. for _, f := range files {
  363. if f.IsDir() {
  364. continue
  365. }
  366. if t, err := l.timeFromName(f.Name(), prefix, ext); err == nil {
  367. logFiles = append(logFiles, logInfo{t, f})
  368. continue
  369. }
  370. if t, err := l.timeFromName(f.Name(), prefix, ext+compressSuffix); err == nil {
  371. logFiles = append(logFiles, logInfo{t, f})
  372. continue
  373. }
  374. // error parsing means that the suffix at the end was not generated
  375. // by lumberjack, and therefore it's not a backup file.
  376. }
  377. sort.Sort(byFormatTime(logFiles))
  378. return logFiles, nil
  379. }
  380. // timeFromName extracts the formatted time from the filename by stripping off
  381. // the filename's prefix and extension. This prevents someone's filename from
  382. // confusing time.parse.
  383. func (l *Logger) timeFromName(filename, prefix, ext string) (time.Time, error) {
  384. if !strings.HasPrefix(filename, prefix) {
  385. return time.Time{}, errors.New("mismatched prefix")
  386. }
  387. if !strings.HasSuffix(filename, ext) {
  388. return time.Time{}, errors.New("mismatched extension")
  389. }
  390. ts := filename[len(prefix) : len(filename)-len(ext)]
  391. return time.Parse(backupTimeFormat, ts)
  392. }
  393. // max returns the maximum size in bytes of log files before rolling.
  394. func (l *Logger) max() int64 {
  395. if l.MaxSize == 0 {
  396. return int64(defaultMaxSize * megabyte)
  397. }
  398. return int64(l.MaxSize) * int64(megabyte)
  399. }
  400. // dir returns the directory for the current filename.
  401. func (l *Logger) dir() string {
  402. return filepath.Dir(l.filename())
  403. }
  404. // prefixAndExt returns the filename part and extension part from the Logger's
  405. // filename.
  406. func (l *Logger) prefixAndExt() (prefix, ext string) {
  407. filename := filepath.Base(l.filename())
  408. ext = filepath.Ext(filename)
  409. prefix = filename[:len(filename)-len(ext)] + "-"
  410. return prefix, ext
  411. }
  412. // compressLogFile compresses the given log file, removing the
  413. // uncompressed log file if successful.
  414. func compressLogFile(src, dst string) (err error) {
  415. f, err := os.Open(src)
  416. if err != nil {
  417. return fmt.Errorf("failed to open log file: %v", err)
  418. }
  419. defer f.Close()
  420. fi, err := osStat(src)
  421. if err != nil {
  422. return fmt.Errorf("failed to stat log file: %v", err)
  423. }
  424. if err := chown(dst, fi); err != nil {
  425. return fmt.Errorf("failed to chown compressed log file: %v", err)
  426. }
  427. // If this file already exists, we presume it was created by
  428. // a previous attempt to compress the log file.
  429. gzf, err := os.OpenFile(dst, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, fi.Mode())
  430. if err != nil {
  431. return fmt.Errorf("failed to open compressed log file: %v", err)
  432. }
  433. defer gzf.Close()
  434. gz := gzip.NewWriter(gzf)
  435. defer func() {
  436. if err != nil {
  437. os.Remove(dst)
  438. err = fmt.Errorf("failed to compress log file: %v", err)
  439. }
  440. }()
  441. if _, err := io.Copy(gz, f); err != nil {
  442. return err
  443. }
  444. if err := gz.Close(); err != nil {
  445. return err
  446. }
  447. if err := gzf.Close(); err != nil {
  448. return err
  449. }
  450. if err := f.Close(); err != nil {
  451. return err
  452. }
  453. if err := os.Remove(src); err != nil {
  454. return err
  455. }
  456. return nil
  457. }
  458. // logInfo is a convenience struct to return the filename and its embedded
  459. // timestamp.
  460. type logInfo struct {
  461. timestamp time.Time
  462. os.FileInfo
  463. }
  464. // byFormatTime sorts by newest time formatted in the name.
  465. type byFormatTime []logInfo
  466. func (b byFormatTime) Less(i, j int) bool {
  467. return b[i].timestamp.After(b[j].timestamp)
  468. }
  469. func (b byFormatTime) Swap(i, j int) {
  470. b[i], b[j] = b[j], b[i]
  471. }
  472. func (b byFormatTime) Len() int {
  473. return len(b)
  474. }