feat: make diffrent fatal.log

This commit is contained in:
langhuihui
2023-08-18 12:43:58 +08:00
parent 21b86ef655
commit b979636281
6 changed files with 48 additions and 56 deletions

18
io.go
View File

@@ -167,17 +167,12 @@ func (io *IO) receive(streamPath string, specific IIO) error {
if v, ok := specific.(ISubscriber); ok { if v, ok := specific.(ISubscriber); ok {
wt = v.GetSubscriber().Config.WaitTimeout wt = v.GetSubscriber().Config.WaitTimeout
} }
io.Context, io.CancelFunc = context.WithCancel(util.Conditoinal[context.Context](io.Context == nil, Engine, io.Context))
s, create := findOrCreateStream(u.Path, wt) s, create := findOrCreateStream(u.Path, wt)
if s == nil { if s == nil {
return ErrBadStreamName return ErrBadStreamName
} }
io.Stream = s if io.Stream == nil { //初次
io.Spesific = specific io.Context, io.CancelFunc = context.WithCancel(util.Conditoinal[context.Context](io.Context == nil, Engine, io.Context))
io.StartTime = time.Now()
if io.Type == "" {
io.Type = reflect.TypeOf(specific).Elem().Name()
}
logFeilds := []zapcore.Field{zap.String("type", io.Type)} logFeilds := []zapcore.Field{zap.String("type", io.Type)}
if io.ID != "" { if io.ID != "" {
logFeilds = append(logFeilds, zap.String("ID", io.ID)) logFeilds = append(logFeilds, zap.String("ID", io.ID))
@@ -188,6 +183,13 @@ func (io *IO) receive(streamPath string, specific IIO) error {
logFeilds = append(logFeilds, zap.String("streamPath", s.Path)) logFeilds = append(logFeilds, zap.String("streamPath", s.Path))
io.Logger = io.Logger.With(logFeilds...) io.Logger = io.Logger.With(logFeilds...)
} }
}
io.Stream = s
io.Spesific = specific
io.StartTime = time.Now()
if io.Type == "" {
io.Type = reflect.TypeOf(specific).Elem().Name()
}
if v, ok := specific.(IPublisher); ok { if v, ok := specific.(IPublisher); ok {
conf := v.GetPublisher().Config conf := v.GetPublisher().Config
io.Type = strings.TrimSuffix(io.Type, "Publisher") io.Type = strings.TrimSuffix(io.Type, "Publisher")
@@ -195,7 +197,7 @@ func (io *IO) receive(streamPath string, specific IIO) error {
s.pubLocker.Lock() s.pubLocker.Lock()
defer s.pubLocker.Unlock() defer s.pubLocker.Unlock()
oldPublisher := s.Publisher oldPublisher := s.Publisher
if oldPublisher != nil && !oldPublisher.IsClosed() { if oldPublisher != nil {
zot := zap.String("old type", oldPublisher.GetPublisher().Type) zot := zap.String("old type", oldPublisher.GetPublisher().Type)
if oldPublisher == specific { // 断线重连 if oldPublisher == specific { // 断线重连
s.Info("republish", zot) s.Info("republish", zot)

View File

@@ -54,8 +54,6 @@ func (pub *Puller) startPull(puller IPuller) {
} }
}() }()
puber := puller.GetPublisher() puber := puller.GetPublisher()
originContext := puber.Context // 保存原始的Context
logger := puber.Logger // 保存原始的Logger
for puller.Info("start pull"); puller.Reconnect(); puller.Warn("restart pull") { for puller.Info("start pull"); puller.Reconnect(); puller.Warn("restart pull") {
if err = puller.Connect(); err != nil { if err = puller.Connect(); err != nil {
if err == io.EOF { if err == io.EOF {
@@ -68,8 +66,6 @@ func (pub *Puller) startPull(puller IPuller) {
} }
time.Sleep(time.Second * 5) time.Sleep(time.Second * 5)
} else { } else {
puber.Context = originContext // 每次重连都需要恢复原始的Context
puber.Logger = logger // 每次重连都需要恢复原始的Logger
if err = puller.Publish(pub.StreamPath, puller); err != nil { if err = puller.Publish(pub.StreamPath, puller); err != nil {
puller.Error("pull publish", zap.Error(err)) puller.Error("pull publish", zap.Error(err))
return return

View File

@@ -2,12 +2,14 @@ package util
import ( import (
"context" "context"
"log"
"os" "os"
"os/signal" "os/signal"
"path/filepath" "path/filepath"
"runtime" "runtime"
"strings" "strings"
"syscall" "syscall"
"time"
) )
var Null = struct{}{} var Null = struct{}{}
@@ -16,6 +18,21 @@ func Clone[T any](x T) *T {
return &x return &x
} }
func initFatalLog() *os.File {
fatal_log := "./fatal"
if _fatal_log := os.Getenv("M7S_FATAL_LOG"); _fatal_log != "" {
fatal_log = _fatal_log
}
os.MkdirAll(fatal_log, 0666)
fatal_log = filepath.Join(fatal_log, time.Now().Format("2006-01-02 15:04:05")+".log")
logFile, err := os.OpenFile(fatal_log, os.O_CREATE|os.O_APPEND|os.O_RDWR, 0666)
if err != nil {
log.Println("服务启动出错", "打开异常日志文件失败", err)
return nil
}
return logFile
}
func CurrentDir(path ...string) string { func CurrentDir(path ...string) string {
if _, currentFilePath, _, _ := runtime.Caller(1); len(path) == 0 { if _, currentFilePath, _, _ := runtime.Caller(1); len(path) == 0 {
return filepath.Dir(currentFilePath) return filepath.Dir(currentFilePath)

View File

@@ -3,24 +3,14 @@
package util package util
import ( import (
"log"
"os" "os"
"syscall" "syscall"
"time"
) )
func init() { func init() {
fatal_log := "./fatal.log" logFile := initFatalLog()
if _fatal_log := os.Getenv("M7S_FATAL_LOG"); _fatal_log != "" { if logFile != nil {
fatal_log = _fatal_log
}
logFile, err := os.OpenFile(fatal_log, os.O_CREATE|os.O_APPEND|os.O_RDWR, 0666)
if err != nil {
log.Println("服务启动出错", "打开异常日志文件失败", err)
return
}
// 将进程标准出错重定向至文件,进程崩溃时运行时将向该文件记录协程调用栈信息 // 将进程标准出错重定向至文件,进程崩溃时运行时将向该文件记录协程调用栈信息
syscall.Dup2(int(logFile.Fd()), int(os.Stderr.Fd())) syscall.Dup2(int(logFile.Fd()), int(os.Stderr.Fd()))
}
os.Stderr.WriteString("\n" + time.Now().Format("2006-01-02 15:04:05") + "--------------------------------\n")
} }

View File

@@ -3,23 +3,14 @@
package util package util
import ( import (
"log"
"os" "os"
"syscall" "syscall"
"time"
) )
func init() { func init() {
fatal_log := "./fatal.log" logFile := initFatalLog()
if _fatal_log := os.Getenv("M7S_FATAL_LOG"); _fatal_log != "" { if logFile != nil {
fatal_log = _fatal_log
}
logFile, err := os.OpenFile(fatal_log, os.O_CREATE|os.O_APPEND|os.O_RDWR, 0666)
if err != nil {
log.Println("服务启动出错", "打开异常日志文件失败", err)
return
}
// 将进程标准出错重定向至文件,进程崩溃时运行时将向该文件记录协程调用栈信息 // 将进程标准出错重定向至文件,进程崩溃时运行时将向该文件记录协程调用栈信息
syscall.Dup3(int(logFile.Fd()), int(os.Stderr.Fd()), 0) syscall.Dup3(int(logFile.Fd()), int(os.Stderr.Fd()), 0)
os.Stderr.WriteString("\n" + time.Now().Format("2006-01-02 15:04:05") + "--------------------------------\n") }
} }

View File

@@ -6,7 +6,6 @@ import (
"log" "log"
"os" "os"
"syscall" "syscall"
"time"
) )
var ( var (
@@ -27,16 +26,13 @@ func setStdHandle(stdhandle int32, handle syscall.Handle) error {
// redirectStderr to the file passed in // redirectStderr to the file passed in
func init() { func init() {
fatal_log := "./fatal.log" logFile := initFatalLog()
if _fatal_log := os.Getenv("M7S_FATAL_LOG"); _fatal_log != "" { if logFile != nil {
fatal_log = _fatal_log
}
logFile, err := os.OpenFile(fatal_log, os.O_CREATE|os.O_APPEND|os.O_RDWR, 0666)
err = setStdHandle(syscall.STD_ERROR_HANDLE, syscall.Handle(logFile.Fd())) err = setStdHandle(syscall.STD_ERROR_HANDLE, syscall.Handle(logFile.Fd()))
if err != nil { if err != nil {
log.Fatalf("Failed to redirect stderr to file: %v", err) log.Fatalf("Failed to redirect stderr to file: %v", err)
} }
// SetStdHandle does not affect prior references to stderr // SetStdHandle does not affect prior references to stderr
os.Stderr = logFile os.Stderr = logFile
os.Stderr.WriteString("\n" + time.Now().Format("2006-01-02 15:04:05") + "--------------------------------\n") }
} }