package db
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
"leit.com/leit_seat_aps/common"
|
|
"leit.com/leit_seat_aps/glog"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
type Recordlog struct {
|
|
Servername string `bson:"servername" json:"servername"` //服务名称
|
|
Debuglevel string `bson:"debuglevel" json:"debuglevel"` //错误级别
|
|
TimeStamp string `bson:"timestamp" json:"timestamp"` //时间戳
|
|
Message string `bson:"message" json:"message"` //出错信息提示
|
|
Function string `bson:"function" json:"function"` //出错函数
|
|
File string `bson:"file" json:"file"` //出错文件
|
|
Location string `bson:"location" json:"location"` //出错位置
|
|
}
|
|
|
|
type SerchData struct {
|
|
Key string
|
|
Val string
|
|
Userid string
|
|
}
|
|
|
|
func (t *Recordlog) InsertRecord() (insertID primitive.ObjectID) {
|
|
//1.初始化链接
|
|
client := MgoDb()
|
|
//2.选择数据库,数据表
|
|
collect := client.Database("logDb").Collection("recordlog")
|
|
insertRest, err := collect.InsertOne(context.TODO(), t)
|
|
if err != nil {
|
|
glog.InfoExtln("mongodb err is", err)
|
|
return
|
|
}
|
|
insertID = insertRest.InsertedID.(primitive.ObjectID)
|
|
return insertID
|
|
}
|
|
|
|
func (t *Recordlog) FindLog(logred SerchData, skip int64, limit int64) ([]*Recordlog, int64) {
|
|
res := bson.M{}
|
|
// 创建需要过滤的条件
|
|
if logred.Key == "debuglevel" {
|
|
res = bson.M{"debuglevel": primitive.Regex{Pattern: logred.Val}}
|
|
}
|
|
if logred.Key == "message" {
|
|
res = bson.M{"message": primitive.Regex{Pattern: logred.Val}}
|
|
}
|
|
if logred.Key == "function" {
|
|
res = bson.M{"function": primitive.Regex{Pattern: logred.Val}}
|
|
}
|
|
if logred.Key == "file" {
|
|
res = bson.M{"file": primitive.Regex{Pattern: logred.Val}}
|
|
}
|
|
if logred.Key == "timestamp" {
|
|
times := strings.Split(logred.Val, "-")
|
|
stime := time.Unix(int64(common.ValueToInt(times[0], 0)), 0)
|
|
etime := time.Unix(int64(common.ValueToInt(times[1], 0)), 0)
|
|
stimestr := common.TimeFormat(stime, "yyyyMMddHHmmss")
|
|
etimestr := common.TimeFormat(etime, "yyyyMMddHHmmss")
|
|
res = bson.M{"timestamp": bson.M{"$lte": etimestr, "$gte": stimestr}}
|
|
}
|
|
if logred.Userid != "admin" {
|
|
res = bson.M{"operator": logred.Userid}
|
|
}
|
|
//1.初始化链接
|
|
client := MgoDb()
|
|
//2.选择数据库,数据表
|
|
collect := client.Database("logDb").Collection("recordlog")
|
|
|
|
//var skip int64 = 0//从那个开始
|
|
//var limit int64 = 2//炼制几个输出字段
|
|
cursor, err := collect.Find(context.TODO(), res, &options.FindOptions{
|
|
Skip: &skip,
|
|
Limit: &limit,
|
|
Sort: bson.D{{"timestamp", -1}},
|
|
})
|
|
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
return nil, 0
|
|
}
|
|
defer cursor.Close(context.TODO())
|
|
//创建需要反序列化成什么样子的结构体对象
|
|
records := make([]*Recordlog, 0)
|
|
for cursor.Next(context.TODO()) {
|
|
record := &Recordlog{}
|
|
//反序列化
|
|
err = cursor.Decode(record)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
return nil, 0
|
|
}
|
|
//追加
|
|
records = append(records, record)
|
|
}
|
|
// 获取数据总数
|
|
count, err := collect.CountDocuments(context.Background(), bson.D{})
|
|
if err != nil {
|
|
return records, 0
|
|
}
|
|
return records, count
|
|
}
|