MCPcopy Create free account
hub / github.com/GoEdgeLab/EdgeNode / Init

Method Init

internal/apps/log_writer.go:21–67  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

19}
20
21func (this *LogWriter) Init() {
22 // 创建目录
23 var dir = files.NewFile(Tea.LogDir())
24 if !dir.Exists() {
25 err := dir.Mkdir()
26 if err != nil {
27 log.Println("[LOG]create log dir failed: " + err.Error())
28 }
29 }
30
31 // 打开要写入的日志文件
32 var logPath = Tea.LogFile("run.log")
33 fp, err := os.OpenFile(logPath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
34 if err != nil {
35 log.Println("[LOG]open log file failed: " + err.Error())
36 } else {
37 this.fp = fp
38 }
39
40 this.c = make(chan string, 1024)
41
42 // 异步写入文件
43 var maxFileSize int64 = 128 << 20 // 文件最大尺寸,超出此尺寸则清空
44 if fp != nil {
45 goman.New(func() {
46 var totalSize int64 = 0
47 stat, err := fp.Stat()
48 if err == nil {
49 totalSize = stat.Size()
50 }
51
52 for message := range this.c {
53 totalSize += int64(len(message))
54 _, err := fp.WriteString(timeutil.Format("Y/m/d H:i:s ") + message + "\n")
55 if err != nil {
56 log.Println("[LOG]write log failed: " + err.Error())
57 } else {
58 // 如果太大则Truncate
59 if totalSize > maxFileSize {
60 _ = fp.Truncate(0)
61 totalSize = 0
62 }
63 }
64 }
65 })
66 }
67}
68
69func (this *LogWriter) Write(message string) {
70 backgroundEnv, _ := os.LookupEnv("EdgeBackground")

Callers

nothing calls this directly

Calls 7

NewFunction · 0.92
WriteStringMethod · 0.80
StatMethod · 0.65
FormatMethod · 0.65
ErrorMethod · 0.45
SizeMethod · 0.45
TruncateMethod · 0.45

Tested by

no test coverage detected