Skip to content

Commit

Permalink
some log changes (MHSanaei#1789)
Browse files Browse the repository at this point in the history
* some logs changes

* removed some empty lines
  • Loading branch information
somebodywashere authored Feb 10, 2024
1 parent 4daaf0a commit 3a1be63
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 43 deletions.
36 changes: 16 additions & 20 deletions web/job/check_client_ip_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ type CheckClientIpJob struct {
var job *CheckClientIpJob
var ipFiles = []string{
xray.GetIPLimitLogPath(),
xray.GetIPLimitPrevLogPath(),
xray.GetIPLimitBannedLogPath(),
xray.GetIPLimitBannedPrevLogPath(),
xray.GetAccessPersistentLogPath(),
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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()
}
}

Expand Down
39 changes: 20 additions & 19 deletions web/job/clear_logs_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
}
Expand Down
4 changes: 0 additions & 4 deletions xray/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down

0 comments on commit 3a1be63

Please sign in to comment.