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
+10 -12
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
time string logBuffer []struct {
level logging.Level time string
log string level logging.Level
} 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}`)
+20 -14
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)
var lines []string
if service == "sing-box" { if service == "s-ui" {
cmdArgs := []string{"journalctl", "-u", service, "--no-pager", "-n", count, "-p", level} return logger.GetLogs(c, level)
// Run the command
cmd := exec.Command(cmdArgs[0], cmdArgs[1:]...)
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
return []string{"Failed to run journalctl command!"}
}
lines = strings.Split(out.String(), "\n")
} else {
lines = logger.GetLogs(c, level)
} }
ppid := os.Getppid()
var lines []string
var cmdArgs []string
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
cmd := exec.Command(cmdArgs[0], cmdArgs[1:]...)
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
return []string{"Failed to get logs!", err.Error()}
}
lines = strings.Split(out.String(), "\n")
return lines return lines
} }