1
0

logger.go 7.2 KB

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