working commit
This commit is contained in:
+21
-17
@@ -253,8 +253,10 @@ func (srv *Server) Daemonize() error {
|
||||
|
||||
func (srv *Server) Rotator() {
|
||||
srv.wg.Add(1)
|
||||
var counter uint64
|
||||
logFunc := func() {
|
||||
for {
|
||||
counter += 1
|
||||
select {
|
||||
case <-srv.ctx.Done():
|
||||
srv.wg.Done()
|
||||
@@ -262,25 +264,27 @@ func (srv *Server) Rotator() {
|
||||
return
|
||||
default:
|
||||
}
|
||||
stat, err := srv.logf.Stat()
|
||||
if err == nil && stat.Size() > srv.conf.LogLimit {
|
||||
srv.log.Infof("Rotate log file")
|
||||
countFiles := 3
|
||||
for i := 1; i < countFiles; i++ {
|
||||
nextName := fmt.Sprintf("%s.%d", srv.conf.LogPath, i+1)
|
||||
prevName := fmt.Sprintf("%s.%d", srv.conf.LogPath, i)
|
||||
os.Rename(prevName, nextName)
|
||||
}
|
||||
os.Rename(srv.conf.LogPath, srv.conf.LogPath+".1")
|
||||
logFile, err := os.OpenFile(srv.conf.LogPath, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0640)
|
||||
if err == nil {
|
||||
syscall.Dup2(int(logFile.Fd()), int(os.Stdout.Fd()))
|
||||
syscall.Dup2(int(logFile.Fd()), int(os.Stderr.Fd()))
|
||||
srv.logf.Close()
|
||||
srv.logf = logFile
|
||||
if (counter % 60) == 1 {
|
||||
stat, err := srv.logf.Stat()
|
||||
if err == nil && stat.Size() > srv.conf.LogLimit {
|
||||
srv.log.Infof("Rotate log file")
|
||||
countFiles := 3
|
||||
for i := 1; i < countFiles; i++ {
|
||||
nextName := fmt.Sprintf("%s.%d", srv.conf.LogPath, i+1)
|
||||
prevName := fmt.Sprintf("%s.%d", srv.conf.LogPath, i)
|
||||
os.Rename(prevName, nextName)
|
||||
}
|
||||
os.Rename(srv.conf.LogPath, srv.conf.LogPath+".1")
|
||||
logFile, err := os.OpenFile(srv.conf.LogPath, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0640)
|
||||
if err == nil {
|
||||
syscall.Dup2(int(logFile.Fd()), int(os.Stdout.Fd()))
|
||||
syscall.Dup2(int(logFile.Fd()), int(os.Stderr.Fd()))
|
||||
srv.logf.Close()
|
||||
srv.logf = logFile
|
||||
}
|
||||
}
|
||||
}
|
||||
time.Sleep(3 * time.Second)
|
||||
time.Sleep(1 * time.Second)
|
||||
}
|
||||
}
|
||||
go logFunc()
|
||||
|
||||
Reference in New Issue
Block a user