Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
飞一样的人
gin-vue-admin
提交
778e5bb9
G
gin-vue-admin
项目概览
飞一样的人
/
gin-vue-admin
与 Fork 源项目一致
Fork自
FLIPPED-AURORA / gin-vue-admin
通知
1
Star
1
Fork
0
代码
文件
提交
分支
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,发现更多精彩内容 >>
未验证
提交
778e5bb9
编写于
7月 10, 2021
作者:
Mr.奇淼(
提交者:
GitHub
7月 10, 2021
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #569 from SliverHorn/master
update: 自定义gorm的logger优化
上级
015912a9
ff1f61bf
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
28 addition
and
65 deletion
+28
-65
server/config/gorm.go
server/config/gorm.go
+2
-2
server/initialize/gorm.go
server/initialize/gorm.go
+5
-11
server/initialize/internal/logger.go
server/initialize/internal/logger.go
+21
-51
server/initialize/timer.go
server/initialize/timer.go
+0
-1
未找到文件。
server/config/gorm.go
浏览文件 @
778e5bb9
...
...
@@ -8,8 +8,8 @@ type Mysql struct {
Password
string
`mapstructure:"password" json:"password" yaml:"password"`
// 数据库密码
MaxIdleConns
int
`mapstructure:"max-idle-conns" json:"maxIdleConns" yaml:"max-idle-conns"`
// 空闲中的最大连接数
MaxOpenConns
int
`mapstructure:"max-open-conns" json:"maxOpenConns" yaml:"max-open-conns"`
// 打开到数据库的最大连接数
LogMode
bool
`mapstructure:"log-mode" json:"logMode" yaml:"log-mode"`
// 是否开启Gorm全局日志
LogZap
string
`mapstructure:"log-zap" json:"logZap" yaml:"log-zap"`
LogMode
string
`mapstructure:"log-mode" json:"logMode" yaml:"log-mode"`
// 是否开启Gorm全局日志
LogZap
bool
`mapstructure:"log-zap" json:"logZap" yaml:"log-zap"`
// 是否通过zap写入日志文件
}
func
(
m
*
Mysql
)
Dsn
()
string
{
...
...
server/initialize/gorm.go
浏览文件 @
778e5bb9
...
...
@@ -78,7 +78,7 @@ func GormMysql() *gorm.DB {
DontSupportRenameColumn
:
true
,
// 用 `change` 重命名列,MySQL 8 之前的数据库和 MariaDB 不支持重命名列
SkipInitializeWithVersion
:
false
,
// 根据版本自动配置
}
if
db
,
err
:=
gorm
.
Open
(
mysql
.
New
(
mysqlConfig
),
gormConfig
(
m
.
LogMode
));
err
!=
nil
{
if
db
,
err
:=
gorm
.
Open
(
mysql
.
New
(
mysqlConfig
),
gormConfig
());
err
!=
nil
{
//global.GVA_LOG.Error("MySQL启动异常", zap.Any("err", err))
//os.Exit(0)
//return nil
...
...
@@ -97,9 +97,9 @@ func GormMysql() *gorm.DB {
//@param: mod bool
//@return: *gorm.Config
func
gormConfig
(
mod
bool
)
*
gorm
.
Config
{
var
config
=
&
gorm
.
Config
{
DisableForeignKeyConstraintWhenMigrating
:
true
}
switch
global
.
GVA_CONFIG
.
Mysql
.
Log
Zap
{
func
gormConfig
()
*
gorm
.
Config
{
config
:
=
&
gorm
.
Config
{
DisableForeignKeyConstraintWhenMigrating
:
true
}
switch
global
.
GVA_CONFIG
.
Mysql
.
Log
Mode
{
case
"silent"
,
"Silent"
:
config
.
Logger
=
internal
.
Default
.
LogMode
(
logger
.
Silent
)
case
"error"
,
"Error"
:
...
...
@@ -108,14 +108,8 @@ func gormConfig(mod bool) *gorm.Config {
config
.
Logger
=
internal
.
Default
.
LogMode
(
logger
.
Warn
)
case
"info"
,
"Info"
:
config
.
Logger
=
internal
.
Default
.
LogMode
(
logger
.
Info
)
case
"zap"
,
"Zap"
:
config
.
Logger
=
internal
.
Default
.
LogMode
(
logger
.
Info
)
default
:
if
mod
{
config
.
Logger
=
internal
.
Default
.
LogMode
(
logger
.
Info
)
break
}
config
.
Logger
=
internal
.
Default
.
LogMode
(
logger
.
Silent
)
config
.
Logger
=
internal
.
Default
.
LogMode
(
logger
.
Info
)
}
return
config
}
server/initialize/internal/logger.go
浏览文件 @
778e5bb9
...
...
@@ -4,7 +4,6 @@ import (
"context"
"fmt"
"gin-vue-admin/global"
"go.uber.org/zap"
"gorm.io/gorm/logger"
"gorm.io/gorm/utils"
"io/ioutil"
...
...
@@ -13,11 +12,6 @@ import (
"time"
)
// writer log writer interface
type
writer
interface
{
Printf
(
string
,
...
interface
{})
}
type
config
struct
{
SlowThreshold
time
.
Duration
Colorful
bool
...
...
@@ -34,27 +28,27 @@ var (
Recorder
=
traceRecorder
{
Interface
:
Default
,
BeginAt
:
time
.
Now
()}
)
func
New
(
writer
w
riter
,
config
config
)
logger
.
Interface
{
func
New
(
writer
logger
.
W
riter
,
config
config
)
logger
.
Interface
{
var
(
infoStr
=
"%s
\n
[info] "
warnStr
=
"%s
\n
[warn] "
errStr
=
"%s
\n
[error] "
traceStr
=
"%s
\n
[%.3fms] [rows:%v] %s"
traceWarnStr
=
"%s %s
\n
[%.3fms] [rows:%v] %s"
traceErrStr
=
"%s %s
\n
[%.3fms] [rows:%v] %s"
traceStr
=
"%s
\n
[%.3fms] [rows:%v] %s
\n
"
traceWarnStr
=
"%s %s
\n
[%.3fms] [rows:%v] %s
\n
"
traceErrStr
=
"%s %s
\n
[%.3fms] [rows:%v] %s
\n
"
)
if
config
.
Colorful
{
infoStr
=
logger
.
Green
+
"%s
\n
"
+
logger
.
Reset
+
logger
.
Green
+
"[info] "
+
logger
.
Reset
warnStr
=
logger
.
BlueBold
+
"%s
\n
"
+
logger
.
Reset
+
logger
.
Magenta
+
"[warn] "
+
logger
.
Reset
errStr
=
logger
.
Magenta
+
"%s
\n
"
+
logger
.
Reset
+
logger
.
Red
+
"[error] "
+
logger
.
Reset
traceStr
=
logger
.
Green
+
"%s
\n
"
+
logger
.
Reset
+
logger
.
Yellow
+
"[%.3fms] "
+
logger
.
BlueBold
+
"[rows:%v]"
+
logger
.
Reset
+
" %s"
traceWarnStr
=
logger
.
Green
+
"%s "
+
logger
.
Yellow
+
"%s
\n
"
+
logger
.
Reset
+
logger
.
RedBold
+
"[%.3fms] "
+
logger
.
Yellow
+
"[rows:%v]"
+
logger
.
Magenta
+
" %s"
+
logger
.
Reset
traceErrStr
=
logger
.
RedBold
+
"%s "
+
logger
.
MagentaBold
+
"%s
\n
"
+
logger
.
Reset
+
logger
.
Yellow
+
"[%.3fms] "
+
logger
.
BlueBold
+
"[rows:%v]"
+
logger
.
Reset
+
" %s"
traceStr
=
logger
.
Green
+
"%s
\n
"
+
logger
.
Reset
+
logger
.
Yellow
+
"[%.3fms] "
+
logger
.
BlueBold
+
"[rows:%v]"
+
logger
.
Reset
+
" %s
\n
"
traceWarnStr
=
logger
.
Green
+
"%s "
+
logger
.
Yellow
+
"%s
\n
"
+
logger
.
Reset
+
logger
.
RedBold
+
"[%.3fms] "
+
logger
.
Yellow
+
"[rows:%v]"
+
logger
.
Magenta
+
" %s
\n
"
+
logger
.
Reset
traceErrStr
=
logger
.
RedBold
+
"%s "
+
logger
.
MagentaBold
+
"%s
\n
"
+
logger
.
Reset
+
logger
.
Yellow
+
"[%.3fms] "
+
logger
.
BlueBold
+
"[rows:%v]"
+
logger
.
Reset
+
" %s
\n
"
}
return
&
customL
ogger
{
w
riter
:
writer
,
return
&
_l
ogger
{
W
riter
:
writer
,
config
:
config
,
infoStr
:
infoStr
,
warnStr
:
warnStr
,
...
...
@@ -65,43 +59,43 @@ func New(writer writer, config config) logger.Interface {
}
}
type
customLogger
struct
{
writer
type
_logger
struct
{
config
logger
.
Writer
infoStr
,
warnStr
,
errStr
string
traceStr
,
traceErrStr
,
traceWarnStr
string
}
// LogMode log mode
func
(
c
*
customL
ogger
)
LogMode
(
level
logger
.
LogLevel
)
logger
.
Interface
{
func
(
c
*
_l
ogger
)
LogMode
(
level
logger
.
LogLevel
)
logger
.
Interface
{
newLogger
:=
*
c
newLogger
.
LogLevel
=
level
return
&
newLogger
}
// Info print info
func
(
c
*
customL
ogger
)
Info
(
ctx
context
.
Context
,
message
string
,
data
...
interface
{})
{
func
(
c
*
_l
ogger
)
Info
(
ctx
context
.
Context
,
message
string
,
data
...
interface
{})
{
if
c
.
LogLevel
>=
logger
.
Info
{
c
.
Printf
(
c
.
infoStr
+
message
,
append
([]
interface
{}{
utils
.
FileWithLineNum
()},
data
...
)
...
)
}
}
// Warn print warn messages
func
(
c
*
customL
ogger
)
Warn
(
ctx
context
.
Context
,
message
string
,
data
...
interface
{})
{
func
(
c
*
_l
ogger
)
Warn
(
ctx
context
.
Context
,
message
string
,
data
...
interface
{})
{
if
c
.
LogLevel
>=
logger
.
Warn
{
c
.
Printf
(
c
.
warnStr
+
message
,
append
([]
interface
{}{
utils
.
FileWithLineNum
()},
data
...
)
...
)
}
}
// Error print error messages
func
(
c
*
customL
ogger
)
Error
(
ctx
context
.
Context
,
message
string
,
data
...
interface
{})
{
func
(
c
*
_l
ogger
)
Error
(
ctx
context
.
Context
,
message
string
,
data
...
interface
{})
{
if
c
.
LogLevel
>=
logger
.
Error
{
c
.
Printf
(
c
.
errStr
+
message
,
append
([]
interface
{}{
utils
.
FileWithLineNum
()},
data
...
)
...
)
}
}
// Trace print sql message
func
(
c
*
customL
ogger
)
Trace
(
ctx
context
.
Context
,
begin
time
.
Time
,
fc
func
()
(
string
,
int64
),
err
error
)
{
func
(
c
*
_l
ogger
)
Trace
(
ctx
context
.
Context
,
begin
time
.
Time
,
fc
func
()
(
string
,
int64
),
err
error
)
{
if
c
.
LogLevel
>
0
{
elapsed
:=
time
.
Since
(
begin
)
switch
{
...
...
@@ -131,35 +125,11 @@ func (c *customLogger) Trace(ctx context.Context, begin time.Time, fc func() (st
}
}
func
(
c
*
customLogger
)
Printf
(
message
string
,
data
...
interface
{})
{
if
global
.
GVA_CONFIG
.
Mysql
.
LogZap
!=
""
{
switch
len
(
data
)
{
case
0
:
global
.
GVA_LOG
.
Info
(
message
)
case
1
:
global
.
GVA_LOG
.
Info
(
"gorm"
,
zap
.
Any
(
"src"
,
data
[
0
]))
case
2
:
global
.
GVA_LOG
.
Info
(
"gorm"
,
zap
.
Any
(
"src"
,
data
[
0
]),
zap
.
Any
(
"duration"
,
data
[
1
]))
case
3
:
global
.
GVA_LOG
.
Info
(
"gorm"
,
zap
.
Any
(
"src"
,
data
[
0
]),
zap
.
Any
(
"duration"
,
data
[
1
]),
zap
.
Any
(
"rows"
,
data
[
2
]))
case
4
:
global
.
GVA_LOG
.
Info
(
"gorm"
,
zap
.
Any
(
"src"
,
data
[
0
]),
zap
.
Any
(
"duration"
,
data
[
1
]),
zap
.
Any
(
"rows"
,
data
[
2
]),
zap
.
Any
(
"sql"
,
data
[
3
]))
}
return
}
switch
len
(
data
)
{
case
0
:
c
.
writer
.
Printf
(
message
,
""
)
case
1
:
c
.
writer
.
Printf
(
message
,
data
[
0
])
case
2
:
c
.
writer
.
Printf
(
message
,
data
[
0
],
data
[
1
])
case
3
:
c
.
writer
.
Printf
(
message
,
data
[
0
],
data
[
1
],
data
[
2
])
case
4
:
c
.
writer
.
Printf
(
message
,
data
[
0
],
data
[
1
],
data
[
2
],
data
[
3
])
case
5
:
c
.
writer
.
Printf
(
message
,
data
[
0
],
data
[
1
],
data
[
2
],
data
[
3
],
data
[
4
])
func
(
c
*
_logger
)
Printf
(
message
string
,
data
...
interface
{})
{
if
global
.
GVA_CONFIG
.
Mysql
.
LogZap
{
global
.
GVA_LOG
.
Info
(
fmt
.
Sprintf
(
message
,
data
...
))
}
else
{
c
.
Writer
.
Printf
(
message
,
data
...
)
}
}
...
...
server/initialize/timer.go
浏览文件 @
778e5bb9
...
...
@@ -10,7 +10,6 @@ import (
func
Timer
()
{
if
global
.
GVA_CONFIG
.
Timer
.
Start
{
for
_
,
detail
:=
range
global
.
GVA_CONFIG
.
Timer
.
Detail
{
fmt
.
Println
(
detail
)
go
func
(
detail
config
.
Detail
)
{
global
.
GVA_Timer
.
AddTaskByFunc
(
"ClearDB"
,
global
.
GVA_CONFIG
.
Timer
.
Spec
,
func
()
{
err
:=
utils
.
ClearTable
(
global
.
GVA_DB
,
detail
.
TableName
,
detail
.
CompareField
,
detail
.
Interval
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录