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

@@ -18,7 +18,7 @@ func Guardian(output bool) {
go Daemon.DaemonChan() go Daemon.DaemonChan()
if e := Daemon.MarkDaemon(); e != nil { if e := Daemon.MarkDaemon(); e != nil {
util.Log.Fatalln(e) util.Log.Warn("写入daemon标记文件失败: ", e)
} }
} }
@@ -31,14 +31,14 @@ func Guardian(output bool) {
}() }()
if global.Config.Settings.Basic.Interfaces == "" { //单网卡 if global.Config.Settings.Basic.Interfaces == "" { //单网卡
if !util.Checker.NetOk(global.Transports(nil)) { if !util.Checker.NetOk(global.Transports(nil)) {
util.Log.Println("Network down, trying to login") util.Log.Info("检测到掉线, trying to login")
e := Login(output, true, nil) e := Login(output, true, nil)
if e != nil { if e != nil {
util.Log.Println("Error: ", e) util.Log.Warn("登陆失败: ", e)
} }
} else { } else {
if global.Config.Settings.Debug.Enable { if global.Config.Settings.Debug.Enable {
util.Log.Println("Network ok") util.Log.Debug("Network ok")
} }
} }
} else { //多网卡 } else { //多网卡
@@ -47,16 +47,16 @@ func Guardian(output bool) {
var down []srunModels.Eth var down []srunModels.Eth
for _, eth := range interfaces { for _, eth := range interfaces {
if !util.Checker.NetOk(global.Transports(eth.Addr)) { if !util.Checker.NetOk(global.Transports(eth.Addr)) {
util.Log.Println(eth.Name + " network down") util.Log.Info("检测到掉线网口 ", eth.Name)
down = append(down, eth) down = append(down, eth)
} }
} }
for _, eth := range down { for _, eth := range down {
util.Log.Println(eth.Name) util.Log.Info(eth.Name)
e := Login(output, true, eth.Addr) e := Login(output, true, eth.Addr)
if e != nil { if e != nil {
util.Log.Println(eth.Name+" login error: ", e) util.Log.Warn("网口 ", eth.Name+" 登录失败: ", e)
} }
} }
} }
@@ -71,12 +71,12 @@ func Guardian(output bool) {
// EnterGuardian 守护模式入口控制是否进入daemon // EnterGuardian 守护模式入口控制是否进入daemon
func EnterGuardian() { func EnterGuardian() {
util.Log.Println("[Guardian mode]") util.Log.Info("[Guardian mode]")
if global.Config.Settings.Daemon.Enable || global.Flags.Daemon { if global.Config.Settings.Daemon.Enable || global.Flags.Daemon {
if err := exec.Command(os.Args[0], append(os.Args[1:], "--running-daemon")...).Start(); err != nil { if err := exec.Command(os.Args[0], append(os.Args[1:], "--running-daemon")...).Start(); err != nil {
util.Log.Fatalln(err) util.Log.Fatal("启动守护失败: ", err)
} }
util.Log.Println("[Daemon mode entered]") util.Log.Info("[Daemon mode entered]")
return return
} }
Guardian(true) Guardian(true)

19
main.go
View File

@@ -28,24 +28,15 @@ func main() {
//单次登录模式 //单次登录模式
if global.Config.Settings.Basic.Interfaces == "" { //单网卡 if global.Config.Settings.Basic.Interfaces == "" { //单网卡
if err := controllers.Login(true, global.Config.Settings.Basic.SkipNetCheck, nil); err != nil { if err := controllers.Login(true, global.Config.Settings.Basic.SkipNetCheck, nil); err != nil {
util.Log.Println("运行出错,状态异常") util.Log.Fatal("运行出错,状态异常: ", err)
if global.Config.Settings.Debug.Enable {
util.Log.Fatalln(err)
} else {
util.Log.Println(err)
return
}
} }
} else { //多网卡 } else { //多网卡
interfaces, e := util.GetInterfaceAddr() util.Log.Debug("多网卡模式")
if e != nil { interfaces, _ := util.GetInterfaceAddr()
return
}
for _, eth := range interfaces { for _, eth := range interfaces {
util.Log.Println(eth.Name) util.Log.Info("网卡: ", eth.Name)
if err := controllers.Login(true, global.Config.Settings.Basic.SkipNetCheck, eth.Addr); err != nil { if err := controllers.Login(true, global.Config.Settings.Basic.SkipNetCheck, eth.Addr); err != nil {
util.Log.Println(eth.Name + "运行出错,状态异常") util.Log.Warn("运行出错,状态异常: ", err)
util.Log.Println(err)
} }
} }
} }

View File

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

View File

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

View File

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

View File

@@ -10,20 +10,20 @@ import (
) )
func Login(c *srunTransfer.Login) error { func Login(c *srunTransfer.Login) error {
util.Log.Debug = c.Debug util.Log.DebugMode = c.Debug
util.Log.WriteFile = c.WriteLog util.Log.WriteFile = c.WriteLog
util.Log.OutPut = c.OutPut util.Log.OutPut = c.OutPut
G := util.GenerateLoginInfo(c.Https, c.LoginInfo.Form, c.LoginInfo.Meta) G := util.GenerateLoginInfo(c.Https, c.LoginInfo.Form, c.LoginInfo.Meta)
if c.CheckNet { if c.CheckNet {
util.Log.Println("Step0: 检查状态…") util.Log.Info("Step0: 检查状态…")
if util.Checker.NetOk(c.Transport) { if util.Checker.NetOk(c.Transport) {
util.Log.Println("网络 ok") util.Log.Info("网络 ok")
return nil return nil
} }
} }
util.Log.Println("Step1: 正在获取客户端ip") util.Log.Info("Step1: 正在获取客户端ip")
{ {
if _, body, e := tool.HTTP.GetString(&tool.GetRequest{ if _, body, e := tool.HTTP.GetString(&tool.GetRequest{
Url: G.UrlLoginPage, Url: G.UrlLoginPage,
@@ -36,7 +36,7 @@ func Login(c *srunTransfer.Login) error {
} }
} }
util.Log.Println("Step2: 正在获取Token") util.Log.Info("Step2: 正在获取Token")
{ {
if _, data, e := tool.HTTP.GetString(&tool.GetRequest{ if _, data, e := tool.HTTP.GetString(&tool.GetRequest{
Url: G.UrlGetChallengeApi, Url: G.UrlGetChallengeApi,
@@ -54,7 +54,7 @@ func Login(c *srunTransfer.Login) error {
} }
} }
util.Log.Println("Step3: 执行登录…") util.Log.Info("Step3: 执行登录…")
{ {
info, e := json.Marshal(map[string]string{ info, e := json.Marshal(map[string]string{
"username": G.Form.UserName, "username": G.Form.UserName,
@@ -101,9 +101,9 @@ func Login(c *srunTransfer.Login) error {
} else if G.LoginResult, e = util.GetResult(res); e != nil { } else if G.LoginResult, e = util.GetResult(res); e != nil {
return e return e
} else { } else {
util.Log.Println("登录结果: " + G.LoginResult) util.Log.Info("登录结果: " + G.LoginResult)
if c.Debug { if c.Debug {
util.Log.Println(res) util.Log.Info(res)
} }
} }