logger.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. // Package logger provides logging functionality for the 3x-ui panel with
  2. // dual-backend logging (console/syslog and file) and buffered log storage for web UI.
  3. package logger
  4. import (
  5. "fmt"
  6. "os"
  7. "path/filepath"
  8. "runtime"
  9. "time"
  10. "github.com/mhsanaei/3x-ui/v3/config"
  11. "github.com/op/go-logging"
  12. "gopkg.in/natefinch/lumberjack.v2"
  13. )
  14. const (
  15. maxLogBufferSize = 10240 // Maximum log entries kept in memory
  16. logFileName = "3xui.log" // Log file name
  17. timeFormat = "2006/01/02 15:04:05" // Log timestamp format
  18. // On-disk rotation limits — single file capped, old segments pruned automatically.
  19. maxLogFileMB = 10 // rotate active log when larger than this
  20. maxLogBackups = 5 // rotated files retained (beyond current segment)
  21. maxLogAgeDays = 7 // remove rotated backups older than this (0 disables time-based pruning)
  22. compressRotated = true
  23. )
  24. var (
  25. logger *logging.Logger
  26. fileRotate *lumberjack.Logger // nil when file backend disabled
  27. // logBuffer maintains recent log entries in memory for web UI retrieval
  28. logBuffer []struct {
  29. time string
  30. level logging.Level
  31. log string
  32. }
  33. )
  34. // InitLogger initializes dual logging backends: console/syslog and file.
  35. // Console logging uses the specified level, file logging always uses DEBUG level.
  36. func InitLogger(level logging.Level) {
  37. newLogger := logging.MustGetLogger("x-ui")
  38. backends := make([]logging.Backend, 0, 2)
  39. // Console/syslog backend with configurable level
  40. if consoleBackend := initDefaultBackend(); consoleBackend != nil {
  41. leveledBackend := logging.AddModuleLevel(consoleBackend)
  42. leveledBackend.SetLevel(level, "x-ui")
  43. backends = append(backends, leveledBackend)
  44. }
  45. // File backend with DEBUG level for comprehensive logging
  46. if fileBackend := initFileBackend(); fileBackend != nil {
  47. leveledBackend := logging.AddModuleLevel(fileBackend)
  48. leveledBackend.SetLevel(logging.DEBUG, "x-ui")
  49. backends = append(backends, leveledBackend)
  50. }
  51. multiBackend := logging.MultiLogger(backends...)
  52. newLogger.SetBackend(multiBackend)
  53. logger = newLogger
  54. }
  55. // initDefaultBackend creates the console/syslog logging backend.
  56. // Windows: Uses stderr directly (no syslog support)
  57. // Unix-like: Attempts syslog, falls back to stderr
  58. func initDefaultBackend() logging.Backend {
  59. var backend logging.Backend
  60. includeTime := false
  61. if runtime.GOOS == "windows" {
  62. // Windows: Use stderr directly (no syslog support)
  63. backend = logging.NewLogBackend(os.Stderr, "", 0)
  64. includeTime = true
  65. } else {
  66. // Unix-like: Try syslog, fallback to stderr
  67. if syslogBackend, err := logging.NewSyslogBackend(""); err != nil {
  68. fmt.Fprintf(os.Stderr, "syslog backend disabled: %v\n", err)
  69. backend = logging.NewLogBackend(os.Stderr, "", 0)
  70. includeTime = os.Getppid() > 0
  71. } else {
  72. backend = syslogBackend
  73. }
  74. }
  75. return logging.NewBackendFormatter(backend, newFormatter(includeTime))
  76. }
  77. // initFileBackend creates the file logging backend with size/age‑bounded rotation
  78. // so log volume cannot grow without limit on disk.
  79. func initFileBackend() logging.Backend {
  80. logDir := config.GetLogFolder()
  81. if err := os.MkdirAll(logDir, 0o750); err != nil {
  82. fmt.Fprintf(os.Stderr, "failed to create log folder %s: %v\n", logDir, err)
  83. return nil
  84. }
  85. logPath := filepath.Join(logDir, logFileName)
  86. fileRotate = &lumberjack.Logger{
  87. Filename: logPath,
  88. MaxSize: maxLogFileMB,
  89. MaxBackups: maxLogBackups,
  90. MaxAge: maxLogAgeDays,
  91. LocalTime: true,
  92. Compress: compressRotated,
  93. }
  94. backend := logging.NewLogBackend(fileRotate, "", 0)
  95. return logging.NewBackendFormatter(backend, newFormatter(true))
  96. }
  97. // newFormatter creates a log formatter with optional timestamp.
  98. func newFormatter(withTime bool) logging.Formatter {
  99. format := `%{level} - %{message}`
  100. if withTime {
  101. format = `%{time:` + timeFormat + `} %{level} - %{message}`
  102. }
  103. return logging.MustStringFormatter(format)
  104. }
  105. // CloseLogger closes the rotating log writer and cleans up resources.
  106. // Should be called during application shutdown.
  107. func CloseLogger() {
  108. if fileRotate != nil {
  109. _ = fileRotate.Close()
  110. fileRotate = nil
  111. }
  112. }
  113. // Debug logs a debug message and adds it to the log buffer.
  114. func Debug(args ...any) {
  115. logger.Debug(args...)
  116. addToBuffer("DEBUG", fmt.Sprint(args...))
  117. }
  118. // Debugf logs a formatted debug message and adds it to the log buffer.
  119. func Debugf(format string, args ...any) {
  120. logger.Debugf(format, args...)
  121. addToBuffer("DEBUG", fmt.Sprintf(format, args...))
  122. }
  123. // Info logs an info message and adds it to the log buffer.
  124. func Info(args ...any) {
  125. logger.Info(args...)
  126. addToBuffer("INFO", fmt.Sprint(args...))
  127. }
  128. // Infof logs a formatted info message and adds it to the log buffer.
  129. func Infof(format string, args ...any) {
  130. logger.Infof(format, args...)
  131. addToBuffer("INFO", fmt.Sprintf(format, args...))
  132. }
  133. // Notice logs a notice message and adds it to the log buffer.
  134. func Notice(args ...any) {
  135. logger.Notice(args...)
  136. addToBuffer("NOTICE", fmt.Sprint(args...))
  137. }
  138. // Noticef logs a formatted notice message and adds it to the log buffer.
  139. func Noticef(format string, args ...any) {
  140. logger.Noticef(format, args...)
  141. addToBuffer("NOTICE", fmt.Sprintf(format, args...))
  142. }
  143. // Warning logs a warning message and adds it to the log buffer.
  144. func Warning(args ...any) {
  145. logger.Warning(args...)
  146. addToBuffer("WARNING", fmt.Sprint(args...))
  147. }
  148. // Warningf logs a formatted warning message and adds it to the log buffer.
  149. func Warningf(format string, args ...any) {
  150. logger.Warningf(format, args...)
  151. addToBuffer("WARNING", fmt.Sprintf(format, args...))
  152. }
  153. // Error logs an error message and adds it to the log buffer.
  154. func Error(args ...any) {
  155. logger.Error(args...)
  156. addToBuffer("ERROR", fmt.Sprint(args...))
  157. }
  158. // Errorf logs a formatted error message and adds it to the log buffer.
  159. func Errorf(format string, args ...any) {
  160. logger.Errorf(format, args...)
  161. addToBuffer("ERROR", fmt.Sprintf(format, args...))
  162. }
  163. // addToBuffer adds a log entry to the in-memory ring buffer for web UI retrieval.
  164. func addToBuffer(level string, newLog string) {
  165. t := time.Now()
  166. if len(logBuffer) >= maxLogBufferSize {
  167. logBuffer = logBuffer[1:]
  168. }
  169. logLevel, _ := logging.LogLevel(level)
  170. logBuffer = append(logBuffer, struct {
  171. time string
  172. level logging.Level
  173. log string
  174. }{
  175. time: t.Format(timeFormat),
  176. level: logLevel,
  177. log: newLog,
  178. })
  179. }
  180. // GetLogs retrieves up to c log entries from the buffer that are at or below the specified level.
  181. func GetLogs(c int, level string) []string {
  182. var output []string
  183. logLevel, _ := logging.LogLevel(level)
  184. for i := len(logBuffer) - 1; i >= 0 && len(output) <= c; i-- {
  185. if logBuffer[i].level <= logLevel {
  186. output = append(output, fmt.Sprintf("%s %s - %s", logBuffer[i].time, logBuffer[i].level, logBuffer[i].log))
  187. }
  188. }
  189. return output
  190. }