logger.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. // PurgeCheck defines the timeout after which the bot should
  19. // check for stale log files.
  20. PurgeCheck = 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. // Exit poll goroutine.
  53. close(logPollQuit)
  54. // Clean up the existing log file.
  55. if logFile != nil {
  56. log.SetOutput(os.Stderr)
  57. logFile.Close()
  58. logFile = nil
  59. }
  60. })
  61. }
  62. // poll periodically purges stale log files and ensures logs are cycled
  63. // after the appropriate timeout.
  64. func poll(dir string) {
  65. refresh := time.Tick(RefreshTimeout)
  66. purgeCheck := time.Tick(PurgeCheck)
  67. var err error
  68. for err == nil {
  69. select {
  70. case <-logPollQuit:
  71. return
  72. case <-refresh:
  73. err = openLog(dir)
  74. case <-purgeCheck:
  75. err = purgeLogs(dir)
  76. }
  77. }
  78. if err != nil {
  79. log.Println("[app]", err)
  80. }
  81. }
  82. // openLog opens a new, or existing log file.
  83. func openLog(dir string) error {
  84. // Ensure the log file directory exists.
  85. err := os.Mkdir(dir, 0700)
  86. if err != nil && !os.IsExist(err) {
  87. return err
  88. }
  89. // Determine the name of the new log file.
  90. timeStamp := time.Now().Format(Format)
  91. file := fmt.Sprintf("%s.txt", timeStamp)
  92. file = filepath.Join(dir, file)
  93. // Exit if we're already using this file.
  94. if logFile != nil && logFile.Name() == file {
  95. return nil
  96. }
  97. // Create/open the new logfile.
  98. fd, err := os.OpenFile(file, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0600)
  99. if err != nil {
  100. return err
  101. }
  102. // Set the new log output.
  103. log.SetOutput(fd)
  104. // Close the old log file and assign the new one.
  105. if logFile != nil {
  106. logFile.Close()
  107. }
  108. logFile = fd
  109. // Set the log prefix to include our process id.
  110. // This makes analyzing log data a little easier.
  111. log.SetPrefix(fmt.Sprintf("[%d] ", os.Getpid()))
  112. return nil
  113. }
  114. // purgeLogs checks the given directory for files which are older than a
  115. // predefined number of days. If found, the log file in question is deleted.
  116. // This ensures we do not keep stale logs around unnecessarily.
  117. func purgeLogs(dir string) error {
  118. log.Println("[log] Purging stale log files...")
  119. fd, err := os.Open(dir)
  120. if err != nil {
  121. return err
  122. }
  123. files, err := fd.Readdir(-1)
  124. fd.Close()
  125. if err != nil {
  126. return err
  127. }
  128. for _, file := range files {
  129. if time.Since(file.ModTime()) < Expiration {
  130. continue
  131. }
  132. path := filepath.Join(dir, file.Name())
  133. err = os.Remove(path)
  134. if err != nil {
  135. return err
  136. }
  137. }
  138. return nil
  139. }