logger.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // This file is subject to a 1-clause BSD license.
  2. // Its contents can be found in the enclosed LICENSE file.
  3. // Package logger defines facilities to write bot data to log files,
  4. // along with code which cycles log cycles and purges log files
  5. // when needed.
  6. package logger
  7. import (
  8. "fmt"
  9. "log"
  10. "os"
  11. "path/filepath"
  12. "sync"
  13. "time"
  14. )
  15. var (
  16. // Format defines the date layout for log file names.
  17. Format = "20060102"
  18. // PurgeTimeout defines the timeout after which the bot should
  19. // check for stale log files.
  20. PurgeTimeout = time.Hour * 24
  21. // RefreshTimeout determines how often we should check if a new
  22. // log file should be opened.
  23. RefreshTimeout = time.Minute
  24. // Expiration defines how old a log file should be, before it
  25. // is considered stale.
  26. Expiration = time.Hour * 24 * 7 * 2
  27. )
  28. // These defines some internal state.
  29. var (
  30. logFile *os.File
  31. startOnce sync.Once
  32. stopOnce sync.Once
  33. logPollQuit = make(chan struct{})
  34. )
  35. // Init initializes a new log file, if necessary. It then launches a
  36. // background service which periodically checks if a new log file should
  37. // be created. This happens according to a predefined timeout. Additionally,
  38. // it will periodically purge stale log files from disk.
  39. func Init(dir string) {
  40. startOnce.Do(func() {
  41. err := openLog(dir)
  42. if err != nil {
  43. log.Println("[app] Init log:", err)
  44. return
  45. }
  46. go poll(dir)
  47. })
  48. }
  49. // Shutdown shuts down the background log operations.
  50. func Shutdown() {
  51. stopOnce.Do(func() {
  52. close(logPollQuit)
  53. })
  54. }
  55. // poll periodically purges stale log files and ensures logs are cycled
  56. // after the appropriate timeout.
  57. func poll(dir string) {
  58. // Do an initial purge of stale logs. This ensures that we
  59. // do not accumulate stale files if the PurgeTimeout below
  60. // is never triggered. Which might happen if the program is
  61. // shut down before the timeout occurs.
  62. err := purgeLogs(dir)
  63. loopy:
  64. for err == nil {
  65. select {
  66. case <-logPollQuit:
  67. break loopy
  68. case <-time.After(RefreshTimeout):
  69. err = openLog(dir)
  70. case <-time.After(PurgeTimeout):
  71. err = purgeLogs(dir)
  72. }
  73. }
  74. if err != nil {
  75. log.Println("[app]", err)
  76. }
  77. // Clean up the existing log file.
  78. if logFile != nil {
  79. log.SetOutput(os.Stderr)
  80. logFile.Close()
  81. logFile = nil
  82. }
  83. }
  84. // openLog opens a new, or existing log file.
  85. func openLog(dir string) error {
  86. // Ensure the log file directory exists.
  87. err := os.Mkdir(dir, 0700)
  88. if err != nil && !os.IsExist(err) {
  89. return err
  90. }
  91. // Determine the name of the new log file.
  92. timeStamp := time.Now().Format(Format)
  93. file := fmt.Sprintf("%s.txt", timeStamp)
  94. file = filepath.Join(dir, file)
  95. // Exit if we're already using this file.
  96. if logFile != nil && logFile.Name() == file {
  97. if logFile.Name() == file {
  98. return nil
  99. }
  100. log.Println("[log] Opening new log file:", file)
  101. }
  102. // Create/open the new logfile.
  103. fd, err := os.OpenFile(file, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0600)
  104. if err != nil {
  105. return err
  106. }
  107. // Set the new log output.
  108. log.SetOutput(fd)
  109. // Close the old log file and assign the new one.
  110. if logFile != nil {
  111. logFile.Close()
  112. }
  113. logFile = fd
  114. // Set the log prefix to include our process id.
  115. // This makes analyzing log data a little easier.
  116. log.SetPrefix(fmt.Sprintf("[%d] ", os.Getpid()))
  117. return nil
  118. }
  119. // purgeLogs checks the given directory for files which are older than a
  120. // predefined number of days. If found, the log file in question is deleted.
  121. // This ensures we do not keep stale logs around unnecessarily.
  122. func purgeLogs(dir string) error {
  123. log.Println("[log] Purging stale log files...")
  124. fd, err := os.Open(dir)
  125. if err != nil {
  126. return err
  127. }
  128. files, err := fd.Readdir(-1)
  129. fd.Close()
  130. if err != nil {
  131. return err
  132. }
  133. for _, file := range files {
  134. if time.Since(file.ModTime()) < Expiration {
  135. continue
  136. }
  137. path := filepath.Join(dir, file.Name())
  138. err = os.Remove(path)
  139. if err != nil {
  140. return err
  141. }
  142. }
  143. return nil
  144. }