package main import ( "encoding/json" "log" "os" ) var oplog *Oplog // Oplog 操作日志类 type Oplog struct { oplog *log.Logger } func initOplog() { if oplog == nil { oplog = &Oplog{} f, err := os.OpenFile("./op.log", os.O_CREATE|os.O_APPEND|os.O_RDWR, 0644) if err != nil { panic(err) } oplog.oplog = log.New(f, "", log.Lmsgprefix) } } // Write 操作日志写入 func (op *Oplog) Write(data interface{}) { if d, ok := data.(*Task); ok { data, err := json.Marshal(d) if err != nil { log.Println(err) } op.oplog.Println(string(data)) return } log.Println("data must Task: ", data) }