added simple log rotator
This commit is contained in:
@@ -48,6 +48,7 @@ type Config struct {
|
|||||||
Datadir string `json:"datadir" yaml:datadir`
|
Datadir string `json:"datadir" yaml:datadir`
|
||||||
Hostname string `json:"hostname" yaml:hostname`
|
Hostname string `json:"hostname" yaml:hostname`
|
||||||
Hostnames []string `json:"hostnames" yaml:hostnames`
|
Hostnames []string `json:"hostnames" yaml:hostnames`
|
||||||
|
LogLimit int64 `json:"logLimit" yaml:logLimit`
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewConfig() *Config {
|
func NewConfig() *Config {
|
||||||
@@ -82,6 +83,7 @@ func NewConfig() *Config {
|
|||||||
//Certpath: certpath,
|
//Certpath: certpath,
|
||||||
//Keypath: keypath,
|
//Keypath: keypath,
|
||||||
Hostnames: make([]string, 0),
|
Hostnames: make([]string, 0),
|
||||||
|
LogLimit: 1024 * 1024 * 10, // 10 Mb
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -431,6 +431,24 @@ func (srv *Server) Daemonize() error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
// Log file rotator
|
||||||
|
logFunc := func() {
|
||||||
|
for {
|
||||||
|
stat, err := os.Stat(srv.conf.Logpath)
|
||||||
|
if err == nil && stat.Size() > srv.conf.LogLimit {
|
||||||
|
os.Rename(srv.conf.Logpath+".2", srv.conf.Logpath+".3")
|
||||||
|
os.Rename(srv.conf.Logpath+".1", srv.conf.Logpath+".2")
|
||||||
|
os.Rename(srv.conf.Logpath, srv.conf.Logpath+".1")
|
||||||
|
logFile.Close()
|
||||||
|
logFile, err = os.OpenFile(srv.conf.Logpath, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0640)
|
||||||
|
syscall.Dup2(int(logFile.Fd()), int(os.Stdout.Fd()))
|
||||||
|
syscall.Dup2(int(logFile.Fd()), int(os.Stderr.Fd()))
|
||||||
|
time.Sleep(10 * time.Second)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
go logFunc()
|
||||||
// Write process ID
|
// Write process ID
|
||||||
rundir := filepath.Dir(srv.conf.Runpath)
|
rundir := filepath.Dir(srv.conf.Runpath)
|
||||||
err = os.MkdirAll(rundir, 0750)
|
err = os.MkdirAll(rundir, 0750)
|
||||||
|
|||||||
Reference in New Issue
Block a user