提交 3c16f8a3 编写于 作者: Mr.奇淼('s avatar Mr.奇淼(

混入日志包

上级 8eae7f18
......@@ -8,6 +8,7 @@ import (
"main/router"
)
//初始化总路由
func InitRouter() *gin.Engine {
var Router = gin.Default()
Router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
......
package qmlog
// 日志初始化包 调用qmlog.QMLog.Info 记录日志 24小时切割 日志保存7天 可自行设置
import (
"fmt"
rotatelogs "github.com/lestrrat/go-file-rotatelogs"
"github.com/rifflock/lfshook"
"github.com/sirupsen/logrus"
"os"
"time"
)
var QMLog = logrus.New()
//禁止logrus的输出
func InitLog() {
src, err := os.OpenFile(os.DevNull, os.O_APPEND|os.O_WRONLY, os.ModeAppend)
if err != nil {
fmt.Println("err", err)
}
QMLog.Out = src
QMLog.SetLevel(logrus.DebugLevel)
apiLogPath := "api.log"
logWriter, err := rotatelogs.New(
apiLogPath+".%Y-%m-%d-%H-%M.log",
rotatelogs.WithLinkName(apiLogPath), // 生成软链,指向最新日志文件
rotatelogs.WithMaxAge(7*24*time.Hour), // 文件最大保存时间
rotatelogs.WithRotationTime(24*time.Hour), // 日志切割时间间隔
)
writeMap := lfshook.WriterMap{
logrus.InfoLevel: logWriter,
logrus.FatalLevel: logWriter,
}
lfHook := lfshook.NewHook(writeMap, &logrus.JSONFormatter{})
QMLog.AddHook(lfHook)
}
package mysql
package qmsql
import (
"github.com/jinzhu/gorm"
......@@ -9,6 +9,7 @@ import (
var DEFAULTDB *gorm.DB
//初始化数据库并产生数据库全局变量
func InitMysql(admin config.Admin) *gorm.DB {
if db, err := gorm.Open("mysql", admin.UserName+":"+admin.Password+"@("+admin.Path+")/"+admin.Dbname+"?"+admin.Config); err != nil {
log.Printf("DEFAULTDB数据库启动异常%S", err)
......
......@@ -5,6 +5,7 @@ import (
"main/model/dbModel"
)
//注册数据库表专用
func RegistTable(db *gorm.DB) {
db.AutoMigrate(dbModel.User{})
}
......@@ -3,17 +3,19 @@ package main
import (
"main/config"
"main/init/initRouter"
"main/init/mysql"
"main/init/qmlog"
"main/init/qmsql"
"main/init/registTable"
"net/http"
"time"
)
func main() {
registTable.RegistTable(mysql.InitMysql(config.Dbconfig.Admin))
defer mysql.DEFAULTDB.Close()
qmlog.InitLog()
registTable.RegistTable(qmsql.InitMysql(config.Dbconfig.Admin))
defer qmsql.DEFAULTDB.Close()
Router := initRouter.InitRouter()
//qmlog.QMLog.Info("服务器开启") // 日志测试代码
s := &http.Server{
Addr: ":8888",
Handler: Router,
......
package middleware
import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/lestrrat/go-file-rotatelogs"
"github.com/rifflock/lfshook"
"github.com/sirupsen/logrus"
"os"
"main/init/qmlog"
"time"
)
func Logger() gin.HandlerFunc {
logClient := logrus.New()
//禁止logrus的输出
src, err := os.OpenFile(os.DevNull, os.O_APPEND|os.O_WRONLY, os.ModeAppend)
if err != nil {
fmt.Println("err", err)
}
logClient.Out = src
logClient.SetLevel(logrus.DebugLevel)
apiLogPath := "api.log"
logWriter, err := rotatelogs.New(
apiLogPath+".%Y-%m-%d-%H-%M.log",
rotatelogs.WithLinkName(apiLogPath), // 生成软链,指向最新日志文件
rotatelogs.WithMaxAge(7*24*time.Hour), // 文件最大保存时间
rotatelogs.WithRotationTime(24*time.Hour), // 日志切割时间间隔
)
writeMap := lfshook.WriterMap{
logrus.InfoLevel: logWriter,
logrus.FatalLevel: logWriter,
}
lfHook := lfshook.NewHook(writeMap, &logrus.JSONFormatter{})
logClient.AddHook(lfHook)
return func(c *gin.Context) {
// 开始时间
start := time.Now()
......@@ -50,7 +24,7 @@ func Logger() gin.HandlerFunc {
buf := make([]byte, 1024)
n, _ := c.Request.Body.Read(buf)
requestParams := buf[0:n]
logClient.Infof("| %3d | %13v | %15s | %s %s |%s|",
qmlog.QMLog.Infof("| %3d | %13v | %15s | %s %s |%s|",
statusCode,
latency,
clientIP,
......
......@@ -4,7 +4,7 @@ import (
"github.com/jinzhu/gorm"
"github.com/pkg/errors"
uuid "github.com/satori/go.uuid"
"main/init/mysql"
"main/init/qmsql"
"main/tools"
)
......@@ -27,7 +27,7 @@ type User struct {
func (u *User) Regist() (err error, userInter *User) {
var user User
//判断用户名是否注册
findErr := mysql.DEFAULTDB.Where("user_name = ?", u.UserName).First(&user).Error
findErr := qmsql.DEFAULTDB.Where("user_name = ?", u.UserName).First(&user).Error
//err为nil表明读取到了 不能注册
if findErr == nil {
return errors.New("用户名已注册"), nil
......@@ -35,7 +35,7 @@ func (u *User) Regist() (err error, userInter *User) {
// 否则 附加uuid 密码md5简单加密 注册
u.PassWord = tools.MD5V(u.PassWord)
u.UUID = uuid.NewV4()
err = mysql.DEFAULTDB.Create(u).Error
err = qmsql.DEFAULTDB.Create(u).Error
}
return err, u
}
......@@ -45,13 +45,13 @@ func (u *User) ChangePassWord(newPassWord string) (err error, userInter *User) {
var user User
//后期修改jwt+password模式
u.PassWord = tools.MD5V(u.PassWord)
err = mysql.DEFAULTDB.Where("user_name = ? AND pass_word = ?", u.UserName, u.PassWord).First(&user).Update("pass_word", tools.MD5V(newPassWord)).Error
err = qmsql.DEFAULTDB.Where("user_name = ? AND pass_word = ?", u.UserName, u.PassWord).First(&user).Update("pass_word", tools.MD5V(newPassWord)).Error
return err, u
}
//用户更新接口
func (u *User) UpdataUser() (err error, userInter *User) {
err = mysql.DEFAULTDB.Create(u).Error
err = qmsql.DEFAULTDB.Create(u).Error
return err, u
}
......@@ -59,6 +59,6 @@ func (u *User) UpdataUser() (err error, userInter *User) {
func (u *User) Login() (err error, userInter *User) {
var user User
u.PassWord = tools.MD5V(u.PassWord)
err = mysql.DEFAULTDB.Where("user_name = ? AND pass_word = ?", u.UserName, u.PassWord).First(&user).Error
err = qmsql.DEFAULTDB.Where("user_name = ? AND pass_word = ?", u.UserName, u.PassWord).First(&user).Error
return err, &user
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册