logger.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. package logger
  2. import (
  3. "context"
  4. "fmt"
  5. "io"
  6. "log"
  7. "os"
  8. "path/filepath"
  9. "sync"
  10. "time"
  11. "github.com/QuantumNous/new-api/common"
  12. "github.com/QuantumNous/new-api/setting/operation_setting"
  13. "github.com/bytedance/gopkg/util/gopool"
  14. "github.com/gin-gonic/gin"
  15. )
  16. const (
  17. loggerINFO = "INFO"
  18. loggerWarn = "WARN"
  19. loggerError = "ERR"
  20. loggerDebug = "DEBUG"
  21. )
  22. const maxLogCount = 1000000
  23. var logCount int
  24. var setupLogLock sync.Mutex
  25. var setupLogWorking bool
  26. var currentLogPath string
  27. var currentLogPathMu sync.RWMutex
  28. var currentLogFile *os.File
  29. func GetCurrentLogPath() string {
  30. currentLogPathMu.RLock()
  31. defer currentLogPathMu.RUnlock()
  32. return currentLogPath
  33. }
  34. func SetupLogger() {
  35. defer func() {
  36. setupLogWorking = false
  37. }()
  38. if *common.LogDir != "" {
  39. ok := setupLogLock.TryLock()
  40. if !ok {
  41. log.Println("setup log is already working")
  42. return
  43. }
  44. defer func() {
  45. setupLogLock.Unlock()
  46. }()
  47. logPath := filepath.Join(*common.LogDir, fmt.Sprintf("oneapi-%s.log", time.Now().Format("20060102150405")))
  48. fd, err := os.OpenFile(logPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
  49. if err != nil {
  50. log.Fatal("failed to open log file")
  51. }
  52. currentLogPathMu.Lock()
  53. oldFile := currentLogFile
  54. currentLogPath = logPath
  55. currentLogFile = fd
  56. currentLogPathMu.Unlock()
  57. common.LogWriterMu.Lock()
  58. gin.DefaultWriter = io.MultiWriter(os.Stdout, fd)
  59. gin.DefaultErrorWriter = io.MultiWriter(os.Stderr, fd)
  60. if oldFile != nil {
  61. _ = oldFile.Close()
  62. }
  63. common.LogWriterMu.Unlock()
  64. }
  65. }
  66. func LogInfo(ctx context.Context, msg string) {
  67. logHelper(ctx, loggerINFO, msg)
  68. }
  69. func LogWarn(ctx context.Context, msg string) {
  70. logHelper(ctx, loggerWarn, msg)
  71. }
  72. func LogError(ctx context.Context, msg string) {
  73. logHelper(ctx, loggerError, msg)
  74. }
  75. func LogDebug(ctx context.Context, msg string, args ...any) {
  76. if common.DebugEnabled {
  77. if len(args) > 0 {
  78. msg = fmt.Sprintf(msg, args...)
  79. }
  80. logHelper(ctx, loggerDebug, msg)
  81. }
  82. }
  83. func logHelper(ctx context.Context, level string, msg string) {
  84. id := ctx.Value(common.RequestIdKey)
  85. if id == nil {
  86. id = "SYSTEM"
  87. }
  88. now := time.Now()
  89. common.LogWriterMu.RLock()
  90. writer := gin.DefaultErrorWriter
  91. if level == loggerINFO {
  92. writer = gin.DefaultWriter
  93. }
  94. _, _ = fmt.Fprintf(writer, "[%s] %v | %s | %s \n", level, now.Format("2006/01/02 - 15:04:05"), id, msg)
  95. common.LogWriterMu.RUnlock()
  96. logCount++ // we don't need accurate count, so no lock here
  97. if logCount > maxLogCount && !setupLogWorking {
  98. logCount = 0
  99. setupLogWorking = true
  100. gopool.Go(func() {
  101. SetupLogger()
  102. })
  103. }
  104. }
  105. func LogQuota(quota int) string {
  106. // 新逻辑:根据额度展示类型输出
  107. q := float64(quota)
  108. switch operation_setting.GetQuotaDisplayType() {
  109. case operation_setting.QuotaDisplayTypeCNY:
  110. usd := q / common.QuotaPerUnit
  111. cny := usd * operation_setting.USDExchangeRate
  112. return fmt.Sprintf("¥%.6f 额度", cny)
  113. case operation_setting.QuotaDisplayTypeCustom:
  114. usd := q / common.QuotaPerUnit
  115. rate := operation_setting.GetGeneralSetting().CustomCurrencyExchangeRate
  116. symbol := operation_setting.GetGeneralSetting().CustomCurrencySymbol
  117. if symbol == "" {
  118. symbol = "¤"
  119. }
  120. if rate <= 0 {
  121. rate = 1
  122. }
  123. v := usd * rate
  124. return fmt.Sprintf("%s%.6f 额度", symbol, v)
  125. case operation_setting.QuotaDisplayTypeTokens:
  126. return fmt.Sprintf("%d 点额度", quota)
  127. default: // USD
  128. return fmt.Sprintf("$%.6f 额度", q/common.QuotaPerUnit)
  129. }
  130. }
  131. func FormatQuota(quota int) string {
  132. q := float64(quota)
  133. switch operation_setting.GetQuotaDisplayType() {
  134. case operation_setting.QuotaDisplayTypeCNY:
  135. usd := q / common.QuotaPerUnit
  136. cny := usd * operation_setting.USDExchangeRate
  137. return fmt.Sprintf("¥%.6f", cny)
  138. case operation_setting.QuotaDisplayTypeCustom:
  139. usd := q / common.QuotaPerUnit
  140. rate := operation_setting.GetGeneralSetting().CustomCurrencyExchangeRate
  141. symbol := operation_setting.GetGeneralSetting().CustomCurrencySymbol
  142. if symbol == "" {
  143. symbol = "¤"
  144. }
  145. if rate <= 0 {
  146. rate = 1
  147. }
  148. v := usd * rate
  149. return fmt.Sprintf("%s%.6f", symbol, v)
  150. case operation_setting.QuotaDisplayTypeTokens:
  151. return fmt.Sprintf("%d", quota)
  152. default:
  153. return fmt.Sprintf("$%.6f", q/common.QuotaPerUnit)
  154. }
  155. }
  156. // LogJson 仅供测试使用 only for test
  157. func LogJson(ctx context.Context, msg string, obj any) {
  158. jsonStr, err := common.Marshal(obj)
  159. if err != nil {
  160. LogError(ctx, fmt.Sprintf("json marshal failed: %s", err.Error()))
  161. return
  162. }
  163. LogDebug(ctx, fmt.Sprintf("%s | %s", msg, string(jsonStr)))
  164. }