package log

import (
	"fmt"
	"leafstalk/otherutils/file"
	"leafstalk/otherutils/system"
	"log"
	"os"
	"time"
)

// 输出到控制台的错误日志重定向到文件中
func RedirectStderr(appName string) {

	matchedPattern := fmt.Sprintf("%s.stderr.*.log", appName)
	err := file.RemoveMatchedFiles(matchedPattern)
	if err != nil {
		log.Fatalf("redirectStderr error %v", err)
	}

	nfp := fmt.Sprintf("%s.stderr.%s.log", appName, time.Now().Format("20060102"))

	file, err := os.OpenFile(nfp, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
	if err != nil {
		log.Fatalf("redirectStderr OpenFile error %v", err)
	}
	system.Stderr(file)
}
