From bd15d8f7ee50b125898b2b6ab3560767595cf87e Mon Sep 17 00:00:00 2001 From: somebodywashere <68244480+somebodywashere@users.noreply.github.com> Date: Mon, 18 Dec 2023 21:27:42 +0300 Subject: [PATCH] Improved logs clearing, added previous logs File name change: 3xipl-access-persistent.log -> 3xipl-ap.log All previous logs have .prev suffix --- web/job/check_client_ip_job.go | 3 +++ web/job/clear_logs_job.go | 31 +++++++++++++++++++++++++++++-- xray/process.go | 14 +++++++++++++- 3 files changed, 45 insertions(+), 3 deletions(-) diff --git a/web/job/check_client_ip_job.go b/web/job/check_client_ip_job.go index b15473c5..65257170 100644 --- a/web/job/check_client_ip_job.go +++ b/web/job/check_client_ip_job.go @@ -22,8 +22,11 @@ var job *CheckClientIpJob var disAllowedIps []string var ipFiles = []string{ xray.GetIPLimitLogPath(), +xray.GetIPLimitPrevLogPath(), xray.GetIPLimitBannedLogPath(), +xray.GetIPLimitBannedPrevLogPath(), xray.GetAccessPersistentLogPath(), +xray.GetAccessPersistentPrevLogPath(), } func NewCheckClientIpJob() *CheckClientIpJob { diff --git a/web/job/clear_logs_job.go b/web/job/clear_logs_job.go index 34f13eaa..5ceb5a75 100644 --- a/web/job/clear_logs_job.go +++ b/web/job/clear_logs_job.go @@ -15,10 +15,37 @@ func NewClearLogsJob() *ClearLogsJob { // Here Run is an interface method of the Job interface func (j *ClearLogsJob) Run() { logFiles := []string{xray.GetIPLimitLogPath(), xray.GetIPLimitBannedLogPath(), xray.GetAccessPersistentLogPath()} + logFilesPrev := []string{xray.GetIPLimitPrevLogPath(), xray.GetIPLimitBannedPrevLogPath(), xray.GetAccessPersistentPrevLogPath()} - // clear log files + // clear old previous logs + for i := 0; i < len(logFilesPrev); i++ { + if err := os.Truncate(logFilesPrev[i], 0); err != nil { + logger.Warning("clear logs job err:", err) + } + } + + // clear log files and copy to previous logs for i := 0; i < len(logFiles); i++ { - if err := os.Truncate(logFiles[i], 0); err != nil { + + // copy to previous logs + logFilePrev, err := os.OpenFile(logFilesPrev[i], os.O_CREATE|os.O_APPEND|os.O_RDWR, 0644) + if err != nil { + logger.Warning("clear logs job err:", err) + } + + logFile, err := os.ReadFile(logFiles[i]) + if err != nil { + logger.Warning("clear logs job err:", err) + } + + _, err = logFilePrev.Write(logFile) + if err != nil { + logger.Warning("clear logs job err:", err) + } + defer logFilePrev.Close() + + err = os.Truncate(logFiles[i], 0) + if err != nil { logger.Warning("clear logs job err:", err) } } diff --git a/xray/process.go b/xray/process.go index 9289362f..093cf69d 100644 --- a/xray/process.go +++ b/xray/process.go @@ -41,12 +41,24 @@ func GetIPLimitLogPath() string { return config.GetLogFolder() + "/3xipl.log" } +func GetIPLimitPrevLogPath() string { + return config.GetLogFolder() + "/3xipl.prev.log" +} + func GetIPLimitBannedLogPath() string { return config.GetLogFolder() + "/3xipl-banned.log" } +func GetIPLimitBannedPrevLogPath() string { + return config.GetLogFolder() + "/3xipl-banned.prev.log" +} + func GetAccessPersistentLogPath() string { - return config.GetLogFolder() + "/3xipl-access-persistent.log" + return config.GetLogFolder() + "/3xipl-ap.log" +} + +func GetAccessPersistentPrevLogPath() string { + return config.GetLogFolder() + "/3xipl-ap.prev.log" } func GetAccessLogPath() string {