feat: 改用 logrus 输出日志

This commit is contained in:
Mmx233
2022-08-14 20:47:23 +08:00
parent 51e06a1596
commit df87d75aca
14 changed files with 107 additions and 181 deletions

View File

@@ -1,9 +1,11 @@
package util
import log "github.com/sirupsen/logrus"
func getbyte(a byte) int {
x := int(a)
if x > 255 {
Log.Fatal("INVALID_CHARACTER_ERR: DOM Exception 5")
log.Fatalln("INVALID_CHARACTER_ERR: DOM Exception 5")
}
return x
}

View File

@@ -3,6 +3,7 @@ package util
import (
"github.com/Mmx233/BitSrunLoginGo/global"
srunModels "github.com/Mmx233/BitSrunLoginGo/models"
log "github.com/sirupsen/logrus"
"net"
"regexp"
"strings"
@@ -17,13 +18,13 @@ func GetInterfaceAddr() ([]srunModels.Eth, error) {
}
reg, e := regexp.Compile(global.Config.Settings.Basic.Interfaces)
if e != nil {
Log.Fatal("interfaces设置异常无法解析: ", e)
log.Fatalln("interfaces设置异常无法解析: ", e)
}
for _, eth := range interfaces {
if reg.Match([]byte(eth.Name)) {
addrs, e := eth.Addrs()
if e != nil {
Log.Warn(eth.Name+" 网卡地址获取失败: ", e)
log.Warnln(eth.Name+" 网卡地址获取失败: ", e)
continue
}
for _, addr := range addrs {
@@ -31,7 +32,7 @@ func GetInterfaceAddr() ([]srunModels.Eth, error) {
var ip *net.TCPAddr
ip, e = net.ResolveTCPAddr("tcp", strings.Split(addr.String(), "/")[0]+":0")
if e != nil {
Log.Warn(eth.Name+" ip解析失败", e)
log.Warnln(eth.Name+" ip解析失败", e)
continue
}
result = append(result, srunModels.Eth{

View File

@@ -1,84 +0,0 @@
package util
import (
"fmt"
"github.com/Mmx233/tool"
"log"
"os"
"strings"
"time"
)
type loG struct {
timeStamp string
WriteFile bool
Path string
OutPut bool
DebugMode bool
}
var Log loG
func (c *loG) Init(debug, logFile, outPut bool, path string) error {
c.DebugMode = debug
c.WriteFile = logFile
c.OutPut = outPut
c.timeStamp = time.Now().Format("2006.01.02-15.04.05")
//日志路径初始化与处理
if c.DebugMode && c.WriteFile {
if !strings.HasSuffix(path, "/") {
path += "/"
}
c.Path = path
return os.MkdirAll(path, os.ModePerm)
}
return nil
}
func (c *loG) time() string {
return time.Now().Format("2006/01/02 15:04:05")
}
func (c *loG) WriteLog(name string, a ...interface{}) {
err := tool.File.Add(c.Path+name, c.time()+" "+fmt.Sprint(a...), 700)
if err != nil && c.OutPut {
log.Println(err)
}
}
func (c *loG) print(name string, fatal bool, a ...interface{}) {
a = append([]interface{}{"[" + name + "] "}, a...)
if c.DebugMode && c.WriteFile {
c.WriteLog("Login-"+c.timeStamp+".log", a...)
}
if c.OutPut {
if fatal {
if c.DebugMode {
log.Panicln(a...)
} else {
log.Fatalln(a...)
}
} else {
log.Println(a...)
}
}
}
func (c *loG) Debug(a ...interface{}) {
if c.DebugMode {
c.print("DEBUG", false, a...)
}
}
func (c *loG) Info(a ...interface{}) {
c.print("INFO", false, a...)
}
func (c *loG) Warn(a ...interface{}) {
c.print("WARN", false, a...)
}
func (c *loG) Fatal(a ...interface{}) {
c.print("FATAL", true, a...)
}