[logs] new bug-free log_writer

This commit is contained in:
Alireza Ahmadi
2024-02-13 19:35:06 +01:00
parent 294a3f46a0
commit ee2bbffc8f
2 changed files with 10 additions and 15 deletions

View File

@@ -302,7 +302,7 @@
</a-button> </a-button>
</a-form-item> </a-form-item>
</a-form> </a-form>
<div class="ant-input" style="height: auto; max-height: 500px; overflow: auto;" v-html="logModal.logs"></div> <div class="ant-input" style="height: auto; max-height: 500px; overflow: auto;" v-html="logModal.formattedLogs"></div>
</a-modal> </a-modal>
<a-modal id="backup-modal" v-model="backupModal.visible" :title="backupModal.title" <a-modal id="backup-modal" v-model="backupModal.visible" :title="backupModal.title"
@@ -435,7 +435,7 @@
loading: false, loading: false,
show(logs) { show(logs) {
this.visible = true; this.visible = true;
this.logs = logs || []; this.logs = logs;
this.formattedLogs = this.logs.length > 0 ? this.formatLogs(this.logs) : "No Record..."; this.formattedLogs = this.logs.length > 0 ? this.formatLogs(this.logs) : "No Record...";
}, },
formatLogs(logs) { formatLogs(logs) {

View File

@@ -1,6 +1,7 @@
package xray package xray
import ( import (
"regexp"
"strings" "strings"
"x-ui/logger" "x-ui/logger"
) )
@@ -14,26 +15,20 @@ type LogWriter struct {
} }
func (lw *LogWriter) Write(m []byte) (n int, err error) { func (lw *LogWriter) Write(m []byte) (n int, err error) {
regex := regexp.MustCompile(`^(\d{4}/\d{2}/\d{2} \d{2}:\d{2}:\d{2}) \[([^\]]+)\] (.+)$`)
// Convert the data to a string // Convert the data to a string
message := strings.TrimSpace(string(m)) message := strings.TrimSpace(string(m))
messages := strings.Split(message, "\n") messages := strings.Split(message, "\n")
lw.lastLine = messages[len(messages)-1] lw.lastLine = messages[len(messages)-1]
for _, msg := range messages { for _, msg := range messages {
messageBody := msg matches := regex.FindStringSubmatch(msg)
// Remove timestamp if len(matches) > 3 {
splittedMsg := strings.SplitN(msg, " ", 3) level := matches[2]
if len(splittedMsg) > 2 { msgBody := matches[3]
messageBody = strings.TrimSpace(strings.SplitN(msg, " ", 3)[2])
}
// Find level in []
startIndex := strings.Index(messageBody, "[")
endIndex := strings.Index(messageBody, "]")
if startIndex != -1 && endIndex != -1 && startIndex < endIndex {
level := strings.TrimSpace(messageBody[startIndex+1 : endIndex])
msgBody := "XRAY: " + strings.TrimSpace(messageBody[endIndex+1:])
// Map the level to the appropriate logger function // Map the level to the appropriate logger function
switch level { switch level {