2025-08-14 20:05:06 +08:00
|
|
|
|
package logger
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"context"
|
|
|
|
|
|
"fmt"
|
|
|
|
|
|
"io"
|
|
|
|
|
|
"log"
|
|
|
|
|
|
"os"
|
|
|
|
|
|
"path/filepath"
|
|
|
|
|
|
"sync"
|
|
|
|
|
|
"time"
|
2025-08-15 13:20:36 +08:00
|
|
|
|
|
2025-10-11 15:30:09 +08:00
|
|
|
|
"github.com/QuantumNous/new-api/common"
|
|
|
|
|
|
"github.com/QuantumNous/new-api/setting/operation_setting"
|
|
|
|
|
|
|
2025-08-15 13:20:36 +08:00
|
|
|
|
"github.com/bytedance/gopkg/util/gopool"
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
2025-08-14 20:05:06 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
|
loggerINFO = "INFO"
|
|
|
|
|
|
loggerWarn = "WARN"
|
|
|
|
|
|
loggerError = "ERR"
|
|
|
|
|
|
loggerDebug = "DEBUG"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
const maxLogCount = 1000000
|
|
|
|
|
|
|
|
|
|
|
|
var logCount int
|
|
|
|
|
|
var setupLogLock sync.Mutex
|
|
|
|
|
|
var setupLogWorking bool
|
2026-03-21 20:06:49 +08:00
|
|
|
|
var currentLogPath string
|
|
|
|
|
|
var currentLogPathMu sync.RWMutex
|
|
|
|
|
|
var currentLogFile *os.File
|
|
|
|
|
|
|
|
|
|
|
|
func GetCurrentLogPath() string {
|
|
|
|
|
|
currentLogPathMu.RLock()
|
|
|
|
|
|
defer currentLogPathMu.RUnlock()
|
|
|
|
|
|
return currentLogPath
|
|
|
|
|
|
}
|
2025-08-14 20:05:06 +08:00
|
|
|
|
|
|
|
|
|
|
func SetupLogger() {
|
2025-08-15 13:20:36 +08:00
|
|
|
|
defer func() {
|
|
|
|
|
|
setupLogWorking = false
|
|
|
|
|
|
}()
|
2025-08-14 20:05:06 +08:00
|
|
|
|
if *common.LogDir != "" {
|
|
|
|
|
|
ok := setupLogLock.TryLock()
|
|
|
|
|
|
if !ok {
|
|
|
|
|
|
log.Println("setup log is already working")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
defer func() {
|
|
|
|
|
|
setupLogLock.Unlock()
|
|
|
|
|
|
}()
|
|
|
|
|
|
logPath := filepath.Join(*common.LogDir, fmt.Sprintf("oneapi-%s.log", time.Now().Format("20060102150405")))
|
|
|
|
|
|
fd, err := os.OpenFile(logPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
log.Fatal("failed to open log file")
|
|
|
|
|
|
}
|
2026-03-21 20:06:49 +08:00
|
|
|
|
currentLogPathMu.Lock()
|
|
|
|
|
|
oldFile := currentLogFile
|
|
|
|
|
|
currentLogPath = logPath
|
|
|
|
|
|
currentLogFile = fd
|
2026-03-21 20:40:39 +08:00
|
|
|
|
currentLogPathMu.Unlock()
|
|
|
|
|
|
|
|
|
|
|
|
common.LogWriterMu.Lock()
|
2025-08-14 20:05:06 +08:00
|
|
|
|
gin.DefaultWriter = io.MultiWriter(os.Stdout, fd)
|
|
|
|
|
|
gin.DefaultErrorWriter = io.MultiWriter(os.Stderr, fd)
|
2026-03-21 20:06:49 +08:00
|
|
|
|
if oldFile != nil {
|
|
|
|
|
|
_ = oldFile.Close()
|
|
|
|
|
|
}
|
2026-03-21 20:40:39 +08:00
|
|
|
|
common.LogWriterMu.Unlock()
|
2025-08-14 20:05:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func LogInfo(ctx context.Context, msg string) {
|
|
|
|
|
|
logHelper(ctx, loggerINFO, msg)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func LogWarn(ctx context.Context, msg string) {
|
|
|
|
|
|
logHelper(ctx, loggerWarn, msg)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func LogError(ctx context.Context, msg string) {
|
|
|
|
|
|
logHelper(ctx, loggerError, msg)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-28 23:25:43 +08:00
|
|
|
|
func LogDebug(ctx context.Context, msg string, args ...any) {
|
2025-08-14 20:05:06 +08:00
|
|
|
|
if common.DebugEnabled {
|
2025-11-04 14:56:53 +08:00
|
|
|
|
if len(args) > 0 {
|
|
|
|
|
|
msg = fmt.Sprintf(msg, args...)
|
|
|
|
|
|
}
|
2025-08-14 20:05:06 +08:00
|
|
|
|
logHelper(ctx, loggerDebug, msg)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func logHelper(ctx context.Context, level string, msg string) {
|
|
|
|
|
|
id := ctx.Value(common.RequestIdKey)
|
|
|
|
|
|
if id == nil {
|
|
|
|
|
|
id = "SYSTEM"
|
|
|
|
|
|
}
|
|
|
|
|
|
now := time.Now()
|
2026-03-21 20:40:39 +08:00
|
|
|
|
common.LogWriterMu.RLock()
|
|
|
|
|
|
writer := gin.DefaultErrorWriter
|
|
|
|
|
|
if level == loggerINFO {
|
|
|
|
|
|
writer = gin.DefaultWriter
|
|
|
|
|
|
}
|
2025-08-14 20:05:06 +08:00
|
|
|
|
_, _ = fmt.Fprintf(writer, "[%s] %v | %s | %s \n", level, now.Format("2006/01/02 - 15:04:05"), id, msg)
|
2026-03-21 20:40:39 +08:00
|
|
|
|
common.LogWriterMu.RUnlock()
|
2025-08-14 20:05:06 +08:00
|
|
|
|
logCount++ // we don't need accurate count, so no lock here
|
|
|
|
|
|
if logCount > maxLogCount && !setupLogWorking {
|
|
|
|
|
|
logCount = 0
|
|
|
|
|
|
setupLogWorking = true
|
|
|
|
|
|
gopool.Go(func() {
|
|
|
|
|
|
SetupLogger()
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func LogQuota(quota int) string {
|
2025-09-29 23:23:31 +08:00
|
|
|
|
// 新逻辑:根据额度展示类型输出
|
|
|
|
|
|
q := float64(quota)
|
|
|
|
|
|
switch operation_setting.GetQuotaDisplayType() {
|
|
|
|
|
|
case operation_setting.QuotaDisplayTypeCNY:
|
|
|
|
|
|
usd := q / common.QuotaPerUnit
|
|
|
|
|
|
cny := usd * operation_setting.USDExchangeRate
|
|
|
|
|
|
return fmt.Sprintf("¥%.6f 额度", cny)
|
|
|
|
|
|
case operation_setting.QuotaDisplayTypeCustom:
|
|
|
|
|
|
usd := q / common.QuotaPerUnit
|
|
|
|
|
|
rate := operation_setting.GetGeneralSetting().CustomCurrencyExchangeRate
|
|
|
|
|
|
symbol := operation_setting.GetGeneralSetting().CustomCurrencySymbol
|
|
|
|
|
|
if symbol == "" {
|
|
|
|
|
|
symbol = "¤"
|
|
|
|
|
|
}
|
|
|
|
|
|
if rate <= 0 {
|
|
|
|
|
|
rate = 1
|
|
|
|
|
|
}
|
|
|
|
|
|
v := usd * rate
|
|
|
|
|
|
return fmt.Sprintf("%s%.6f 额度", symbol, v)
|
|
|
|
|
|
case operation_setting.QuotaDisplayTypeTokens:
|
2025-08-14 20:05:06 +08:00
|
|
|
|
return fmt.Sprintf("%d 点额度", quota)
|
2025-09-29 23:23:31 +08:00
|
|
|
|
default: // USD
|
|
|
|
|
|
return fmt.Sprintf("$%.6f 额度", q/common.QuotaPerUnit)
|
2025-08-14 20:05:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func FormatQuota(quota int) string {
|
2025-09-29 23:23:31 +08:00
|
|
|
|
q := float64(quota)
|
|
|
|
|
|
switch operation_setting.GetQuotaDisplayType() {
|
|
|
|
|
|
case operation_setting.QuotaDisplayTypeCNY:
|
|
|
|
|
|
usd := q / common.QuotaPerUnit
|
|
|
|
|
|
cny := usd * operation_setting.USDExchangeRate
|
|
|
|
|
|
return fmt.Sprintf("¥%.6f", cny)
|
|
|
|
|
|
case operation_setting.QuotaDisplayTypeCustom:
|
|
|
|
|
|
usd := q / common.QuotaPerUnit
|
|
|
|
|
|
rate := operation_setting.GetGeneralSetting().CustomCurrencyExchangeRate
|
|
|
|
|
|
symbol := operation_setting.GetGeneralSetting().CustomCurrencySymbol
|
|
|
|
|
|
if symbol == "" {
|
|
|
|
|
|
symbol = "¤"
|
|
|
|
|
|
}
|
|
|
|
|
|
if rate <= 0 {
|
|
|
|
|
|
rate = 1
|
|
|
|
|
|
}
|
|
|
|
|
|
v := usd * rate
|
|
|
|
|
|
return fmt.Sprintf("%s%.6f", symbol, v)
|
|
|
|
|
|
case operation_setting.QuotaDisplayTypeTokens:
|
2025-08-14 20:05:06 +08:00
|
|
|
|
return fmt.Sprintf("%d", quota)
|
2025-09-29 23:23:31 +08:00
|
|
|
|
default:
|
|
|
|
|
|
return fmt.Sprintf("$%.6f", q/common.QuotaPerUnit)
|
2025-08-14 20:05:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// LogJson 仅供测试使用 only for test
|
|
|
|
|
|
func LogJson(ctx context.Context, msg string, obj any) {
|
2026-02-10 21:15:09 +08:00
|
|
|
|
jsonStr, err := common.Marshal(obj)
|
2025-08-14 20:05:06 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
LogError(ctx, fmt.Sprintf("json marshal failed: %s", err.Error()))
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2025-10-16 12:38:21 +08:00
|
|
|
|
LogDebug(ctx, fmt.Sprintf("%s | %s", msg, string(jsonStr)))
|
2025-08-14 20:05:06 +08:00
|
|
|
|
}
|