mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2025-10-13 19:49:12 +00:00
feat: add backend for file logger
This commit is contained in:
parent
b578a33518
commit
9181ca8a9d
1 changed files with 56 additions and 18 deletions
|
@ -5,13 +5,16 @@ package logger
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/mhsanaei/3x-ui/v2/config"
|
||||||
"github.com/op/go-logging"
|
"github.com/op/go-logging"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
logger *logging.Logger
|
logger *logging.Logger
|
||||||
|
logFile *os.File
|
||||||
|
|
||||||
// addToBuffer appends a log entry into the in-memory ring buffer used for
|
// 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
|
// retrieving recent logs via the web UI. It keeps the buffer bounded to avoid
|
||||||
|
@ -27,33 +30,68 @@ func init() {
|
||||||
InitLogger(logging.INFO)
|
InitLogger(logging.INFO)
|
||||||
}
|
}
|
||||||
|
|
||||||
// InitLogger initializes the logger with the specified logging level.
|
// InitLogger initializes the logger backends with the specified logging level.
|
||||||
func InitLogger(level logging.Level) {
|
func InitLogger(level logging.Level) {
|
||||||
newLogger := logging.MustGetLogger("x-ui")
|
newLogger := logging.MustGetLogger("x-ui")
|
||||||
var err error
|
backends := make([]logging.Backend, 0, 2)
|
||||||
var backend logging.Backend
|
|
||||||
var format logging.Formatter
|
|
||||||
ppid := os.Getppid()
|
|
||||||
|
|
||||||
backend, err = logging.NewSyslogBackend("")
|
if defaultBackend := initDefaultBackend(); defaultBackend != nil {
|
||||||
if err != nil {
|
backends = append(backends, defaultBackend)
|
||||||
println(err)
|
|
||||||
backend = logging.NewLogBackend(os.Stderr, "", 0)
|
|
||||||
}
|
}
|
||||||
if ppid > 0 && err != nil {
|
if fileBackend := initFileBackend(); fileBackend != nil {
|
||||||
format = logging.MustStringFormatter(`%{time:2006/01/02 15:04:05} %{level} - %{message}`)
|
backends = append(backends, fileBackend)
|
||||||
} else {
|
|
||||||
format = logging.MustStringFormatter(`%{level} - %{message}`)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
backendFormatter := logging.NewBackendFormatter(backend, format)
|
multiBackend := logging.MultiLogger(backends...)
|
||||||
backendLeveled := logging.AddModuleLevel(backendFormatter)
|
multiBackend.SetLevel(level, "x-ui")
|
||||||
backendLeveled.SetLevel(level, "x-ui")
|
|
||||||
newLogger.SetBackend(backendLeveled)
|
|
||||||
|
|
||||||
|
newLogger.SetBackend(multiBackend)
|
||||||
logger = newLogger
|
logger = newLogger
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func initDefaultBackend() logging.Backend {
|
||||||
|
backendSyslog, err := logging.NewSyslogBackend("")
|
||||||
|
var backend logging.Backend = backendSyslog
|
||||||
|
includeTime := false
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintf(os.Stderr, "syslog backend disabled: %v\n", err)
|
||||||
|
ppid := os.Getppid()
|
||||||
|
backend = logging.NewLogBackend(os.Stderr, "", 0)
|
||||||
|
includeTime = ppid > 0
|
||||||
|
}
|
||||||
|
return logging.NewBackendFormatter(backend, newFormatter(includeTime))
|
||||||
|
}
|
||||||
|
|
||||||
|
func initFileBackend() logging.Backend {
|
||||||
|
logDir := config.GetLogFolder()
|
||||||
|
if err := os.MkdirAll(logDir, 0o750); err != nil {
|
||||||
|
fmt.Fprintf(os.Stderr, "failed to create log folder %s: %v\n", logDir, err)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
logPath := filepath.Join(logDir, "3xui.log")
|
||||||
|
file, err := os.OpenFile(logPath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0o660)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintf(os.Stderr, "failed to open log file %s: %v\n", logPath, err)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if logFile != nil {
|
||||||
|
_ = logFile.Close()
|
||||||
|
}
|
||||||
|
logFile = file
|
||||||
|
|
||||||
|
backend := logging.NewLogBackend(file, "", 0)
|
||||||
|
return logging.NewBackendFormatter(backend, newFormatter(true))
|
||||||
|
}
|
||||||
|
|
||||||
|
func newFormatter(withTime bool) logging.Formatter {
|
||||||
|
if withTime {
|
||||||
|
return logging.MustStringFormatter(`%{time:2006/01/02 15:04:05} %{level} - %{message}`)
|
||||||
|
}
|
||||||
|
return logging.MustStringFormatter(`%{level} - %{message}`)
|
||||||
|
}
|
||||||
|
|
||||||
// Debug logs a debug message and adds it to the log buffer.
|
// Debug logs a debug message and adds it to the log buffer.
|
||||||
func Debug(args ...any) {
|
func Debug(args ...any) {
|
||||||
logger.Debug(args...)
|
logger.Debug(args...)
|
||||||
|
|
Loading…
Reference in a new issue