mirror of
https://github.com/alireza0/x-ui.git
synced 2026-03-14 05:23:09 +00:00
[log] combine with xray logs
This commit is contained in:
53
xray/log_writer.go
Normal file
53
xray/log_writer.go
Normal file
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user