1Panel/backend/log/manager.go

54 lines
931 B
Go
Raw Normal View History

2022-12-08 13:27:49 +00:00
package log
import (
"github.com/robfig/cron/v3"
"path"
"sync"
"time"
)
type manager struct {
2023-02-01 07:50:39 +00:00
startAt time.Time
fire chan string
cr *cron.Cron
context chan int
wg sync.WaitGroup
lock sync.Mutex
2022-12-08 13:27:49 +00:00
}
func (m *manager) Fire() chan string {
return m.fire
}
func (m *manager) Close() {
close(m.context)
m.cr.Stop()
}
func NewManager(c *Config) (Manager, error) {
m := &manager{
startAt: time.Now(),
cr: cron.New(),
fire: make(chan string),
context: make(chan int),
wg: sync.WaitGroup{},
}
2022-12-08 13:40:53 +00:00
if _, err := m.cr.AddFunc(c.RollingTimePattern, func() {
2022-12-08 13:27:49 +00:00
m.fire <- m.GenLogFileName(c)
}); err != nil {
return nil, err
}
m.cr.Start()
return m, nil
}
func (m *manager) GenLogFileName(c *Config) (filename string) {
m.lock.Lock()
filename = path.Join(c.LogPath, c.FileName+"-"+m.startAt.Format(c.TimeTagFormat)) + c.LogSuffix
2022-12-08 13:27:49 +00:00
m.startAt = time.Now()
m.lock.Unlock()
return
}