-
Notifications
You must be signed in to change notification settings - Fork 0
/
adapter.go
74 lines (59 loc) · 1.54 KB
/
adapter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package storageAdapterFs
import (
"os"
"time"
"github.com/robfig/cron/v3"
)
type Adapter struct {
fileManager handlesManager
cronHandler *cron.Cron
}
type Config struct {
BasePath string
OpenHandleTimeout time.Duration
ForceUpdateOnMidnight bool
DateFormat string
Timezone string
GroupStreamsIntoFolders bool
FilePermissions os.FileMode
}
func NewFSStorageAdapter(config Config) (*Adapter, error) {
if config.FilePermissions == 0 {
config.FilePermissions = 0744
}
// ensure logs directory
if _, err := os.Stat(config.BasePath); os.IsNotExist(err) {
if err := os.MkdirAll(config.BasePath, config.FilePermissions); err != nil {
return &Adapter{}, err
}
}
adapter := Adapter{
fileManager: newHandlesManager(config),
}
if config.ForceUpdateOnMidnight {
customLocation, err := time.LoadLocation(config.Timezone)
if err != nil {
return &Adapter{}, err
}
cronHandler := cron.New(cron.WithLocation(customLocation))
cronHandler.AddFunc("0 0 * * * *", func() {
adapter.fileManager.CloseAll()
})
cronHandler.Start()
adapter.cronHandler = cronHandler
}
return &adapter, nil
}
func (a *Adapter) AppendLogs(key string, logs string) {
a.fileManager.Write(key, logs)
}
func (a *Adapter) ListLogFiles(key string) []string {
return a.fileManager.ListLogFiles(key)
}
func (a *Adapter) GetLogs(key string, logFile string) ([]byte, error) {
return a.fileManager.GetLogs(key, logFile)
}
func (a *Adapter) Shutdown() {
a.fileManager.CloseAll()
a.cronHandler.Stop()
}