logger.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // Package logger provides logging functionality for the 3x-ui panel with
  2. // buffered log storage and multiple log levels.
  3. package logger
  4. import (
  5. "fmt"
  6. "os"
  7. "time"
  8. "github.com/op/go-logging"
  9. )
  10. var (
  11. logger *logging.Logger
  12. // addToBuffer appends a log entry into the in-memory ring buffer used for
  13. // retrieving recent logs via the web UI. It keeps the buffer bounded to avoid
  14. // uncontrolled growth.
  15. logBuffer []struct {
  16. time string
  17. level logging.Level
  18. log string
  19. }
  20. )
  21. func init() {
  22. InitLogger(logging.INFO)
  23. }
  24. // InitLogger initializes the logger with the specified logging level.
  25. func InitLogger(level logging.Level) {
  26. newLogger := logging.MustGetLogger("x-ui")
  27. var err error
  28. var backend logging.Backend
  29. var format logging.Formatter
  30. ppid := os.Getppid()
  31. backend, err = logging.NewSyslogBackend("")
  32. if err != nil {
  33. println(err)
  34. backend = logging.NewLogBackend(os.Stderr, "", 0)
  35. }
  36. if ppid > 0 && err != nil {
  37. format = logging.MustStringFormatter(`%{time:2006/01/02 15:04:05} %{level} - %{message}`)
  38. } else {
  39. format = logging.MustStringFormatter(`%{level} - %{message}`)
  40. }
  41. backendFormatter := logging.NewBackendFormatter(backend, format)
  42. backendLeveled := logging.AddModuleLevel(backendFormatter)
  43. backendLeveled.SetLevel(level, "x-ui")
  44. newLogger.SetBackend(backendLeveled)
  45. logger = newLogger
  46. }
  47. // Debug logs a debug message and adds it to the log buffer.
  48. func Debug(args ...any) {
  49. logger.Debug(args...)
  50. addToBuffer("DEBUG", fmt.Sprint(args...))
  51. }
  52. // Debugf logs a formatted debug message and adds it to the log buffer.
  53. func Debugf(format string, args ...any) {
  54. logger.Debugf(format, args...)
  55. addToBuffer("DEBUG", fmt.Sprintf(format, args...))
  56. }
  57. // Info logs an info message and adds it to the log buffer.
  58. func Info(args ...any) {
  59. logger.Info(args...)
  60. addToBuffer("INFO", fmt.Sprint(args...))
  61. }
  62. // Infof logs a formatted info message and adds it to the log buffer.
  63. func Infof(format string, args ...any) {
  64. logger.Infof(format, args...)
  65. addToBuffer("INFO", fmt.Sprintf(format, args...))
  66. }
  67. // Notice logs a notice message and adds it to the log buffer.
  68. func Notice(args ...any) {
  69. logger.Notice(args...)
  70. addToBuffer("NOTICE", fmt.Sprint(args...))
  71. }
  72. // Noticef logs a formatted notice message and adds it to the log buffer.
  73. func Noticef(format string, args ...any) {
  74. logger.Noticef(format, args...)
  75. addToBuffer("NOTICE", fmt.Sprintf(format, args...))
  76. }
  77. // Warning logs a warning message and adds it to the log buffer.
  78. func Warning(args ...any) {
  79. logger.Warning(args...)
  80. addToBuffer("WARNING", fmt.Sprint(args...))
  81. }
  82. // Warningf logs a formatted warning message and adds it to the log buffer.
  83. func Warningf(format string, args ...any) {
  84. logger.Warningf(format, args...)
  85. addToBuffer("WARNING", fmt.Sprintf(format, args...))
  86. }
  87. // Error logs an error message and adds it to the log buffer.
  88. func Error(args ...any) {
  89. logger.Error(args...)
  90. addToBuffer("ERROR", fmt.Sprint(args...))
  91. }
  92. // Errorf logs a formatted error message and adds it to the log buffer.
  93. func Errorf(format string, args ...any) {
  94. logger.Errorf(format, args...)
  95. addToBuffer("ERROR", fmt.Sprintf(format, args...))
  96. }
  97. func addToBuffer(level string, newLog string) {
  98. t := time.Now()
  99. if len(logBuffer) >= 10240 {
  100. logBuffer = logBuffer[1:]
  101. }
  102. logLevel, _ := logging.LogLevel(level)
  103. logBuffer = append(logBuffer, struct {
  104. time string
  105. level logging.Level
  106. log string
  107. }{
  108. time: t.Format("2006/01/02 15:04:05"),
  109. level: logLevel,
  110. log: newLog,
  111. })
  112. }
  113. // GetLogs retrieves up to c log entries from the buffer that are at or below the specified level.
  114. func GetLogs(c int, level string) []string {
  115. var output []string
  116. logLevel, _ := logging.LogLevel(level)
  117. for i := len(logBuffer) - 1; i >= 0 && len(output) <= c; i-- {
  118. if logBuffer[i].level <= logLevel {
  119. output = append(output, fmt.Sprintf("%s %s - %s", logBuffer[i].time, logBuffer[i].level, logBuffer[i].log))
  120. }
  121. }
  122. return output
  123. }