mirror of
https://github.com/TeaOSLab/EdgeNode.git
synced 2025-12-03 23:11:54 +08:00
优化节点日志记录,可以记录和上报panic错误
This commit is contained in:
@@ -184,6 +184,8 @@ func (this *AppCmd) runStart() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_ = os.Setenv("EdgeBackground", "on")
|
||||||
|
|
||||||
cmd := exec.Command(os.Args[0])
|
cmd := exec.Command(os.Args[0])
|
||||||
err := cmd.Start()
|
err := cmd.Start()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -6,6 +6,10 @@ import (
|
|||||||
"github.com/iwind/TeaGo/logs"
|
"github.com/iwind/TeaGo/logs"
|
||||||
"github.com/iwind/TeaGo/utils/time"
|
"github.com/iwind/TeaGo/utils/time"
|
||||||
"log"
|
"log"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"runtime"
|
||||||
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
type LogWriter struct {
|
type LogWriter struct {
|
||||||
@@ -34,7 +38,23 @@ func (this *LogWriter) Init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (this *LogWriter) Write(message string) {
|
func (this *LogWriter) Write(message string) {
|
||||||
log.Println(message)
|
// 文件和行号
|
||||||
|
var callDepth = 3
|
||||||
|
var file string
|
||||||
|
var line int
|
||||||
|
var ok bool
|
||||||
|
_, file, line, ok = runtime.Caller(callDepth)
|
||||||
|
if !ok {
|
||||||
|
file = "???"
|
||||||
|
line = 0
|
||||||
|
} else {
|
||||||
|
file = filepath.Base(file)
|
||||||
|
}
|
||||||
|
|
||||||
|
backgroundEnv, _ := os.LookupEnv("EdgeBackground")
|
||||||
|
if backgroundEnv != "on" {
|
||||||
|
log.Println(message + " (" + file + ":" + strconv.Itoa(line) + ")")
|
||||||
|
}
|
||||||
|
|
||||||
if this.fileAppender != nil {
|
if this.fileAppender != nil {
|
||||||
_, err := this.fileAppender.AppendString(timeutil.Format("Y/m/d H:i:s ") + message + "\n")
|
_, err := this.fileAppender.AppendString(timeutil.Format("Y/m/d H:i:s ") + message + "\n")
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package nodes
|
package nodes
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
||||||
@@ -28,6 +29,7 @@ import (
|
|||||||
"os/exec"
|
"os/exec"
|
||||||
"runtime"
|
"runtime"
|
||||||
"sync"
|
"sync"
|
||||||
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -75,6 +77,9 @@ func (this *Node) Start() {
|
|||||||
DaemonPid = os.Getppid()
|
DaemonPid = os.Getppid()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 处理异常
|
||||||
|
this.handlePanic()
|
||||||
|
|
||||||
// 启动事件
|
// 启动事件
|
||||||
events.Notify(events.EventStart)
|
events.Notify(events.EventStart)
|
||||||
|
|
||||||
@@ -178,6 +183,7 @@ func (this *Node) Daemon() {
|
|||||||
|
|
||||||
// 可以标记当前是从守护进程启动的
|
// 可以标记当前是从守护进程启动的
|
||||||
_ = os.Setenv("EdgeDaemon", "on")
|
_ = os.Setenv("EdgeDaemon", "on")
|
||||||
|
_ = os.Setenv("EdgeBackground", "on")
|
||||||
|
|
||||||
cmd := exec.Command(exe)
|
cmd := exec.Command(exe)
|
||||||
err = cmd.Start()
|
err = cmd.Start()
|
||||||
@@ -531,3 +537,33 @@ func (this *Node) listenSock() error {
|
|||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 处理异常
|
||||||
|
func (this *Node) handlePanic() {
|
||||||
|
// 如果是在前台运行就直接返回
|
||||||
|
backgroundEnv, _ := os.LookupEnv("EdgeBackground")
|
||||||
|
if backgroundEnv != "on" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var panicFile = Tea.Root + "/logs/panic.log"
|
||||||
|
|
||||||
|
// 分析panic
|
||||||
|
data, err := ioutil.ReadFile(panicFile)
|
||||||
|
if err == nil {
|
||||||
|
var index = bytes.Index(data, []byte("panic:"))
|
||||||
|
if index >= 0 {
|
||||||
|
remotelogs.Error("NODE", "fatal error: "+string(data[index:]))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fp, err := os.OpenFile(panicFile, os.O_CREATE|os.O_TRUNC|os.O_WRONLY|os.O_APPEND, 0777)
|
||||||
|
if err != nil {
|
||||||
|
logs.Println("NODE", "open 'panic.log' failed: "+err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
err = syscall.Dup2(int(fp.Fd()), int(os.Stderr.Fd()))
|
||||||
|
if err != nil {
|
||||||
|
logs.Println("NODE", "write to 'panic.log' failed: "+err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user