2017-04-24 18:25:17 +08:00
|
|
|
|
package models
|
|
|
|
|
|
|
|
|
|
import (
|
2018-01-26 17:17:38 +08:00
|
|
|
|
"errors"
|
|
|
|
|
"github.com/astaxie/beego/orm"
|
2017-06-14 09:23:29 +08:00
|
|
|
|
"github.com/lifei6671/mindoc/conf"
|
2017-06-09 18:14:55 +08:00
|
|
|
|
"sync/atomic"
|
2018-01-26 17:17:38 +08:00
|
|
|
|
"time"
|
2017-04-24 18:25:17 +08:00
|
|
|
|
)
|
|
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
|
var loggerQueue = &logQueue{channel: make(chan *Logger, 100), isRuning: 0}
|
2017-06-09 18:14:55 +08:00
|
|
|
|
|
|
|
|
|
type logQueue struct {
|
2018-01-26 17:17:38 +08:00
|
|
|
|
channel chan *Logger
|
2017-06-09 18:14:55 +08:00
|
|
|
|
isRuning int32
|
|
|
|
|
}
|
2017-04-24 18:25:17 +08:00
|
|
|
|
|
|
|
|
|
// Logger struct .
|
|
|
|
|
type Logger struct {
|
2018-01-26 17:17:38 +08:00
|
|
|
|
LoggerId int64 `orm:"pk;auto;unique;column(log_id)" json:"log_id"`
|
|
|
|
|
MemberId int `orm:"column(member_id);type(int)" json:"member_id"`
|
2017-06-09 18:14:55 +08:00
|
|
|
|
// 日志类别:operate 操作日志/ system 系统日志/ exception 异常日志 / document 文档操作日志
|
2018-01-26 17:17:38 +08:00
|
|
|
|
Category string `orm:"column(category);size(255);default(operate)" json:"category"`
|
|
|
|
|
Content string `orm:"column(content);type(text)" json:"content"`
|
|
|
|
|
OriginalData string `orm:"column(original_data);type(text)" json:"original_data"`
|
|
|
|
|
PresentData string `orm:"column(present_data);type(text)" json:"present_data"`
|
|
|
|
|
CreateTime time.Time `orm:"type(datetime);column(create_time);auto_now_add" json:"create_time"`
|
|
|
|
|
UserAgent string `orm:"column(user_agent);size(500)" json:"user_agent"`
|
|
|
|
|
IPAddress string `orm:"column(ip_address);size(255)" json:"ip_address"`
|
2017-04-24 18:25:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TableName 获取对应数据库表名.
|
|
|
|
|
func (m *Logger) TableName() string {
|
|
|
|
|
return "logs"
|
|
|
|
|
}
|
2018-01-26 17:17:38 +08:00
|
|
|
|
|
2017-04-24 18:25:17 +08:00
|
|
|
|
// TableEngine 获取数据使用的引擎.
|
|
|
|
|
func (m *Logger) TableEngine() string {
|
|
|
|
|
return "INNODB"
|
|
|
|
|
}
|
|
|
|
|
func (m *Logger) TableNameWithPrefix() string {
|
|
|
|
|
return conf.GetDatabasePrefix() + m.TableName()
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-01 20:24:43 +08:00
|
|
|
|
func NewLogger() *Logger {
|
|
|
|
|
return &Logger{}
|
|
|
|
|
}
|
2017-04-24 18:25:17 +08:00
|
|
|
|
|
2017-06-09 18:14:55 +08:00
|
|
|
|
func (m *Logger) Add() error {
|
|
|
|
|
if m.MemberId <= 0 {
|
|
|
|
|
return errors.New("用户ID不能为空")
|
|
|
|
|
}
|
|
|
|
|
if m.Category == "" {
|
|
|
|
|
m.Category = "system"
|
|
|
|
|
}
|
|
|
|
|
if m.Content == "" {
|
|
|
|
|
return errors.New("日志内容不能为空")
|
|
|
|
|
}
|
|
|
|
|
loggerQueue.channel <- m
|
|
|
|
|
if atomic.LoadInt32(&(loggerQueue.isRuning)) <= 0 {
|
2018-01-26 17:17:38 +08:00
|
|
|
|
atomic.AddInt32(&(loggerQueue.isRuning), 1)
|
2017-06-09 18:14:55 +08:00
|
|
|
|
go addLoggerAsync()
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2017-04-24 18:25:17 +08:00
|
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
|
func addLoggerAsync() {
|
|
|
|
|
defer atomic.AddInt32(&(loggerQueue.isRuning), -1)
|
2017-06-09 18:14:55 +08:00
|
|
|
|
o := orm.NewOrm()
|
|
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
|
for {
|
|
|
|
|
logger := <-loggerQueue.channel
|
2017-06-09 18:14:55 +08:00
|
|
|
|
|
|
|
|
|
o.Insert(logger)
|
|
|
|
|
}
|
|
|
|
|
}
|