Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
phsl
/
new-api
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
4f123cf9
authored
Apr 28, 2025
by
creamlike1024
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'error-logs' of github.com:zenghongtu/new-api into zenghongtu-error-logs
parents
4759bbdb
75f0cb8e
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
69 additions
and
11 deletions
+69
-11
controller/relay.go
+20
-0
model/log.go
+30
-0
web/src/components/LogsTable.js
+19
-11
No files found.
controller/relay.go
View file @
4f123cf9
...
...
@@ -39,6 +39,26 @@ func relayHandler(c *gin.Context, relayMode int) *dto.OpenAIErrorWithStatusCode
default
:
err
=
relay
.
TextHelper
(
c
)
}
if
err
!=
nil
{
// 保存错误日志到mysql中
userId
:=
c
.
GetInt
(
"id"
)
tokenName
:=
c
.
GetString
(
"token_name"
)
modelName
:=
c
.
GetString
(
"original_model"
)
tokenId
:=
c
.
GetInt
(
"token_id"
)
userGroup
:=
c
.
GetString
(
"group"
)
channelId
:=
c
.
GetInt
(
"channel_id"
)
other
:=
make
(
map
[
string
]
interface
{})
other
[
"error_type"
]
=
err
.
Error
.
Type
other
[
"error_code"
]
=
err
.
Error
.
Code
other
[
"status_code"
]
=
err
.
StatusCode
other
[
"channel_id"
]
=
channelId
other
[
"channel_name"
]
=
c
.
GetString
(
"channel_name"
)
other
[
"channel_type"
]
=
c
.
GetInt
(
"channel_type"
)
model
.
RecordErrorLog
(
c
,
userId
,
channelId
,
modelName
,
tokenName
,
err
.
Error
.
Message
,
tokenId
,
0
,
false
,
userGroup
,
other
)
}
return
err
}
...
...
model/log.go
View file @
4f123cf9
...
...
@@ -40,6 +40,7 @@ const (
LogTypeConsume
LogTypeManage
LogTypeSystem
LogTypeError
)
func
formatUserLogs
(
logs
[]
*
Log
)
{
...
...
@@ -88,6 +89,35 @@ func RecordLog(userId int, logType int, content string) {
}
}
func
RecordErrorLog
(
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
{})
{
common
.
LogInfo
(
c
,
fmt
.
Sprintf
(
"record error log: userId=%d, channelId=%d, modelName=%s, tokenName=%s, content=%s"
,
userId
,
channelId
,
modelName
,
tokenName
,
content
))
username
:=
c
.
GetString
(
"username"
)
otherStr
:=
common
.
MapToJsonStr
(
other
)
log
:=
&
Log
{
UserId
:
userId
,
Username
:
username
,
CreatedAt
:
common
.
GetTimestamp
(),
Type
:
LogTypeError
,
Content
:
content
,
PromptTokens
:
0
,
CompletionTokens
:
0
,
TokenName
:
tokenName
,
ModelName
:
modelName
,
Quota
:
0
,
ChannelId
:
channelId
,
TokenId
:
tokenId
,
UseTime
:
useTimeSeconds
,
IsStream
:
isStream
,
Group
:
group
,
Other
:
otherStr
,
}
err
:=
LOG_DB
.
Create
(
log
)
.
Error
if
err
!=
nil
{
common
.
LogError
(
c
,
"failed to record log: "
+
err
.
Error
())
}
}
func
RecordConsumeLog
(
c
*
gin
.
Context
,
userId
int
,
channelId
int
,
promptTokens
int
,
completionTokens
int
,
modelName
string
,
tokenName
string
,
quota
int
,
content
string
,
tokenId
int
,
userQuota
int
,
useTimeSeconds
int
,
isStream
bool
,
group
string
,
other
map
[
string
]
interface
{})
{
...
...
web/src/components/LogsTable.js
View file @
4f123cf9
...
...
@@ -103,9 +103,15 @@ const LogsTable = () => {
{
t
(
'系统'
)}
<
/Tag
>
);
case
5
:
return
(
<
Tag
color
=
'red'
size
=
'large'
>
{
t
(
'错误'
)}
<
/Tag
>
);
default
:
return
(
<
Tag
color
=
'
black
'
size
=
'large'
>
<
Tag
color
=
'
grey
'
size
=
'large'
>
{
t
(
'未知'
)}
<
/Tag
>
);
...
...
@@ -373,7 +379,7 @@ const LogsTable = () => {
className
:
isAdmin
()
?
'tableShow'
:
'tableHiddle'
,
render
:
(
text
,
record
,
index
)
=>
{
return
isAdminUser
?
(
record
.
type
===
0
||
record
.
type
===
2
?
(
record
.
type
===
0
||
record
.
type
===
2
||
record
.
type
===
5
?
(
<
div
>
{
<
Tooltip
content
=
{
record
.
channel_name
||
'[未知]'
}
>
...
...
@@ -426,7 +432,7 @@ const LogsTable = () => {
title
:
t
(
'令牌'
),
dataIndex
:
'token_name'
,
render
:
(
text
,
record
,
index
)
=>
{
return
record
.
type
===
0
||
record
.
type
===
2
?
(
return
record
.
type
===
0
||
record
.
type
===
2
||
record
.
type
===
5
?
(
<
div
>
<
Tag
color
=
'grey'
...
...
@@ -450,7 +456,7 @@ const LogsTable = () => {
title
:
t
(
'分组'
),
dataIndex
:
'group'
,
render
:
(
text
,
record
,
index
)
=>
{
if
(
record
.
type
===
0
||
record
.
type
===
2
)
{
if
(
record
.
type
===
0
||
record
.
type
===
2
||
record
.
type
===
5
)
{
if
(
record
.
group
)
{
return
<>
{
renderGroup
(
record
.
group
)}
<
/>
;
}
else
{
...
...
@@ -490,7 +496,7 @@ const LogsTable = () => {
title
:
t
(
'模型'
),
dataIndex
:
'model_name'
,
render
:
(
text
,
record
,
index
)
=>
{
return
record
.
type
===
0
||
record
.
type
===
2
?
(
return
record
.
type
===
0
||
record
.
type
===
2
||
record
.
type
===
5
?
(
<>
{
renderModelName
(
record
)}
<
/
>
)
:
(
<><
/
>
...
...
@@ -530,7 +536,7 @@ const LogsTable = () => {
title
:
t
(
'提示'
),
dataIndex
:
'prompt_tokens'
,
render
:
(
text
,
record
,
index
)
=>
{
return
record
.
type
===
0
||
record
.
type
===
2
?
(
return
record
.
type
===
0
||
record
.
type
===
2
||
record
.
type
===
5
?
(
<>
{
<
span
>
{
text
}
<
/span>}</
>
)
:
(
<><
/
>
...
...
@@ -543,7 +549,7 @@ const LogsTable = () => {
dataIndex
:
'completion_tokens'
,
render
:
(
text
,
record
,
index
)
=>
{
return
parseInt
(
text
)
>
0
&&
(
record
.
type
===
0
||
record
.
type
===
2
)
?
(
(
record
.
type
===
0
||
record
.
type
===
2
||
record
.
type
===
5
)
?
(
<>
{
<
span
>
{
text
}
<
/span>}</
>
)
:
(
<><
/
>
...
...
@@ -555,7 +561,7 @@ const LogsTable = () => {
title
:
t
(
'花费'
),
dataIndex
:
'quota'
,
render
:
(
text
,
record
,
index
)
=>
{
return
record
.
type
===
0
||
record
.
type
===
2
?
(
return
record
.
type
===
0
||
record
.
type
===
2
||
record
.
type
===
5
?
(
<>
{
renderQuota
(
text
,
6
)}
<
/
>
)
:
(
<><
/
>
...
...
@@ -986,9 +992,10 @@ const LogsTable = () => {
other
?.
group_ratio
,
other
?.
cache_tokens
||
0
,
other
?.
cache_ratio
||
1.0
,
other
?.
image
||
false
,
other
?.
image_ratio
||
0
,
other
?.
image_output
||
0
,);
other
?.
image
||
false
,
other
?.
image_ratio
||
0
,
other
?.
image_output
||
0
,
);
}
expandDataLocal
.
push
({
key
:
t
(
'计费过程'
),
...
...
@@ -1247,6 +1254,7 @@ const LogsTable = () => {
<
Select
.
Option
value
=
'2'
>
{
t
(
'消费'
)}
<
/Select.Option
>
<
Select
.
Option
value
=
'3'
>
{
t
(
'管理'
)}
<
/Select.Option
>
<
Select
.
Option
value
=
'4'
>
{
t
(
'系统'
)}
<
/Select.Option
>
<
Select
.
Option
value
=
'5'
>
{
t
(
'错误'
)}
<
/Select.Option
>
<
/Select
>
<
Button
theme
=
'light'
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment