Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(database): 为数据库执行输出 DEBUG 日志 #1080

Merged
merged 16 commits into from
Oct 20, 2024
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dice/model/db_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

func _SQLiteDBInit(path string, useWAL bool) (*sqlx.DB, error) {
db, err := sqlx.Open("sqlite", path)
db, err := sqlx.Open(zapDriverName, path)
if err != nil {
panic(err)
}
Expand Down
2 changes: 1 addition & 1 deletion dice/model/db_init_cgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

func _SQLiteDBInit(path string, useWAL bool) (*sqlx.DB, error) {
db, err := sqlx.Open("sqlite3", path)
db, err := sqlx.Open(zapDriverName, path)
if err != nil {
panic(err)
}
Expand Down
83 changes: 83 additions & 0 deletions dice/model/sqlhook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
//go:build !cgo
// +build !cgo

package model

import (
"context"
"database/sql"
"fmt"
"time"

"github.com/glebarez/go-sqlite"
"github.com/qustavo/sqlhooks/v2"

log "sealdice-core/utils/kratos"
)

// 覆盖驱动名 sqlite3 会导致 panic, 因此需要创建新的驱动.
//
// database/sql/sql.go:51
const zapDriverName = "sqlite3-log"

func InitZapHook(log *log.Helper) {
hook := &zapHook{Helper: log, IsPrintSQLDuration: true}
sql.Register(zapDriverName, sqlhooks.Wrap(new(sqlite.Driver), hook))
}

// make sure zapHook implement all sqlhooks interface.
var _ interface {
sqlhooks.Hooks
sqlhooks.OnErrorer
} = (*zapHook)(nil)

// zapHook 使用 zap 记录 SQL 查询和参数
type zapHook struct {
*log.Helper

// 是否打印 SQL 耗时
IsPrintSQLDuration bool
}

// sqlDurationKey 是 context.valueCtx Key
type sqlDurationKey struct{}

func buildQueryArgsFields(query string, args ...interface{}) []interface{} {
if len(args) == 0 {
return []interface{}{"查询", query}
}
return []interface{}{"查询", query, "参数", args}
}

func (z *zapHook) Before(ctx context.Context, _ string, _ ...interface{}) (context.Context, error) {
if z == nil || z.Helper == nil {
return ctx, nil
}

if z.IsPrintSQLDuration {
ctx = context.WithValue(ctx, (*sqlDurationKey)(nil), time.Now())
}
return ctx, nil
}

func (z *zapHook) After(ctx context.Context, query string, args ...interface{}) (context.Context, error) {
if z == nil || z.Helper == nil {
return ctx, nil
}

var durationField string
if v, ok := ctx.Value((*sqlDurationKey)(nil)).(time.Time); ok {
durationField = fmt.Sprintf("%v", time.Now().Sub(v))
}

z.Debugf("SQL 执行后: %v 耗时: %v 秒", buildQueryArgsFields(query, args...), durationField)
return ctx, nil
}

func (z *zapHook) OnError(_ context.Context, err error, query string, args ...interface{}) error {
if z == nil || z.Helper == nil {
return nil
}
z.Errorf("SQL 执行出错日志 %v 豹错为: %v", buildQueryArgsFields(query, args...), err)
JustAnotherID marked this conversation as resolved.
Show resolved Hide resolved
return nil
}
83 changes: 83 additions & 0 deletions dice/model/sqlhook_cgo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
//go:build cgo
// +build cgo

package model

import (
"context"
"database/sql"
"fmt"
"time"

"github.com/mattn/go-sqlite3"
"github.com/qustavo/sqlhooks/v2"

log "sealdice-core/utils/kratos"
)

// 覆盖驱动名 sqlite3 会导致 panic, 因此需要创建新的驱动.
//
// database/sql/sql.go:51
const zapDriverName = "sqlite3-log"

func InitZapHook(log *log.Helper) {
hook := &zapHook{Helper: log, IsPrintSQLDuration: true}
sql.Register(zapDriverName, sqlhooks.Wrap(new(sqlite3.SQLiteDriver), hook))
}

// make sure zapHook implement all sqlhooks interface.
var _ interface {
sqlhooks.Hooks
sqlhooks.OnErrorer
} = (*zapHook)(nil)

// zapHook 使用 zap 记录 SQL 查询和参数
type zapHook struct {
*log.Helper

// 是否打印 SQL 耗时
IsPrintSQLDuration bool
}

// sqlDurationKey 是 context.valueCtx Key
type sqlDurationKey struct{}

func buildQueryArgsFields(query string, args ...interface{}) []interface{} {
if len(args) == 0 {
return []interface{}{"查询", query}
}
return []interface{}{"查询", query, "参数", args}
}

func (z *zapHook) Before(ctx context.Context, _ string, _ ...interface{}) (context.Context, error) {
if z == nil || z.Helper == nil {
return ctx, nil
}

if z.IsPrintSQLDuration {
ctx = context.WithValue(ctx, (*sqlDurationKey)(nil), time.Now())
}
return ctx, nil
}

func (z *zapHook) After(ctx context.Context, query string, args ...interface{}) (context.Context, error) {
if z == nil || z.Helper == nil {
return ctx, nil
}

var durationField string
if v, ok := ctx.Value((*sqlDurationKey)(nil)).(time.Time); ok {
durationField = fmt.Sprintf("%v", time.Now().Sub(v))
PaienNate marked this conversation as resolved.
Show resolved Hide resolved
}

z.Debugf("SQL 执行后: %v 耗时: %v 秒", buildQueryArgsFields(query, args...), durationField)
return ctx, nil
}

func (z *zapHook) OnError(_ context.Context, err error, query string, args ...interface{}) error {
if z == nil || z.Helper == nil {
return nil
}
z.Errorf("SQL 执行出错日志 %v 豹错为: %v", buildQueryArgsFields(query, args...), err)
JustAnotherID marked this conversation as resolved.
Show resolved Hide resolved
return nil
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ require (
github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d // indirect
github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c // indirect
github.com/pdfcpu/pdfcpu v0.8.1 // indirect
github.com/qustavo/sqlhooks/v2 v2.1.0 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
github.com/richardlehane/mscfb v1.0.4 // indirect
github.com/richardlehane/msoleps v1.0.3 // indirect
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ github.com/go-resty/resty/v2 v2.11.0 h1:i7jMfNOJYMp69lq7qozJP+bjgzfAzeOhuGlyDrqx
github.com/go-resty/resty/v2 v2.11.0/go.mod h1:iiP/OpA0CkcL3IGt1O0+/SIItFUbkkyw5BGXiVdTu+A=
github.com/go-sourcemap/sourcemap v2.1.3+incompatible h1:W1iEw64niKVGogNgBN3ePyLFfuisuzeidWPMPWmECqU=
github.com/go-sourcemap/sourcemap v2.1.3+incompatible/go.mod h1:F8jJfvm2KbVjc5NqelyYJmf/v5J0dwNLS2mL4sNA1Jg=
github.com/go-sql-driver/mysql v1.4.1/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
github.com/go-sql-driver/mysql v1.8.1 h1:LedoTUt/eveggdHS9qUFC1EFSa8bU2+1pZjSRpvNJ1Y=
github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg=
github.com/go-stack/stack v1.8.0 h1:5SgMzNM5HxrEjV0ww2lTmX6E2Izsfxas4+YHWRs3Lsk=
Expand Down Expand Up @@ -238,6 +239,7 @@ github.com/labstack/echo/v4 v4.12.0 h1:IKpw49IMryVB2p1a4dzwlhP1O2Tf2E0Ir/450lH+k
github.com/labstack/echo/v4 v4.12.0/go.mod h1:UP9Cr2DJXbOK3Kr9ONYzNowSh7HP0aG0ShAyycHSJvM=
github.com/labstack/gommon v0.4.2 h1:F8qTUNXgG1+6WQmqoUWnz8WiEU60mXVVw0P4ht1WRA0=
github.com/labstack/gommon v0.4.2/go.mod h1:QlUFxVM+SNXhDL/Z7YhocGIBYOiwB0mXm1+1bAPHPyU=
github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/lonelyevil/kook v0.0.29/go.mod h1:WjHC7AmbmNjInT/U/etBVOmAw7T6EqdCwApceRGs1sk=
Expand All @@ -256,6 +258,7 @@ github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWE
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6TULQc=
github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mattn/go-sqlite3 v1.10.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
github.com/mattn/go-sqlite3 v1.14.23 h1:gbShiuAP1W5j9UOksQ06aiiqPMxYecovVGwmTxWtuw0=
github.com/mattn/go-sqlite3 v1.14.23/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
Expand Down Expand Up @@ -294,6 +297,7 @@ github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1y
github.com/onsi/gomega v1.16.0/go.mod h1:HnhC7FXeEQY45zxNK3PPoIUhzk/80Xly9PcubAlGdZY=
github.com/open-dingtalk/dingtalk-stream-sdk-go v0.9.0 h1:DL64ORGMk6AUB8q5LbRp8KRFn4oHhdrSepBmbMrtmNo=
github.com/open-dingtalk/dingtalk-stream-sdk-go v0.9.0/go.mod h1:ln3IqPYYocZbYvl9TAOrG/cxGR9xcn4pnZRLdCTEGEU=
github.com/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o=
github.com/otiai10/copy v1.14.0 h1:dCI/t1iTdYGtkvCuBG2BgR6KZa83PTclw4U5n2wAllU=
github.com/otiai10/copy v1.14.0/go.mod h1:ECfuL02W+/FkTWZWgQqXPWZgW9oeKCSQ5qVfSc4qc4w=
github.com/otiai10/mint v1.5.1 h1:XaPLeE+9vGbuyEHem1JNk3bYc7KKqyI/na0/mLd/Kks=
Expand All @@ -314,6 +318,8 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/qustavo/sqlhooks/v2 v2.1.0 h1:54yBemHnGHp/7xgT+pxwmIlMSDNYKx5JW5dfRAiCZi0=
github.com/qustavo/sqlhooks/v2 v2.1.0/go.mod h1:aMREyKo7fOKTwiLuWPsaHRXEmtqG4yREztO0idF83AU=
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
github.com/richardlehane/mscfb v1.0.4 h1:WULscsljNPConisD5hR0+OyZjwK46Pfyr6mPu5ZawpM=
Expand Down
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,8 @@ func main() {
paniclog.InitPanicLog()
// 3. 提示日志打印
log.Info("运行日志开始记录,海豹出现故障时可查看 data/main.log 与 data/panic.log 获取更多信息")
logger := log.NewCustomHelper("SQLX", false, nil)
model.InitZapHook(logger)
judge, osr := oschecker.OldVersionCheck()
// 预留收集信息的接口,如果有需要可以考虑从这里拿数据。不从这里做提示的原因是Windows和Linux的展示方式不同。
if judge {
Expand Down
Loading