fix container logger #176

This commit is contained in:
Alireza Ahmadi
2024-07-18 23:12:31 +02:00
parent d70006cd91
commit 1631ac0c30
2 changed files with 30 additions and 26 deletions
+7 -9
View File
@@ -8,16 +8,14 @@ import (
"github.com/op/go-logging" "github.com/op/go-logging"
) )
var logger *logging.Logger var (
var logBuffer []struct { logger *logging.Logger
logBuffer []struct {
time string time string
level logging.Level level logging.Level
log string log string
} }
)
func init() {
InitLogger(logging.INFO)
}
func InitLogger(level logging.Level) { func InitLogger(level logging.Level) {
newLogger := logging.MustGetLogger("s-ui") newLogger := logging.MustGetLogger("s-ui")
@@ -28,10 +26,10 @@ func InitLogger(level logging.Level) {
backend, err = logging.NewSyslogBackend("") backend, err = logging.NewSyslogBackend("")
if err != nil { if err != nil {
println(err) println("Unable to use syslog: " + err.Error())
backend = logging.NewLogBackend(os.Stderr, "", 0) backend = logging.NewLogBackend(os.Stderr, "", 0)
} }
if ppid > 0 && err != nil { if ppid > 1 && err != nil {
format = logging.MustStringFormatter(`%{time:2006/01/02 15:04:05} %{level} - %{message}`) format = logging.MustStringFormatter(`%{time:2006/01/02 15:04:05} %{level} - %{message}`)
} else { } else {
format = logging.MustStringFormatter(`%{level} - %{message}`) format = logging.MustStringFormatter(`%{level} - %{message}`)
+12 -6
View File
@@ -141,21 +141,27 @@ func (s *ServerService) GetSystemInfo() map[string]interface{} {
func (s *ServerService) GetLogs(service string, count string, level string) []string { func (s *ServerService) GetLogs(service string, count string, level string) []string {
c, _ := strconv.Atoi(count) c, _ := strconv.Atoi(count)
if service == "s-ui" {
return logger.GetLogs(c, level)
}
ppid := os.Getppid()
var lines []string var lines []string
if service == "sing-box" { var cmdArgs []string
cmdArgs := []string{"journalctl", "-u", service, "--no-pager", "-n", count, "-p", level} if ppid > 1 {
cmdArgs = []string{"journalctl", "-u", service, "--no-pager", "-n", count, "-p", level}
} else {
cmdArgs = []string{"tail", "/logs/" + service + ".log", "-n", count}
}
// Run the command // Run the command
cmd := exec.Command(cmdArgs[0], cmdArgs[1:]...) cmd := exec.Command(cmdArgs[0], cmdArgs[1:]...)
var out bytes.Buffer var out bytes.Buffer
cmd.Stdout = &out cmd.Stdout = &out
err := cmd.Run() err := cmd.Run()
if err != nil { if err != nil {
return []string{"Failed to run journalctl command!"} return []string{"Failed to get logs!", err.Error()}
} }
lines = strings.Split(out.String(), "\n") lines = strings.Split(out.String(), "\n")
} else {
lines = logger.GetLogs(c, level)
}
return lines return lines
} }