service/vendor/gitlab.tiefen.fun/ironfan/poseidon/actions/writer.go

57 lines
1.1 KiB
Go

package actions
import (
"encoding/json"
"fmt"
rotatelogs "github.com/lestrrat-go/file-rotatelogs"
"github.com/rs/zerolog"
"time"
)
// 埋点日志在 /data/action/{Category}/ 下
var defaultWriter zerolog.Logger
type Conf struct {
Path string `json:"path" yaml:"path"`
Category string `json:"category" yaml:"category"`
}
func Init(conf *Conf) {
if conf == nil {
conf = new(Conf)
}
if len(conf.Path) <= 0 {
conf.Path = "/data/action/"
}
if len(conf.Category) <= 0 {
conf.Category = "application"
}
fileWriter, _ := rotatelogs.New(
fmt.Sprintf("%s%s/%%Y%%m/action_%%d", conf.Path, conf.Category),
rotatelogs.WithRotationTime(time.Duration(86400)*time.Second),
)
defaultWriter = zerolog.New(fileWriter)
}
func AddActionLog(app string, mid int64, oid, typ, styp, from, ip string, opt map[string]any) error {
optByte, err := json.Marshal(opt)
if err != nil {
return err
}
defaultWriter.Log().
Int64("t", time.Now().Unix()).
Str("app", app).
Int64("mid", mid).
Str("oid", oid).
Str("type", typ).
Str("stype", styp).
Str("from", from).
Str("ip", ip).
Str("opt", string(optByte)).
Send()
return nil
}