1
0

logger.go 7.4 KB

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