示例#1
0
文件: logger.go 项目: iizotop/kmg
func Log(category string, msg string, obj interface{}) {
	logPath := defaultEnv.Env.LogPath
	toWrite := append(kmgJson.MustMarshal(logRow{
		Time: time.Now().Format(time.RFC3339),
		Msg:  msg,
		Obj:  obj,
	}), byte('\n'))
	err := kmgFile.AppendFile(filepath.Join(logPath, category+".log"), toWrite)
	if err != nil {
		panic(err)
	}
	return
}
示例#2
0
//new file log, will mkdir if dir not exist.
// usage:
// 		kmgLog.DefaultLogger = kmgLog.NewFileLogger("log")
func NewFileLogWriter(logDir string) LogWriter {
	kmgFile.MustMkdirAll(logDir)
	return func(r LogRow) {
		b, err := r.Marshal()
		if err != nil {
			fmt.Println("[fileLoger] logToJson fail", err)
			return
		}
		toWrite := append(b, byte('\n'))
		err = kmgFile.AppendFile(filepath.Join(logDir, r.Cat+".log"), toWrite)
		if err != nil {
			fmt.Println("[fileLoger] logToJson fail", err)
			return
		}
	}
}