Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
FLIPPED-AURORA
gin-vue-admin
提交
e6273014
G
gin-vue-admin
项目概览
FLIPPED-AURORA
/
gin-vue-admin
大约 1 年 前同步成功
通知
329
Star
18154
Fork
5505
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
G
gin-vue-admin
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
e6273014
编写于
1月 02, 2020
作者:
Mr.奇淼(
提交者:
GitHub
1月 02, 2020
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #17 from granty1/master
Modify the logic of log
上级
38f158c0
d14c647d
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
51 addition
and
36 deletion
+51
-36
QMPlusServer/controller/servers/reportformat.go
QMPlusServer/controller/servers/reportformat.go
+2
-18
QMPlusServer/middleware/logger.go
QMPlusServer/middleware/logger.go
+49
-18
未找到文件。
QMPlusServer/controller/servers/reportformat.go
浏览文件 @
e6273014
package
servers
package
servers
import
(
import
(
"gin-vue-admin/init/qmlog"
"github.com/gin-gonic/gin"
"net/http"
"net/http"
"time"
"github.com/gin-gonic/gin"
)
)
func
ReportFormat
(
c
*
gin
.
Context
,
success
bool
,
msg
string
,
json
gin
.
H
)
{
func
ReportFormat
(
c
*
gin
.
Context
,
success
bool
,
msg
string
,
json
gin
.
H
)
{
// 开始时间
// 开始时间
start
:=
time
.
Now
()
path
:=
c
.
Request
.
URL
.
Path
clientIP
:=
c
.
ClientIP
()
method
:=
c
.
Request
.
Method
statusCode
:=
c
.
Writer
.
Status
()
qmlog
.
QMLog
.
Infof
(
"| %3d | %13v | %15s | %s %s |%s|"
,
statusCode
,
start
,
clientIP
,
method
,
path
,
gin
.
H
{
"success"
:
success
,
"msg"
:
msg
,
"data"
:
json
,
},
)
c
.
JSON
(
http
.
StatusOK
,
gin
.
H
{
c
.
JSON
(
http
.
StatusOK
,
gin
.
H
{
"success"
:
success
,
"success"
:
success
,
"msg"
:
msg
,
"msg"
:
msg
,
...
...
QMPlusServer/middleware/logger.go
浏览文件 @
e6273014
package
middleware
package
middleware
import
(
import
(
"bytes"
"gin-vue-admin/init/qmlog"
"gin-vue-admin/init/qmlog"
"github.com/gin-gonic/gin"
"net/http/httputil"
"strings"
"time"
"time"
"github.com/gin-gonic/gin"
)
)
func
Logger
()
gin
.
HandlerFunc
{
func
Logger
()
gin
.
HandlerFunc
{
log
:=
qmlog
.
QMLog
return
func
(
c
*
gin
.
Context
)
{
return
func
(
c
*
gin
.
Context
)
{
//
开始时间
//
request time
start
:=
time
.
Now
()
start
:=
time
.
Now
()
// 处理请求
// request path
c
.
Next
()
// 结束时间
end
:=
time
.
Now
()
//执行时间
latency
:=
end
.
Sub
(
start
)
path
:=
c
.
Request
.
URL
.
Path
path
:=
c
.
Request
.
URL
.
Path
logFlag
:=
true
if
strings
.
Contains
(
path
,
"swagger"
)
{
logFlag
=
false
}
// request ip
clientIP
:=
c
.
ClientIP
()
clientIP
:=
c
.
ClientIP
()
// method
method
:=
c
.
Request
.
Method
method
:=
c
.
Request
.
Method
// copy request content
req
,
_
:=
httputil
.
DumpRequest
(
c
.
Request
,
true
)
if
logFlag
{
log
.
Infof
(
`| %s | %s | %s | %5s | %s\n`
,
`Request :`
,
method
,
clientIP
,
path
,
string
(
req
))
}
// replace writer
cusWriter
:=
&
responseBodyWriter
{
ResponseWriter
:
c
.
Writer
,
body
:
bytes
.
NewBufferString
(
""
),
}
c
.
Writer
=
cusWriter
// handle request
c
.
Next
()
// ending time
end
:=
time
.
Now
()
//execute time
latency
:=
end
.
Sub
(
start
)
statusCode
:=
c
.
Writer
.
Status
()
statusCode
:=
c
.
Writer
.
Status
()
buf
:=
make
([]
byte
,
1024
)
if
logFlag
{
n
,
_
:=
c
.
Request
.
Body
.
Read
(
buf
)
log
.
Infof
(
`| %s | %3d | %13v | %s \n`
,
requestParams
:=
buf
[
0
:
n
]
`Response:`
,
qmlog
.
QMLog
.
Infof
(
"| %3d | %13v | %15s | %s %s |%s|"
,
statusCode
,
statusCode
,
latency
,
latency
,
cusWriter
.
body
.
String
())
clientIP
,
}
method
,
path
,
requestParams
,
)
}
}
}
}
type
responseBodyWriter
struct
{
gin
.
ResponseWriter
body
*
bytes
.
Buffer
}
func
(
w
responseBodyWriter
)
Write
(
b
[]
byte
)
(
int
,
error
)
{
w
.
body
.
Write
(
b
)
return
w
.
ResponseWriter
.
Write
(
b
)
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录