added simple log rotator

This commit is contained in:
2026-03-24 10:51:00 +02:00
parent b443292720
commit 6f66d14329
7 changed files with 44 additions and 10 deletions
+3
View File
@@ -35,6 +35,8 @@ type Config struct {
LogPath string `json:"logfile" yaml:"logfile"`
RunPath string `json:"runfile" yaml:"runfile"`
AsDaemon bool `json:"asDaemon" yaml:"asDaemon"`
LogLimit int64 `json:"logLimit" yaml:logLimit`
}
func NewConfig() (*Config, error) {
@@ -43,6 +45,7 @@ func NewConfig() (*Config, error) {
Port: client.DefaultServicePort,
},
AsDaemon: false,
LogLimit: 1024 * 1024 * 10, // 10 Mb
}
hostname, err := os.Hostname()
if err != nil {
+10
View File
@@ -0,0 +1,10 @@
package config
const (
confdirPath = "/home/ziggi/Projects/sys2agent/etc/minilb"
rundirPath = "/home/ziggi/Projects/sys2agent/tmp/run"
logdirPath = "/home/ziggi/Projects/sys2agent/tmp/log"
datadirPath = "/home/ziggi/Projects/sys2agent/tmp/data"
packageVersion = "0.0.1"
)
+25 -4
View File
@@ -3,11 +3,11 @@ package server
import (
"os"
"os/signal"
"syscall"
"os/user"
"path/filepath"
"strconv"
"syscall"
"time"
"helmet/app/config"
"helmet/app/handler"
@@ -88,13 +88,13 @@ func (srv *Server) Build() error {
if err != nil {
return err
}
// Create ghandler
// Create handler
handlerConfig := &handler.HandlerConfig{
Operator: srv.oper,
}
srv.hand = handler.NewHandler(handlerConfig)
// Create gservice
// Create service
serviceConfig := &service.ServiceConfig{
PortNum: srv.conf.Service.Port,
Hostname: srv.conf.Hostname,
@@ -220,6 +220,27 @@ func (srv *Server) Daemonize() error {
if err != nil {
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")
prevLogFile := logFile
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()))
prevLogFile.Close()
}
time.Sleep(10 * time.Second)
}
}
}
go logFunc()
// Write process ID
rundir := filepath.Dir(srv.conf.RunPath)
err = os.MkdirAll(rundir, 0750)