1
0

logger.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package logger
  2. import (
  3. "fmt"
  4. "os"
  5. "time"
  6. "github.com/op/go-logging"
  7. )
  8. var (
  9. logger *logging.Logger
  10. logBuffer []struct {
  11. time string
  12. level logging.Level
  13. log string
  14. }
  15. )
  16. func init() {
  17. InitLogger(logging.INFO)
  18. }
  19. func InitLogger(level logging.Level) {
  20. newLogger := logging.MustGetLogger("x-ui")
  21. var err error
  22. var backend logging.Backend
  23. var format logging.Formatter
  24. ppid := os.Getppid()
  25. backend, err = logging.NewSyslogBackend("")
  26. if err != nil {
  27. println(err)
  28. backend = logging.NewLogBackend(os.Stderr, "", 0)
  29. }
  30. if ppid > 0 && err != nil {
  31. format = logging.MustStringFormatter(`%{time:2006/01/02 15:04:05} %{level} - %{message}`)
  32. } else {
  33. format = logging.MustStringFormatter(`%{level} - %{message}`)
  34. }
  35. backendFormatter := logging.NewBackendFormatter(backend, format)
  36. backendLeveled := logging.AddModuleLevel(backendFormatter)
  37. backendLeveled.SetLevel(level, "x-ui")
  38. newLogger.SetBackend(backendLeveled)
  39. logger = newLogger
  40. }
  41. func Debug(args ...interface{}) {
  42. logger.Debug(args...)
  43. addToBuffer("DEBUG", fmt.Sprint(args...))
  44. }
  45. func Debugf(format string, args ...interface{}) {
  46. logger.Debugf(format, args...)
  47. addToBuffer("DEBUG", fmt.Sprintf(format, args...))
  48. }
  49. func Info(args ...interface{}) {
  50. logger.Info(args...)
  51. addToBuffer("INFO", fmt.Sprint(args...))
  52. }
  53. func Infof(format string, args ...interface{}) {
  54. logger.Infof(format, args...)
  55. addToBuffer("INFO", fmt.Sprintf(format, args...))
  56. }
  57. func Notice(args ...interface{}) {
  58. logger.Notice(args...)
  59. addToBuffer("NOTICE", fmt.Sprint(args...))
  60. }
  61. func Noticef(format string, args ...interface{}) {
  62. logger.Noticef(format, args...)
  63. addToBuffer("NOTICE", fmt.Sprintf(format, args...))
  64. }
  65. func Warning(args ...interface{}) {
  66. logger.Warning(args...)
  67. addToBuffer("WARNING", fmt.Sprint(args...))
  68. }
  69. func Warningf(format string, args ...interface{}) {
  70. logger.Warningf(format, args...)
  71. addToBuffer("WARNING", fmt.Sprintf(format, args...))
  72. }
  73. func Error(args ...interface{}) {
  74. logger.Error(args...)
  75. addToBuffer("ERROR", fmt.Sprint(args...))
  76. }
  77. func Errorf(format string, args ...interface{}) {
  78. logger.Errorf(format, args...)
  79. addToBuffer("ERROR", fmt.Sprintf(format, args...))
  80. }
  81. func addToBuffer(level string, newLog string) {
  82. t := time.Now()
  83. if len(logBuffer) >= 10240 {
  84. logBuffer = logBuffer[1:]
  85. }
  86. logLevel, _ := logging.LogLevel(level)
  87. logBuffer = append(logBuffer, struct {
  88. time string
  89. level logging.Level
  90. log string
  91. }{
  92. time: t.Format("2006/01/02 15:04:05"),
  93. level: logLevel,
  94. log: newLog,
  95. })
  96. }
  97. func GetLogs(c int, level string) []string {
  98. var output []string
  99. logLevel, _ := logging.LogLevel(level)
  100. for i := len(logBuffer) - 1; i >= 0 && len(output) <= c; i-- {
  101. if logBuffer[i].level <= logLevel {
  102. output = append(output, fmt.Sprintf("%s %s - %s", logBuffer[i].time, logBuffer[i].level, logBuffer[i].log))
  103. }
  104. }
  105. return output
  106. }