feat: 优化日志组件,添加日志分级

This commit is contained in:
Mmx233
2022-03-02 22:45:38 +08:00
parent bf757a2bcc
commit f778319a00
6 changed files with 62 additions and 66 deletions

View File

@@ -3,7 +3,7 @@ package util
func getbyte(a byte) int {
x := int(a)
if x > 255 {
Log.Fatalln("INVALID_CHARACTER_ERR: DOM Exception 5")
Log.Fatal("INVALID_CHARACTER_ERR: DOM Exception 5")
}
return x
}

View File

@@ -17,21 +17,21 @@ func GetInterfaceAddr() ([]srunModels.Eth, error) {
}
reg, e := regexp.Compile(global.Config.Settings.Basic.Interfaces)
if e != nil {
Log.Println("interfaces设置异常无法解析")
return nil, e
Log.Fatal("interfaces设置异常无法解析: ", e)
}
for _, eth := range interfaces {
if reg.Match([]byte(eth.Name)) {
addrs, e := eth.Addrs()
if e != nil {
Log.Println(eth.Name + " 地址获取失败")
Log.Warn(eth.Name+" 网卡地址获取失败: ", e)
continue
}
for _, addr := range addrs {
if strings.Contains(addr.String(), ".") {
var ip *net.TCPAddr
ip, e = net.ResolveTCPAddr("tcp", strings.Split(addr.String(), "/")[0]+":0")
if e != nil {
Log.Println(eth.Name+" ip解析失败", e)
Log.Warn(eth.Name+" ip解析失败", e)
continue
}
result = append(result, srunModels.Eth{

View File

@@ -14,16 +14,17 @@ type loG struct {
timeStamp string
WriteFile bool
Path string
Debug bool
OutPut bool
DebugMode bool
}
var Log loG
func (c *loG) Init(debug, logFile, outPut bool, path string) error {
c.Debug = debug
c.DebugMode = debug
c.WriteFile = logFile
c.OutPut = outPut
c.timeStamp = time.Now().Format("2006.01.02-15.04.05")
//日志路径初始化与处理
if !strings.HasSuffix(path, "/") {
@@ -38,46 +39,50 @@ func (c *loG) time() string {
}
func (c *loG) WriteLog(name string, a ...interface{}) {
if !(c.Debug && c.WriteFile) {
return
}
var t string
for _, v := range a {
t += fmt.Sprint(v)
}
err := tool.File.Add(c.Path+name, c.time()+" "+t, 700)
if err != nil {
log.Println("Write log error: ", err)
err := tool.File.Add(c.Path+name, c.time()+" "+fmt.Sprint(a...), 700)
if err != nil && c.OutPut {
log.Println(err)
}
}
func (c *loG) genTimeStamp() {
if c.timeStamp == "" {
c.timeStamp = time.Now().Format("2006.01.02-15.04.05")
func (c *loG) print(fatal bool, a ...interface{}) {
if c.DebugMode && c.WriteFile {
c.WriteLog("Login-"+c.timeStamp+".log", a...)
}
}
func (c *loG) Println(a ...interface{}) {
c.genTimeStamp()
c.WriteLog("Login-"+c.timeStamp+".log", a...)
if c.OutPut {
log.Println(a...)
if fatal {
if c.DebugMode {
log.Panicln(a...)
} else {
log.Fatalln(a...)
}
} else {
log.Println(a...)
}
}
}
func (c *loG) Fatalln(a ...interface{}) {
c.genTimeStamp()
c.WriteLog("LoginError-"+c.timeStamp+".log", a...)
if c.OutPut {
log.Fatalln(a...)
func (c *loG) Debug(a ...interface{}) {
if c.DebugMode {
c.print(false, append([]interface{}{"[DEBUG] "}, a...)...)
}
}
func (c *loG) Info(a ...interface{}) {
c.print(false, append([]interface{}{"[INFO] "}, a...)...)
}
func (c *loG) Warn(a ...interface{}) {
c.print(false, append([]interface{}{"[WARN] "}, a...)...)
}
func (c *loG) Fatal(a ...interface{}) {
c.print(true, append([]interface{}{"[FATAL] "}, a...)...)
}
func (c *loG) CatchRecover() {
if e := recover(); e != nil {
c.Println(e)
var buf [4096]byte
c.Println(string(buf[:runtime.Stack(buf[:], false)]))
os.Exit(1)
c.Fatal(e, "\n", string(buf[:runtime.Stack(buf[:], false)]))
}
}