(c *gin.Context, userId int, channelId int, modelName string, tokenName string, content string, tokenId int, useTimeSeconds int,
isStream bool, group string, other map[string]interface{})
| 94 | } |
| 95 | |
| 96 | func RecordErrorLog(c *gin.Context, userId int, channelId int, modelName string, tokenName string, content string, tokenId int, useTimeSeconds int, |
| 97 | isStream bool, group string, other map[string]interface{}) { |
| 98 | common.LogInfo(c, fmt.Sprintf("record error log: userId=%d, channelId=%d, modelName=%s, tokenName=%s, content=%s", userId, channelId, modelName, tokenName, content)) |
| 99 | username := c.GetString("username") |
| 100 | otherStr := common.MapToJsonStr(other) |
| 101 | // 判断是否需要记录 IP |
| 102 | needRecordIp := false |
| 103 | if settingMap, err := GetUserSetting(userId, false); err == nil { |
| 104 | if settingMap.RecordIpLog { |
| 105 | needRecordIp = true |
| 106 | } |
| 107 | } |
| 108 | log := &Log{ |
| 109 | UserId: userId, |
| 110 | Username: username, |
| 111 | CreatedAt: common.GetTimestamp(), |
| 112 | Type: LogTypeError, |
| 113 | Content: content, |
| 114 | PromptTokens: 0, |
| 115 | CompletionTokens: 0, |
| 116 | TokenName: tokenName, |
| 117 | ModelName: modelName, |
| 118 | Quota: 0, |
| 119 | ChannelId: channelId, |
| 120 | TokenId: tokenId, |
| 121 | UseTime: useTimeSeconds, |
| 122 | IsStream: isStream, |
| 123 | Group: group, |
| 124 | Ip: func() string { |
| 125 | if needRecordIp { |
| 126 | return c.ClientIP() |
| 127 | } |
| 128 | return "" |
| 129 | }(), |
| 130 | Other: otherStr, |
| 131 | } |
| 132 | err := LOG_DB.Create(log).Error |
| 133 | if err != nil { |
| 134 | common.LogError(c, "failed to record log: "+err.Error()) |
| 135 | } |
| 136 | |
| 137 | // 异步记录用量统计(失败请求) |
| 138 | gopool.Go(func() { |
| 139 | if recordErr := RecordUsageStatistics(tokenId, tokenName, modelName, |
| 140 | 0, 0, 0, false); recordErr != nil { |
| 141 | common.SysError("failed to record usage statistics: " + recordErr.Error()) |
| 142 | } |
| 143 | }) |
| 144 | } |
| 145 | |
| 146 | type RecordConsumeLogParams struct { |
| 147 | ChannelId int `json:"channel_id"` |
no test coverage detected