Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/MHSanaei/3x-ui.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlireza Ahmadi <alireza7@gmail.com>2023-12-10 15:07:50 +0300
committerAlireza Ahmadi <alireza7@gmail.com>2023-12-10 15:07:50 +0300
commit070fd52d86451fe28ba508b211987adea7accc4f (patch)
tree467295bababb886a9f6c03bc45c991f0c75e0352 /xray/log_writer.go
parent4cb67fd1c3129b83f3750f863caba081453f42b1 (diff)
[logs] combine with xray logs #1300
Diffstat (limited to 'xray/log_writer.go')
-rw-r--r--xray/log_writer.go53
1 files changed, 53 insertions, 0 deletions
diff --git a/xray/log_writer.go b/xray/log_writer.go
new file mode 100644
index 00000000..c2f66e8f
--- /dev/null
+++ b/xray/log_writer.go
@@ -0,0 +1,53 @@
+package xray
+
+import (
+ "strings"
+ "x-ui/logger"
+)
+
+func NewLogWriter() *LogWriter {
+ return &LogWriter{}
+}
+
+type LogWriter struct {
+ lastLine string
+}
+
+func (lw *LogWriter) Write(m []byte) (n int, err error) {
+ // Convert the data to a string
+ message := strings.TrimSpace(string(m))
+ messages := strings.Split(message, "\n")
+ lw.lastLine = messages[len(messages)-1]
+
+ for _, msg := range messages {
+ // Remove timestamp
+ messageBody := strings.TrimSpace(strings.SplitN(msg, " ", 3)[2])
+
+ // Find level in []
+ startIndex := strings.Index(messageBody, "[")
+ endIndex := strings.Index(messageBody, "]")
+ if startIndex != -1 && endIndex != -1 {
+ level := strings.TrimSpace(messageBody[startIndex+1 : endIndex])
+ msgBody := "XRAY: " + strings.TrimSpace(messageBody[endIndex+1:])
+
+ // Map the level to the appropriate logger function
+ switch level {
+ case "Debug":
+ logger.Debug(msgBody)
+ case "Info":
+ logger.Info(msgBody)
+ case "Warning":
+ logger.Warning(msgBody)
+ case "Error":
+ logger.Error(msgBody)
+ default:
+ logger.Debug("XRAY: " + msg)
+ }
+ } else if msg != "" {
+ logger.Debug("XRAY: " + msg)
+ return len(m), nil
+ }
+ }
+
+ return len(m), nil
+}