Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1dad4b79f5 | ||
|
|
42331cde57 | ||
|
|
36fd04cac9 | ||
|
|
956cf69a82 | ||
|
|
61a115852e | ||
|
|
bb5dd2583d |
@@ -5,21 +5,21 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestNew(t *testing.T) {
|
func TestNew(t *testing.T) {
|
||||||
//mdb, err := New(WithMysql("127.0.0.1", 3306, "root", "MyPassw0rd", "mydb"))
|
mdb, err := New(WithMysql("127.0.0.1", 2881, "yizhi@test", "yizhi", "mie"))
|
||||||
//if err != nil {
|
if err != nil {
|
||||||
// t.Fatal(err)
|
t.Fatal(err)
|
||||||
//}
|
}
|
||||||
//
|
|
||||||
//type User struct {
|
type User struct {
|
||||||
// Id uint64 `gorm:"primaryKey"`
|
Id uint64 `gorm:"primaryKey"`
|
||||||
// Username string `gorm:"unique"`
|
Username string `gorm:"unique"`
|
||||||
//}
|
}
|
||||||
//
|
|
||||||
//if err = mdb.Session(t.Context()).AutoMigrate(&User{}); err != nil {
|
if err = mdb.Session(t.Context()).AutoMigrate(&User{}); err != nil {
|
||||||
// t.Fatal(err)
|
t.Fatal(err)
|
||||||
//}
|
}
|
||||||
//
|
|
||||||
//if err = mdb.Session(t.Context()).Create(&User{Username: "zyp"}).Error; err != nil {
|
if err = mdb.Session(t.Context()).Create(&User{Username: "zyp"}).Error; err != nil {
|
||||||
// t.Fatal(err)
|
t.Fatal(err)
|
||||||
//}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,8 @@ package db
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
|
"gitea.loveuer.com/yizhisec/packages/logger"
|
||||||
)
|
)
|
||||||
|
|
||||||
type config struct {
|
type config struct {
|
||||||
@@ -25,6 +27,7 @@ func WithCtx(ctx context.Context) OptionFn {
|
|||||||
func WithMysql(host string, port int, user string, password string, database string) OptionFn {
|
func WithMysql(host string, port int, user string, password string, database string) OptionFn {
|
||||||
return func(c *config) {
|
return func(c *config) {
|
||||||
dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local", user, password, host, port, database)
|
dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local", user, password, host, port, database)
|
||||||
|
logger.Debug("db init with mysql, dsn: %s", dsn)
|
||||||
c.mysql = &dsn
|
c.mysql = &dsn
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -32,6 +35,7 @@ func WithMysql(host string, port int, user string, password string, database str
|
|||||||
func WithPg(host string, port int, user string, password string, database string) OptionFn {
|
func WithPg(host string, port int, user string, password string, database string) OptionFn {
|
||||||
return func(c *config) {
|
return func(c *config) {
|
||||||
dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%d sslmode=disable TimeZone=Asia/Shanghai", host, user, password, database, port)
|
dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%d sslmode=disable TimeZone=Asia/Shanghai", host, user, password, database, port)
|
||||||
|
logger.Debug("db init with pg, dsn: %s", dsn)
|
||||||
c.pg = &dsn
|
c.pg = &dsn
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -39,6 +43,7 @@ func WithPg(host string, port int, user string, password string, database string
|
|||||||
func WithSqlite(path string) OptionFn {
|
func WithSqlite(path string) OptionFn {
|
||||||
return func(c *config) {
|
return func(c *config) {
|
||||||
if path != "" {
|
if path != "" {
|
||||||
|
logger.Debug("db init with sqlite, path: %s", path)
|
||||||
c.sqlite = &path
|
c.sqlite = &path
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,17 +2,20 @@ package logger
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"gitea.loveuer.com/yizhisec/packages/opt"
|
|
||||||
uuid2 "github.com/google/uuid"
|
uuid2 "github.com/google/uuid"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type _traceId struct{}
|
||||||
|
|
||||||
|
var TraceId = _traceId{}
|
||||||
|
|
||||||
func traceId(ctx context.Context) string {
|
func traceId(ctx context.Context) string {
|
||||||
if ctx == nil {
|
if ctx == nil {
|
||||||
uuid, _ := uuid2.NewV7()
|
uuid, _ := uuid2.NewV7()
|
||||||
return uuid.String()
|
return uuid.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
if id, _ := ctx.Value(opt.TraceKey).(string); id != "" {
|
if id, _ := ctx.Value(TraceId).(string); id != "" {
|
||||||
return id
|
return id
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -21,30 +24,30 @@ func traceId(ctx context.Context) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func DebugCtx(ctx context.Context, msg string, data ...any) {
|
func DebugCtx(ctx context.Context, msg string, data ...any) {
|
||||||
msg = "[" + traceId(ctx) + "] " + msg
|
msg = traceId(ctx) + " | " + msg
|
||||||
DefaultLogger.Debug(msg, data...)
|
DefaultLogger.Debug(msg, data...)
|
||||||
}
|
}
|
||||||
func InfoCtx(ctx context.Context, msg string, data ...any) {
|
func InfoCtx(ctx context.Context, msg string, data ...any) {
|
||||||
msg = "[" + traceId(ctx) + "] " + msg
|
msg = traceId(ctx) + " | " + msg
|
||||||
DefaultLogger.Info(msg, data...)
|
DefaultLogger.Info(msg, data...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func WarnCtx(ctx context.Context, msg string, data ...any) {
|
func WarnCtx(ctx context.Context, msg string, data ...any) {
|
||||||
msg = "[" + traceId(ctx) + "] " + msg
|
msg = traceId(ctx) + " | " + msg
|
||||||
DefaultLogger.Warn(msg, data...)
|
DefaultLogger.Warn(msg, data...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func ErrorCtx(ctx context.Context, msg string, data ...any) {
|
func ErrorCtx(ctx context.Context, msg string, data ...any) {
|
||||||
msg = "[" + traceId(ctx) + "] " + msg
|
msg = traceId(ctx) + " | " + msg
|
||||||
DefaultLogger.Error(msg, data...)
|
DefaultLogger.Error(msg, data...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func PanicCtx(ctx context.Context, msg string, data ...any) {
|
func PanicCtx(ctx context.Context, msg string, data ...any) {
|
||||||
msg = "[" + traceId(ctx) + "] " + msg
|
msg = traceId(ctx) + " | " + msg
|
||||||
DefaultLogger.Panic(msg, data...)
|
DefaultLogger.Panic(msg, data...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func FatalCtx(ctx context.Context, msg string, data ...any) {
|
func FatalCtx(ctx context.Context, msg string, data ...any) {
|
||||||
msg = "[" + traceId(ctx) + "] " + msg
|
msg = traceId(ctx) + " | " + msg
|
||||||
DefaultLogger.Fatal(msg, data...)
|
DefaultLogger.Fatal(msg, data...)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ package logger
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"gitea.loveuer.com/yizhisec/packages/opt"
|
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -11,7 +10,7 @@ func TestCtxLog(t *testing.T) {
|
|||||||
InfoCtx(nil, "hello %s", "world")
|
InfoCtx(nil, "hello %s", "world")
|
||||||
WarnCtx(context.Background(), "hello %s", "world")
|
WarnCtx(context.Background(), "hello %s", "world")
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
ctx = context.WithValue(ctx, opt.TraceKey, "value")
|
ctx = context.WithValue(ctx, TraceId, "value")
|
||||||
SetLogLevel(LogLevelDebug)
|
SetLogLevel(LogLevelDebug)
|
||||||
DebugCtx(ctx, "hello %s", "world")
|
DebugCtx(ctx, "hello %s", "world")
|
||||||
ErrorCtx(ctx, "hello %s", "world")
|
ErrorCtx(ctx, "hello %s", "world")
|
||||||
|
|||||||
@@ -2,17 +2,21 @@ package logger
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"gitea.loveuer.com/yizhisec/packages/logger"
|
|
||||||
"gitea.loveuer.com/yizhisec/packages/opt"
|
|
||||||
"gitea.loveuer.com/yizhisec/packages/tool"
|
|
||||||
"github.com/gin-gonic/gin"
|
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"gitea.loveuer.com/yizhisec/packages/opt"
|
||||||
|
"gitea.loveuer.com/yizhisec/packages/tool"
|
||||||
|
"github.com/fatih/color"
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
defaultTrace = " middleware-logger"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
defaultTrace = "middleware-logger"
|
|
||||||
stringBuilderPool = sync.Pool{
|
stringBuilderPool = sync.Pool{
|
||||||
New: func() interface{} {
|
New: func() interface{} {
|
||||||
return &strings.Builder{}
|
return &strings.Builder{}
|
||||||
@@ -36,39 +40,42 @@ func New() gin.HandlerFunc {
|
|||||||
|
|
||||||
c.Next()
|
c.Next()
|
||||||
|
|
||||||
status := c.Writer.Status()
|
var (
|
||||||
|
status string
|
||||||
|
)
|
||||||
|
|
||||||
|
_status := c.Writer.Status()
|
||||||
duration := time.Since(start)
|
duration := time.Since(start)
|
||||||
|
|
||||||
var logFn func(msg string, data ...any)
|
|
||||||
switch {
|
switch {
|
||||||
case status >= 500:
|
case _status >= 500:
|
||||||
logFn = logger.Error
|
status = color.RedString("%3d", _status)
|
||||||
case status >= 400:
|
case _status >= 400:
|
||||||
logFn = logger.Warn
|
status = color.YellowString("%3d", _status)
|
||||||
default:
|
default:
|
||||||
logFn = logger.Info
|
status = color.GreenString("%3d", _status)
|
||||||
}
|
}
|
||||||
|
|
||||||
if logFn != nil {
|
b := stringBuilderPool.Get().(*strings.Builder)
|
||||||
b := stringBuilderPool.Get().(*strings.Builder)
|
b.Reset()
|
||||||
b.Reset()
|
|
||||||
|
|
||||||
b.WriteString("[")
|
b.WriteString("GIN | ")
|
||||||
b.WriteString(fmt.Sprintf("%36s", trace))
|
b.WriteString(start.Format("2006-01-02T15:04:05"))
|
||||||
b.WriteString("] | ")
|
b.WriteString(" | ")
|
||||||
b.WriteString(fmt.Sprintf("%3d", status))
|
b.WriteString(trace)
|
||||||
b.WriteString(" | ")
|
b.WriteString(" | ")
|
||||||
b.WriteString(fmt.Sprintf("%15s", ip))
|
b.WriteString(status)
|
||||||
b.WriteString(" | ")
|
b.WriteString(" | ")
|
||||||
b.WriteString(tool.HumanDuration(duration.Nanoseconds()))
|
b.WriteString(fmt.Sprintf("%15s", ip))
|
||||||
b.WriteString(" | ")
|
b.WriteString(" | ")
|
||||||
b.WriteString(c.Request.Method)
|
b.WriteString(tool.HumanDuration(duration.Nanoseconds()))
|
||||||
b.WriteString(" | ")
|
b.WriteString(" | ")
|
||||||
b.WriteString(c.Request.RequestURI)
|
b.WriteString(c.Request.Method)
|
||||||
|
b.WriteString(" | ")
|
||||||
|
b.WriteString(c.Request.RequestURI)
|
||||||
|
|
||||||
logFn(b.String())
|
fmt.Println(b.String())
|
||||||
|
|
||||||
stringBuilderPool.Put(b)
|
stringBuilderPool.Put(b)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,10 +2,12 @@ package logger
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"github.com/gin-gonic/gin"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"gitea.loveuer.com/yizhisec/packages/logger"
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestLogger(t *testing.T) {
|
func TestLogger(t *testing.T) {
|
||||||
@@ -20,6 +22,7 @@ func TestLogger(t *testing.T) {
|
|||||||
c.JSON(400, gin.H{"hello": "world"})
|
c.JSON(400, gin.H{"hello": "world"})
|
||||||
})
|
})
|
||||||
app.GET("/500", func(c *gin.Context) {
|
app.GET("/500", func(c *gin.Context) {
|
||||||
|
logger.ErrorCtx(c.Request.Context(), "oops")
|
||||||
c.JSON(500, gin.H{"hello": "world"})
|
c.JSON(500, gin.H{"hello": "world"})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
22
resp/resp.go
22
resp/resp.go
@@ -30,6 +30,18 @@ func R200(c *gin.Context, data any, msgs ...string) {
|
|||||||
c.AbortWithStatusJSON(200, r)
|
c.AbortWithStatusJSON(200, r)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func RC(c *gin.Context, status int, args ...any) {
|
||||||
|
code := 1
|
||||||
|
if status != 200 {
|
||||||
|
code = -1
|
||||||
|
}
|
||||||
|
|
||||||
|
_r(c, &res{
|
||||||
|
Status: status,
|
||||||
|
Code: code,
|
||||||
|
}, args...)
|
||||||
|
}
|
||||||
|
|
||||||
func RE(c *gin.Context, err error) {
|
func RE(c *gin.Context, err error) {
|
||||||
var re *Error
|
var re *Error
|
||||||
|
|
||||||
@@ -68,11 +80,7 @@ H4:
|
|||||||
r.Code = code
|
r.Code = code
|
||||||
}
|
}
|
||||||
H3:
|
H3:
|
||||||
if es, ok := args[2].(error); ok {
|
r.Err = args[2]
|
||||||
r.Err = es.Error()
|
|
||||||
} else {
|
|
||||||
r.Err = args[2]
|
|
||||||
}
|
|
||||||
H2:
|
H2:
|
||||||
r.Data = args[1]
|
r.Data = args[1]
|
||||||
H1:
|
H1:
|
||||||
@@ -86,6 +94,10 @@ END:
|
|||||||
r.Msg = Msg(r.Status)
|
r.Msg = Msg(r.Status)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ce, ok := r.Err.(error); ok {
|
||||||
|
r.Err = ce.Error()
|
||||||
|
}
|
||||||
|
|
||||||
c.AbortWithStatusJSON(r.Status, r)
|
c.AbortWithStatusJSON(r.Status, r)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user