package logger import ( "time" "git.shuncheng.lu/bigthing/gocommon/pkg/conf" "git.shuncheng.lu/bigthing/gocommon/pkg/internal/json" ) // //******************task.log文件 //日志格式举例:2019-08-06 23:50:22,798 INFO - {"name":"city_efficient_job","project":"ebike-geo", "action": 1, "code": 0, "msg": "msg", "event_time": 1565278582322, "event_id": 1565278582322} //#{project}: 项目名称 //#{name}: 任务名称,英文名 //#{action}: 当前操作类型, 0, 开始执行, 1 结束执行 //#{code}: 任务结果: 0 执行成功, 1 失败(option, action==1时必填) //#{msg}: 任务扩展信息(option) //#{event_time}: 精确到毫秒级的时间戳 //#{event_id}: 任务开始的时间戳, 精确到毫秒级 //日志分割逻辑 //接天分割 //task.log # 当前的日志 //task.log.2019-08-03 # 2019-08-03 号的日志 ///data/log/{project_name}/task.log.2019-08-03 #日志存放目录 type taskLogContent struct { Name string `json:"name"` Project string `json:"project"` Action int `json:"action"` Code int `json:"code"` Msg string `json:"msg"` EventTime int64 `json:"event_time"` EventId int64 `json:"event_id"` } var ( TaskInfo = func(name string, action int, code int, msg string, eventId int64) { nowTime := time.Now().UnixNano() / 1e6 content := taskLogContent{ Name: name, Project: conf.GetProjectName(), Action: action, Code: code, Msg: msg, EventTime: nowTime, EventId: eventId} s, _ := json.Marshal(content) err := _fileLogWriter.Tasklog().taskOutput(LevelInfo, string(s)) if err != nil { consoleLogger.Errorf("write Info log to file fail, err: %s", err.Error()) } } TaskWarn = func(name string, action int, code int, msg string) { nowTime := time.Now().UnixNano() / 1e6 content := taskLogContent{ Name: name, Project: conf.GetProjectName(), Action: action, Code: code, Msg: msg, EventTime: nowTime, EventId: nowTime} s, _ := json.Marshal(content) err := _fileLogWriter.Tasklog().taskOutput(LevelWarning, string(s)) if err != nil { consoleLogger.Errorf("write Warn log to file fail, err: %s", err.Error()) } } TaskError = func(name string, action int, code int, msg string) { nowTime := time.Now().UnixNano() / 1e6 content := taskLogContent{ Name: name, Project: conf.GetProjectName(), Action: action, Code: code, Msg: msg, EventTime: nowTime, EventId: nowTime} s, _ := json.Marshal(content) err := _fileLogWriter.Tasklog().taskOutput(LevelError, string(s)) if err != nil { consoleLogger.Errorf("write Error log to file fail, err: %s", err.Error()) } } )