3x-ui/logger/logger.go

147 lines
3.9 KiB
Go
Raw Normal View History

2025-09-20 07:35:50 +00:00
// Package logger provides logging functionality for the 3x-ui panel with
// buffered log storage and multiple log levels.
2023-02-09 19:18:06 +00:00
package logger
import (
"fmt"
2023-02-09 19:18:06 +00:00
"os"
"time"
2023-04-27 20:45:06 +00:00
"github.com/op/go-logging"
2023-02-09 19:18:06 +00:00
)
var (
2025-09-20 07:35:50 +00:00
logger *logging.Logger
// addToBuffer appends a log entry into the in-memory ring buffer used for
// retrieving recent logs via the web UI. It keeps the buffer bounded to avoid
// uncontrolled growth.
logBuffer []struct {
time string
level logging.Level
log string
}
)
2023-02-09 19:18:06 +00:00
func init() {
InitLogger(logging.INFO)
}
2025-09-20 07:35:50 +00:00
// InitLogger initializes the logger with the specified logging level.
2023-02-09 19:18:06 +00:00
func InitLogger(level logging.Level) {
newLogger := logging.MustGetLogger("x-ui")
var err error
var backend logging.Backend
var format logging.Formatter
ppid := os.Getppid()
backend, err = logging.NewSyslogBackend("")
if err != nil {
println(err)
backend = logging.NewLogBackend(os.Stderr, "", 0)
}
if ppid > 0 && err != nil {
format = logging.MustStringFormatter(`%{time:2006/01/02 15:04:05} %{level} - %{message}`)
} else {
format = logging.MustStringFormatter(`%{level} - %{message}`)
2023-05-22 23:13:15 +00:00
}
2023-02-09 19:18:06 +00:00
backendFormatter := logging.NewBackendFormatter(backend, format)
backendLeveled := logging.AddModuleLevel(backendFormatter)
backendLeveled.SetLevel(level, "x-ui")
newLogger.SetBackend(backendLeveled)
2023-02-09 19:18:06 +00:00
logger = newLogger
}
2025-09-20 07:35:50 +00:00
// Debug logs a debug message and adds it to the log buffer.
func Debug(args ...any) {
logger.Debug(args...)
addToBuffer("DEBUG", fmt.Sprint(args...))
2023-02-09 19:18:06 +00:00
}
2025-09-20 07:35:50 +00:00
// Debugf logs a formatted debug message and adds it to the log buffer.
func Debugf(format string, args ...any) {
logger.Debugf(format, args...)
addToBuffer("DEBUG", fmt.Sprintf(format, args...))
2023-02-09 19:18:06 +00:00
}
2025-09-20 07:35:50 +00:00
// Info logs an info message and adds it to the log buffer.
func Info(args ...any) {
logger.Info(args...)
addToBuffer("INFO", fmt.Sprint(args...))
2023-02-09 19:18:06 +00:00
}
2025-09-20 07:35:50 +00:00
// Infof logs a formatted info message and adds it to the log buffer.
func Infof(format string, args ...any) {
logger.Infof(format, args...)
addToBuffer("INFO", fmt.Sprintf(format, args...))
2023-02-09 19:18:06 +00:00
}
2025-09-20 07:35:50 +00:00
// Notice logs a notice message and adds it to the log buffer.
func Notice(args ...any) {
logger.Notice(args...)
addToBuffer("NOTICE", fmt.Sprint(args...))
}
2025-09-20 07:35:50 +00:00
// Noticef logs a formatted notice message and adds it to the log buffer.
func Noticef(format string, args ...any) {
logger.Noticef(format, args...)
addToBuffer("NOTICE", fmt.Sprintf(format, args...))
}
2025-09-20 07:35:50 +00:00
// Warning logs a warning message and adds it to the log buffer.
func Warning(args ...any) {
logger.Warning(args...)
addToBuffer("WARNING", fmt.Sprint(args...))
2023-02-09 19:18:06 +00:00
}
2025-09-20 07:35:50 +00:00
// Warningf logs a formatted warning message and adds it to the log buffer.
func Warningf(format string, args ...any) {
logger.Warningf(format, args...)
addToBuffer("WARNING", fmt.Sprintf(format, args...))
2023-02-09 19:18:06 +00:00
}
2025-09-20 07:35:50 +00:00
// Error logs an error message and adds it to the log buffer.
func Error(args ...any) {
logger.Error(args...)
addToBuffer("ERROR", fmt.Sprint(args...))
2023-02-09 19:18:06 +00:00
}
2025-09-20 07:35:50 +00:00
// Errorf logs a formatted error message and adds it to the log buffer.
func Errorf(format string, args ...any) {
logger.Errorf(format, args...)
addToBuffer("ERROR", fmt.Sprintf(format, args...))
2023-02-09 19:18:06 +00:00
}
2023-06-16 14:55:33 +00:00
func addToBuffer(level string, newLog string) {
t := time.Now()
if len(logBuffer) >= 10240 {
logBuffer = logBuffer[1:]
2023-06-16 14:55:33 +00:00
}
logLevel, _ := logging.LogLevel(level)
logBuffer = append(logBuffer, struct {
time string
level logging.Level
log string
}{
time: t.Format("2006/01/02 15:04:05"),
level: logLevel,
log: newLog,
})
2023-06-16 14:55:33 +00:00
}
2025-09-20 07:35:50 +00:00
// GetLogs retrieves up to c log entries from the buffer that are at or below the specified level.
func GetLogs(c int, level string) []string {
var output []string
logLevel, _ := logging.LogLevel(level)
for i := len(logBuffer) - 1; i >= 0 && len(output) <= c; i-- {
if logBuffer[i].level <= logLevel {
output = append(output, fmt.Sprintf("%s %s - %s", logBuffer[i].time, logBuffer[i].level, logBuffer[i].log))
}
2023-06-16 14:55:33 +00:00
}
return output
2023-06-16 14:55:33 +00:00
}