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()
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 !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)
if e != nil {
util.Log.Println("Error: ", e)
util.Log.Warn("登陆失败: ", e)
}
} else {
if global.Config.Settings.Debug.Enable {
util.Log.Println("Network ok")
util.Log.Debug("Network ok")
}
}
} else { //多网卡
@@ -47,16 +47,16 @@ func Guardian(output bool) {
var down []srunModels.Eth
for _, eth := range interfaces {
if !util.Checker.NetOk(global.Transports(eth.Addr)) {
util.Log.Println(eth.Name + " network down")
util.Log.Info("检测到掉线网口 ", eth.Name)
down = append(down, eth)
}
}
for _, eth := range down {
util.Log.Println(eth.Name)
util.Log.Info(eth.Name)
e := Login(output, true, eth.Addr)
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
func EnterGuardian() {
util.Log.Println("[Guardian mode]")
util.Log.Info("[Guardian mode]")
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 {
util.Log.Fatalln(err)
util.Log.Fatal("启动守护失败: ", err)
}
util.Log.Println("[Daemon mode entered]")
util.Log.Info("[Daemon mode entered]")
return
}
Guardian(true)

19
main.go
View File

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

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) Println(a ...interface{}) {
c.genTimeStamp()
func (c *loG) print(fatal bool, a ...interface{}) {
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) 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)]))
}
}

View File

@@ -10,20 +10,20 @@ import (
)
func Login(c *srunTransfer.Login) error {
util.Log.Debug = c.Debug
util.Log.DebugMode = c.Debug
util.Log.WriteFile = c.WriteLog
util.Log.OutPut = c.OutPut
G := util.GenerateLoginInfo(c.Https, c.LoginInfo.Form, c.LoginInfo.Meta)
if c.CheckNet {
util.Log.Println("Step0: 检查状态…")
util.Log.Info("Step0: 检查状态…")
if util.Checker.NetOk(c.Transport) {
util.Log.Println("网络 ok")
util.Log.Info("网络 ok")
return nil
}
}
util.Log.Println("Step1: 正在获取客户端ip")
util.Log.Info("Step1: 正在获取客户端ip")
{
if _, body, e := tool.HTTP.GetString(&tool.GetRequest{
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{
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{
"username": G.Form.UserName,
@@ -101,9 +101,9 @@ func Login(c *srunTransfer.Login) error {
} else if G.LoginResult, e = util.GetResult(res); e != nil {
return e
} else {
util.Log.Println("登录结果: " + G.LoginResult)
util.Log.Info("登录结果: " + G.LoginResult)
if c.Debug {
util.Log.Println(res)
util.Log.Info(res)
}
}