Files
public/mycatch/mycatch.go
谢小军 341beff278 fix log
2020-06-03 19:58:33 +08:00

53 lines
1.6 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
错误信息日志记录(panic信息不可度量的)
注意:每个 goroutine 开始部分都需要写上:defer mycatch.Dmp()
类似于
go func(){
defer mycatch.Dmp()
}()
*/
package mycatch
import (
"fmt"
"runtime/debug"
"github.com/xxjwxc/public/mylog"
)
// Dmp dmp the recover
func Dmp() {
errstr := ""
if err := recover(); err != nil {
errstr += (fmt.Sprintf("%v\r\n", err)) //输出panic信息
errstr += ("--------------------------------------------\r\n")
}
errstr += (string(debug.Stack())) //输出堆栈信息
mylog.Panic(errstr, "err")
// OnPrintErr(errstr)
}
// func OnPrintErr(errstring string) {
// path := tools.GetCurrentDirectory() + "/err"
// if !tools.CheckFileIsExist(path) {
// os.MkdirAll(path, os.ModePerm) //生成多级目录
// }
// now := time.Now() //获取当前时间
// pid := os.Getpid() //获取进程ID
// timeStr := now.Format("2006-01-02") //设定时间格式
// fname := fmt.Sprintf("%s/panic_%s-%x.log", path, timeStr, pid) //保存错误信息文件名:程序名-进程ID-当前时间(年月日时分秒)
// fmt.Println("panic to file ", fname)
// f, err := os.OpenFile(fname, os.O_CREATE|os.O_APPEND|os.O_RDWR, 0666)
// if err != nil {
// return
// }
// defer f.Close()
// f.WriteString("=========================" + now.Format("2006-01-02 15:04:05 ========================= \r\n"))
// f.WriteString(errstring) //输出堆栈信息
// f.WriteString("=========================end=========================")
// }