mirror of
https://github.com/TeaOSLab/EdgeNode.git
synced 2025-11-08 03:00:27 +08:00
优化本地日志
This commit is contained in:
@@ -1,10 +1,11 @@
|
|||||||
package apps
|
package apps
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/TeaOSLab/EdgeNode/internal/goman"
|
||||||
|
"github.com/TeaOSLab/EdgeNode/internal/utils/sizes"
|
||||||
"github.com/iwind/TeaGo/Tea"
|
"github.com/iwind/TeaGo/Tea"
|
||||||
"github.com/iwind/TeaGo/files"
|
"github.com/iwind/TeaGo/files"
|
||||||
"github.com/iwind/TeaGo/logs"
|
timeutil "github.com/iwind/TeaGo/utils/time"
|
||||||
"github.com/iwind/TeaGo/utils/time"
|
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"runtime"
|
"runtime"
|
||||||
@@ -13,27 +14,55 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type LogWriter struct {
|
type LogWriter struct {
|
||||||
fileAppender *files.Appender
|
fp *os.File
|
||||||
|
c chan string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *LogWriter) Init() {
|
func (this *LogWriter) Init() {
|
||||||
// 创建目录
|
// 创建目录
|
||||||
dir := files.NewFile(Tea.LogDir())
|
var dir = files.NewFile(Tea.LogDir())
|
||||||
if !dir.Exists() {
|
if !dir.Exists() {
|
||||||
err := dir.Mkdir()
|
err := dir.Mkdir()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("[error]" + err.Error())
|
log.Println("[LOG]create log dir failed: " + err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
logFile := files.NewFile(Tea.LogFile("run.log"))
|
|
||||||
|
|
||||||
// 打开要写入的日志文件
|
// 打开要写入的日志文件
|
||||||
appender, err := logFile.Appender()
|
var logPath = Tea.LogFile("run.log")
|
||||||
|
fp, err := os.OpenFile(logPath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logs.Error(err)
|
log.Println("[LOG]open log file failed: " + err.Error())
|
||||||
} else {
|
} else {
|
||||||
this.fileAppender = appender
|
this.fp = fp
|
||||||
|
}
|
||||||
|
|
||||||
|
this.c = make(chan string, 1024)
|
||||||
|
|
||||||
|
// 异步写入文件
|
||||||
|
var maxFileSize = 2 * sizes.G // 文件最大尺寸,超出此尺寸则清空
|
||||||
|
if fp != nil {
|
||||||
|
goman.New(func() {
|
||||||
|
var totalSize int64 = 0
|
||||||
|
stat, err := fp.Stat()
|
||||||
|
if err == nil {
|
||||||
|
totalSize = stat.Size()
|
||||||
|
}
|
||||||
|
|
||||||
|
for message := range this.c {
|
||||||
|
totalSize += int64(len(message))
|
||||||
|
_, err := fp.WriteString(timeutil.Format("Y/m/d H:i:s ") + message + "\n")
|
||||||
|
if err != nil {
|
||||||
|
log.Println("[LOG]write log failed: " + err.Error())
|
||||||
|
} else {
|
||||||
|
// 如果太大则Truncate
|
||||||
|
if totalSize > maxFileSize {
|
||||||
|
_ = fp.Truncate(0)
|
||||||
|
totalSize = 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -59,18 +88,15 @@ func (this *LogWriter) Write(message string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if this.fileAppender != nil {
|
this.c <- message
|
||||||
_, err := this.fileAppender.AppendString(timeutil.Format("Y/m/d H:i:s ") + message + "\n")
|
|
||||||
if err != nil {
|
|
||||||
log.Println("[error]" + err.Error())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *LogWriter) Close() {
|
func (this *LogWriter) Close() {
|
||||||
if this.fileAppender != nil {
|
if this.fp != nil {
|
||||||
_ = this.fileAppender.Close()
|
_ = this.fp.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
close(this.c)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *LogWriter) packagePath(path string) string {
|
func (this *LogWriter) packagePath(path string) string {
|
||||||
|
|||||||
Reference in New Issue
Block a user