From 3a1be63a401c5688e57430474971d66dd0d577f9 Mon Sep 17 00:00:00 2001 From: somebodywashere <68244480+somebodywashere@users.noreply.github.com> Date: Sat, 10 Feb 2024 13:40:39 +0300 Subject: [PATCH] some log changes (#1789) * some logs changes * removed some empty lines --- web/job/check_client_ip_job.go | 36 ++++++++++++++----------------- web/job/clear_logs_job.go | 39 +++++++++++++++++----------------- xray/process.go | 4 ---- 3 files changed, 36 insertions(+), 43 deletions(-) diff --git a/web/job/check_client_ip_job.go b/web/job/check_client_ip_job.go index 2cf72555ff..51a09db868 100644 --- a/web/job/check_client_ip_job.go +++ b/web/job/check_client_ip_job.go @@ -25,7 +25,6 @@ type CheckClientIpJob struct { var job *CheckClientIpJob var ipFiles = []string{ xray.GetIPLimitLogPath(), - xray.GetIPLimitPrevLogPath(), xray.GetIPLimitBannedLogPath(), xray.GetIPLimitBannedPrevLogPath(), xray.GetAccessPersistentLogPath(), @@ -66,7 +65,21 @@ func (j *CheckClientIpJob) clearLogTime() { func (j *CheckClientIpJob) clearAccessLog() { accessLogPath := xray.GetAccessLogPath() - err := os.Truncate(accessLogPath, 0) + logAccessP, err := os.OpenFile(xray.GetAccessPersistentLogPath(), os.O_CREATE|os.O_APPEND|os.O_RDWR, 0644) + j.checkError(err) + defer logAccessP.Close() + + // reopen the access log file for reading + file, err := os.Open(accessLogPath) + j.checkError(err) + defer file.Close() + + // copy access log content to persistent file + _, err = io.Copy(logAccessP, file) + j.checkError(err) + + // clean access log + err = os.Truncate(accessLogPath, 0) j.checkError(err) } @@ -177,24 +190,7 @@ func (j *CheckClientIpJob) processLogFile() { time.Sleep(time.Second * 2) if shouldCleanLog { - // copy access log to persistent file - logAccessP, err := os.OpenFile(xray.GetAccessPersistentLogPath(), os.O_CREATE|os.O_APPEND|os.O_RDWR, 0644) - j.checkError(err) - defer logAccessP.Close() - - // reopen the access log file for reading - file, err := os.Open(accessLogPath) - j.checkError(err) - defer file.Close() - - // copy access log content to persistent file - _, err = io.Copy(logAccessP, file) - j.checkError(err) - - // clean access log - if err := os.Truncate(xray.GetAccessLogPath(), 0); err != nil { - j.checkError(err) - } + j.clearAccessLog() } } diff --git a/web/job/clear_logs_job.go b/web/job/clear_logs_job.go index 5ceb5a7553..c6312006a9 100644 --- a/web/job/clear_logs_job.go +++ b/web/job/clear_logs_job.go @@ -15,8 +15,8 @@ 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()} - + logFilesPrev := []string{xray.GetIPLimitBannedPrevLogPath(), xray.GetAccessPersistentPrevLogPath()} + // clear old previous logs for i := 0; i < len(logFilesPrev); i++ { if err := os.Truncate(logFilesPrev[i], 0); err != nil { @@ -26,25 +26,26 @@ func (j *ClearLogsJob) Run() { // clear log files and copy to previous logs for i := 0; i < len(logFiles); i++ { - - // 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) + if i > 0 { + // copy to previous logs + logFilePrev, err := os.OpenFile(logFilesPrev[i-1], 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() } - defer logFilePrev.Close() - err = os.Truncate(logFiles[i], 0) + 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 093cf69d62..e37a0649de 100644 --- a/xray/process.go +++ b/xray/process.go @@ -41,10 +41,6 @@ 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" }