logger.go 3.9 KB

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