lumberjack.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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. "fmt"
  25. "io"
  26. "io/ioutil"
  27. "os"
  28. "path/filepath"
  29. "sort"
  30. "strings"
  31. "sync"
  32. "time"
  33. )
  34. const (
  35. backupTimeFormat = "2006-01-02T15-04-05.000"
  36. defaultMaxSize = 100
  37. )
  38. // ensure we always implement io.WriteCloser
  39. var _ io.WriteCloser = (*Logger)(nil)
  40. // Logger is an io.WriteCloser that writes to the specified filename.
  41. //
  42. // Logger opens or creates the logfile on first Write. If the file exists and
  43. // is less than MaxSize megabytes, lumberjack will open and append to that file.
  44. // If the file exists and its size is >= MaxSize megabytes, the file is renamed
  45. // by putting the current time in a timestamp in the name immediately before the
  46. // file's extension (or the end of the filename if there's no extension). A new
  47. // log file is then created using original filename.
  48. //
  49. // Whenever a write would cause the current log file exceed MaxSize megabytes,
  50. // the current file is closed, renamed, and a new log file created with the
  51. // original name. Thus, the filename you give Logger is always the "current" log
  52. // file.
  53. //
  54. // Cleaning Up Old Log Files
  55. //
  56. // Whenever a new logfile gets created, old log files may be deleted. The most
  57. // recent files according to the encoded timestamp will be retained, up to a
  58. // number equal to MaxBackups (or all of them if MaxBackups is 0). Any files
  59. // with an encoded timestamp older than MaxAge days are deleted, regardless of
  60. // MaxBackups. Note that the time encoded in the timestamp is the rotation
  61. // time, which may differ from the last time that file was written to.
  62. //
  63. // If MaxBackups and MaxAge are both 0, no old log files will be deleted.
  64. type Logger struct {
  65. // Filename is the file to write logs to. Backup log files will be retained
  66. // in the same directory. It uses <processname>-lumberjack.log in
  67. // os.TempDir() if empty.
  68. Filename string `json:"filename" yaml:"filename"`
  69. // MaxSize is the maximum size in megabytes of the log file before it gets
  70. // rotated. It defaults to 100 megabytes.
  71. MaxSize int `json:"maxsize" yaml:"maxsize"`
  72. // MaxAge is the maximum number of days to retain old log files based on the
  73. // timestamp encoded in their filename. Note that a day is defined as 24
  74. // hours and may not exactly correspond to calendar days due to daylight
  75. // savings, leap seconds, etc. The default is not to remove old log files
  76. // based on age.
  77. MaxAge int `json:"maxage" yaml:"maxage"`
  78. // MaxBackups is the maximum number of old log files to retain. The default
  79. // is to retain all old log files (though MaxAge may still cause them to get
  80. // deleted.)
  81. MaxBackups int `json:"maxbackups" yaml:"maxbackups"`
  82. // LocalTime determines if the time used for formatting the timestamps in
  83. // backup files is the computer's local time. The default is to use UTC
  84. // time.
  85. LocalTime bool `json:"localtime" yaml:"localtime"`
  86. size int64
  87. file *os.File
  88. mu sync.Mutex
  89. }
  90. var (
  91. // currentTime exists so it can be mocked out by tests.
  92. currentTime = time.Now
  93. // os_Stat exists so it can be mocked out by tests.
  94. os_Stat = os.Stat
  95. // megabyte is the conversion factor between MaxSize and bytes. It is a
  96. // variable so tests can mock it out and not need to write megabytes of data
  97. // to disk.
  98. megabyte = 1024 * 1024
  99. )
  100. // Write implements io.Writer. If a write would cause the log file to be larger
  101. // than MaxSize, the file is closed, renamed to include a timestamp of the
  102. // current time, and a new log file is created using the original log file name.
  103. // If the length of the write is greater than MaxSize, an error is returned.
  104. func (l *Logger) Write(p []byte) (n int, err error) {
  105. l.mu.Lock()
  106. defer l.mu.Unlock()
  107. writeLen := int64(len(p))
  108. if writeLen > l.max() {
  109. return 0, fmt.Errorf(
  110. "write length %d exceeds maximum file size %d", writeLen, l.max(),
  111. )
  112. }
  113. if l.file == nil {
  114. if err = l.openExistingOrNew(len(p)); err != nil {
  115. return 0, err
  116. }
  117. }
  118. if l.size+writeLen > l.max() {
  119. if err := l.rotate(); err != nil {
  120. return 0, err
  121. }
  122. }
  123. n, err = l.file.Write(p)
  124. l.size += int64(n)
  125. return n, err
  126. }
  127. // Close implements io.Closer, and closes the current logfile.
  128. func (l *Logger) Close() error {
  129. l.mu.Lock()
  130. defer l.mu.Unlock()
  131. return l.close()
  132. }
  133. // close closes the file if it is open.
  134. func (l *Logger) close() error {
  135. if l.file == nil {
  136. return nil
  137. }
  138. err := l.file.Close()
  139. l.file = nil
  140. return err
  141. }
  142. // Rotate causes Logger to close the existing log file and immediately create a
  143. // new one. This is a helper function for applications that want to initiate
  144. // rotations outside of the normal rotation rules, such as in response to
  145. // SIGHUP. After rotating, this initiates a cleanup of old log files according
  146. // to the normal rules.
  147. func (l *Logger) Rotate() error {
  148. l.mu.Lock()
  149. defer l.mu.Unlock()
  150. return l.rotate()
  151. }
  152. // rotate closes the current file, moves it aside with a timestamp in the name,
  153. // (if it exists), opens a new file with the original filename, and then runs
  154. // cleanup.
  155. func (l *Logger) rotate() error {
  156. if err := l.close(); err != nil {
  157. return err
  158. }
  159. if err := l.openNew(); err != nil {
  160. return err
  161. }
  162. return l.cleanup()
  163. }
  164. // openNew opens a new log file for writing, moving any old log file out of the
  165. // way. This methods assumes the file has already been closed.
  166. func (l *Logger) openNew() error {
  167. err := os.MkdirAll(l.dir(), 0744)
  168. if err != nil {
  169. return fmt.Errorf("can't make directories for new logfile: %s", err)
  170. }
  171. name := l.filename()
  172. mode := os.FileMode(0644)
  173. info, err := os_Stat(name)
  174. if err == nil {
  175. // Copy the mode off the old logfile.
  176. mode = info.Mode()
  177. // move the existing file
  178. newname := backupName(name, l.LocalTime)
  179. if err := os.Rename(name, newname); err != nil {
  180. return fmt.Errorf("can't rename log file: %s", err)
  181. }
  182. // this is a no-op anywhere but linux
  183. if err := chown(name, info); err != nil {
  184. return err
  185. }
  186. }
  187. // we use truncate here because this should only get called when we've moved
  188. // the file ourselves. if someone else creates the file in the meantime,
  189. // just wipe out the contents.
  190. f, err := os.OpenFile(name, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, mode)
  191. if err != nil {
  192. return fmt.Errorf("can't open new logfile: %s", err)
  193. }
  194. l.file = f
  195. l.size = 0
  196. return nil
  197. }
  198. // backupName creates a new filename from the given name, inserting a timestamp
  199. // between the filename and the extension, using the local time if requested
  200. // (otherwise UTC).
  201. func backupName(name string, local bool) string {
  202. dir := filepath.Dir(name)
  203. filename := filepath.Base(name)
  204. ext := filepath.Ext(filename)
  205. prefix := filename[:len(filename)-len(ext)]
  206. t := currentTime()
  207. if !local {
  208. t = t.UTC()
  209. }
  210. timestamp := t.Format(backupTimeFormat)
  211. return filepath.Join(dir, fmt.Sprintf("%s-%s%s", prefix, timestamp, ext))
  212. }
  213. // openExistingOrNew opens the logfile if it exists and if the current write
  214. // would not put it over MaxSize. If there is no such file or the write would
  215. // put it over the MaxSize, a new file is created.
  216. func (l *Logger) openExistingOrNew(writeLen int) error {
  217. filename := l.filename()
  218. info, err := os_Stat(filename)
  219. if os.IsNotExist(err) {
  220. return l.openNew()
  221. }
  222. if err != nil {
  223. return fmt.Errorf("error getting log file info: %s", err)
  224. }
  225. if info.Size()+int64(writeLen) >= l.max() {
  226. return l.rotate()
  227. }
  228. file, err := os.OpenFile(filename, os.O_APPEND|os.O_WRONLY, 0644)
  229. if err != nil {
  230. // if we fail to open the old log file for some reason, just ignore
  231. // it and open a new log file.
  232. return l.openNew()
  233. }
  234. l.file = file
  235. l.size = info.Size()
  236. return nil
  237. }
  238. // genFilename generates the name of the logfile from the current time.
  239. func (l *Logger) filename() string {
  240. if l.Filename != "" {
  241. return l.Filename
  242. }
  243. name := filepath.Base(os.Args[0]) + "-lumberjack.log"
  244. return filepath.Join(os.TempDir(), name)
  245. }
  246. // cleanup deletes old log files, keeping at most l.MaxBackups files, as long as
  247. // none of them are older than MaxAge.
  248. func (l *Logger) cleanup() error {
  249. if l.MaxBackups == 0 && l.MaxAge == 0 {
  250. return nil
  251. }
  252. files, err := l.oldLogFiles()
  253. if err != nil {
  254. return err
  255. }
  256. var deletes []logInfo
  257. if l.MaxBackups > 0 && l.MaxBackups < len(files) {
  258. deletes = files[l.MaxBackups:]
  259. files = files[:l.MaxBackups]
  260. }
  261. if l.MaxAge > 0 {
  262. diff := time.Duration(int64(24*time.Hour) * int64(l.MaxAge))
  263. cutoff := currentTime().Add(-1 * diff)
  264. for _, f := range files {
  265. if f.timestamp.Before(cutoff) {
  266. deletes = append(deletes, f)
  267. }
  268. }
  269. }
  270. if len(deletes) == 0 {
  271. return nil
  272. }
  273. go deleteAll(l.dir(), deletes)
  274. return nil
  275. }
  276. func deleteAll(dir string, files []logInfo) {
  277. // remove files on a separate goroutine
  278. for _, f := range files {
  279. // what am I going to do, log this?
  280. _ = os.Remove(filepath.Join(dir, f.Name()))
  281. }
  282. }
  283. // oldLogFiles returns the list of backup log files stored in the same
  284. // directory as the current log file, sorted by ModTime
  285. func (l *Logger) oldLogFiles() ([]logInfo, error) {
  286. files, err := ioutil.ReadDir(l.dir())
  287. if err != nil {
  288. return nil, fmt.Errorf("can't read log file directory: %s", err)
  289. }
  290. logFiles := []logInfo{}
  291. prefix, ext := l.prefixAndExt()
  292. for _, f := range files {
  293. if f.IsDir() {
  294. continue
  295. }
  296. name := l.timeFromName(f.Name(), prefix, ext)
  297. if name == "" {
  298. continue
  299. }
  300. t, err := time.Parse(backupTimeFormat, name)
  301. if err == nil {
  302. logFiles = append(logFiles, logInfo{t, f})
  303. }
  304. // error parsing means that the suffix at the end was not generated
  305. // by lumberjack, and therefore it's not a backup file.
  306. }
  307. sort.Sort(byFormatTime(logFiles))
  308. return logFiles, nil
  309. }
  310. // timeFromName extracts the formatted time from the filename by stripping off
  311. // the filename's prefix and extension. This prevents someone's filename from
  312. // confusing time.parse.
  313. func (l *Logger) timeFromName(filename, prefix, ext string) string {
  314. if !strings.HasPrefix(filename, prefix) {
  315. return ""
  316. }
  317. filename = filename[len(prefix):]
  318. if !strings.HasSuffix(filename, ext) {
  319. return ""
  320. }
  321. filename = filename[:len(filename)-len(ext)]
  322. return filename
  323. }
  324. // max returns the maximum size in bytes of log files before rolling.
  325. func (l *Logger) max() int64 {
  326. if l.MaxSize == 0 {
  327. return int64(defaultMaxSize * megabyte)
  328. }
  329. return int64(l.MaxSize) * int64(megabyte)
  330. }
  331. // dir returns the directory for the current filename.
  332. func (l *Logger) dir() string {
  333. return filepath.Dir(l.filename())
  334. }
  335. // prefixAndExt returns the filename part and extension part from the Logger's
  336. // filename.
  337. func (l *Logger) prefixAndExt() (prefix, ext string) {
  338. filename := filepath.Base(l.filename())
  339. ext = filepath.Ext(filename)
  340. prefix = filename[:len(filename)-len(ext)] + "-"
  341. return prefix, ext
  342. }
  343. // logInfo is a convenience struct to return the filename and its embedded
  344. // timestamp.
  345. type logInfo struct {
  346. timestamp time.Time
  347. os.FileInfo
  348. }
  349. // byFormatTime sorts by newest time formatted in the name.
  350. type byFormatTime []logInfo
  351. func (b byFormatTime) Less(i, j int) bool {
  352. return b[i].timestamp.After(b[j].timestamp)
  353. }
  354. func (b byFormatTime) Swap(i, j int) {
  355. b[i], b[j] = b[j], b[i]
  356. }
  357. func (b byFormatTime) Len() int {
  358. return len(b)
  359. }