log_entry.go 1.5 KB
package domain

import "time"

// LogEntry 日志内容
type LogEntry struct {
	// 对象名称 数据表名 / 文件名
	ObjectName string `json:"objectName"`
	// 对象类型 1.主表 2.分表 3.副表 4.源文件 5.校验文件
	ObjectType string `json:"objectType"`
	// 操作类型 1.主表生成 2.主表拆分 3.数据导入 4.分表生成 5.表复制 6.编辑记录 7.文件上传 8.文件校验
	OperationType string `json:"operationType"`
	// 日志内容
	Content string `json:"content"`
	// 操作人名称
	OperatorName string `json:"operatorName"`
	// 错误级别
	Level string `json:"level"`
	// 日志时间
	LogTime string `json:"logTime"`
	// 追加文件ID (操作类型 主表生成、数据导入时有效)
	AppendFileId int `json:"appendFileId,omitempty"`
	// 错误信息
	Error string   `json:"error"`
	ctx   *Context `json:"-"`
	Type  string   `json:"-"`
}

func (l LogEntry) Entry() LogEntry {
	return l
}

func (l LogEntry) Context() *Context {
	return l.ctx
}

func (l LogEntry) LogType() string {
	return l.Type
}

func (l LogEntry) OperateType() string {
	return l.OperationType
}

func (l *LogEntry) WithAppendFileId(fileId int) LogEntry {
	l.AppendFileId = fileId
	return *l
}

func NewLogEntry(fileOrTableName string, objectType string, operationType OperationType, ctx *Context) LogEntry {
	return LogEntry{
		ObjectName:    fileOrTableName,
		ObjectType:    objectType,
		OperationType: operationType.ToString(),
		OperatorName:  ctx.OperatorName,
		LogTime:       time.Now().Local().Format("2006-01-02 15:04:05"),
		ctx:           ctx,
	}
}