Skip to content
This repository was archived by the owner on Nov 5, 2024. It is now read-only.

Commit 4971029

Browse files
committed
[log]文件日志策略增加是否自动创建目录选项
1 parent f5e3562 commit 4971029

3 files changed

Lines changed: 25 additions & 6 deletions

File tree

tealogs/storage_file.go

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@ import (
55
"github.com/TeaWeb/code/tealogs/accesslogs"
66
"github.com/iwind/TeaGo/logs"
77
"os"
8+
"path/filepath"
89
"sync"
910
)
1011

1112
// 文件存储策略
1213
type FileStorage struct {
1314
Storage `yaml:", inline"`
1415

15-
Path string `yaml:"path" json:"path"` // 文件路径,支持变量:${year|month|week|day|hour|minute|second}
16+
Path string `yaml:"path" json:"path"` // 文件路径,支持变量:${year|month|week|day|hour|minute|second}
17+
AutoCreate bool `yaml:"autoCreate" json:"autoCreate"` // 是否自动创建目录
1618

1719
writeLocker sync.Mutex
1820

@@ -52,10 +54,10 @@ func (this *FileStorage) Write(accessLogs []*accesslogs.AccessLog) error {
5254
}
5355
_, err = fp.Write(data)
5456
if err != nil {
55-
this.Close()
57+
_ = this.Close()
5658
break
5759
}
58-
fp.WriteString("\n")
60+
_, _ = fp.WriteString("\n")
5961
}
6062
return nil
6163
}
@@ -87,7 +89,20 @@ func (this *FileStorage) fp() *os.File {
8789

8890
// 关闭其他的文件
8991
for _, f := range this.files {
90-
f.Close()
92+
_ = f.Close()
93+
}
94+
95+
// 是否创建文件目录
96+
if this.AutoCreate {
97+
dir := filepath.Dir(path)
98+
_, err := os.Stat(dir)
99+
if os.IsNotExist(err) {
100+
err = os.MkdirAll(dir, 0777)
101+
if err != nil {
102+
logs.Error(err)
103+
return nil
104+
}
105+
}
91106
}
92107

93108
// 打开新文件

teaweb/actions/default/proxy/log/policies/add.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ func (this *AddAction) RunPost(params struct {
3737
StorageType string
3838

3939
// file
40-
FilePath string
40+
FilePath string
41+
FileAutoCreate bool
4142

4243
// es
4344
EsEndpoint string
@@ -93,6 +94,7 @@ func (this *AddAction) RunPost(params struct {
9394
storage.Format = params.StorageFormat
9495
storage.Template = params.StorageTemplate
9596
storage.Path = params.FilePath
97+
storage.AutoCreate = params.FileAutoCreate
9698
instance = storage
9799
case tealogs.StorageTypeES:
98100
params.Must.

teaweb/actions/default/proxy/log/policies/update.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ func (this *UpdateAction) RunPost(params struct {
5656
StorageType string
5757

5858
// file
59-
FilePath string
59+
FilePath string
60+
FileAutoCreate bool
6061

6162
// es
6263
EsEndpoint string
@@ -117,6 +118,7 @@ func (this *UpdateAction) RunPost(params struct {
117118
storage.Format = params.StorageFormat
118119
storage.Template = params.StorageTemplate
119120
storage.Path = params.FilePath
121+
storage.AutoCreate = params.FileAutoCreate
120122
instance = storage
121123
case tealogs.StorageTypeES:
122124
params.Must.

0 commit comments

Comments
 (0)