♻️ Refactored warden logging
This commit is contained in:
parent
ee8b7e5660
commit
1f037113bf
@ -2,13 +2,17 @@ package warden
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/rs/zerolog/log"
|
"io"
|
||||||
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/rs/zerolog/log"
|
||||||
|
"github.com/spf13/viper"
|
||||||
|
"gopkg.in/natefinch/lumberjack.v2"
|
||||||
|
|
||||||
"github.com/samber/lo"
|
"github.com/samber/lo"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -43,11 +47,12 @@ const (
|
|||||||
|
|
||||||
type AppInstance struct {
|
type AppInstance struct {
|
||||||
Manifest Application `json:"manifest"`
|
Manifest Application `json:"manifest"`
|
||||||
|
Status AppStatus `json:"status"`
|
||||||
|
|
||||||
Cmd *exec.Cmd `json:"-"`
|
Cmd *exec.Cmd `json:"-"`
|
||||||
Logger strings.Builder `json:"-"`
|
|
||||||
|
|
||||||
Status AppStatus `json:"status"`
|
LogPath string `json:"-"`
|
||||||
|
Logger *lumberjack.Logger `json:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v *AppInstance) Wake() error {
|
func (v *AppInstance) Wake() error {
|
||||||
@ -76,8 +81,21 @@ func (v *AppInstance) Start() error {
|
|||||||
v.Cmd = exec.Command(manifest.Command[0], manifest.Command[1:]...)
|
v.Cmd = exec.Command(manifest.Command[0], manifest.Command[1:]...)
|
||||||
v.Cmd.Dir = filepath.Join(manifest.Workdir)
|
v.Cmd.Dir = filepath.Join(manifest.Workdir)
|
||||||
v.Cmd.Env = append(v.Cmd.Env, manifest.Environment...)
|
v.Cmd.Env = append(v.Cmd.Env, manifest.Environment...)
|
||||||
v.Cmd.Stdout = &v.Logger
|
|
||||||
v.Cmd.Stderr = &v.Logger
|
logBasePath := viper.GetString("logging.warden_apps")
|
||||||
|
logPath := filepath.Join(logBasePath, fmt.Sprintf("%s.log", manifest.ID))
|
||||||
|
|
||||||
|
v.LogPath = logPath
|
||||||
|
v.Logger = &lumberjack.Logger{
|
||||||
|
Filename: v.LogPath,
|
||||||
|
MaxSize: 10,
|
||||||
|
MaxBackups: 3,
|
||||||
|
MaxAge: 30,
|
||||||
|
Compress: true,
|
||||||
|
}
|
||||||
|
|
||||||
|
v.Cmd.Stdout = v.Logger
|
||||||
|
v.Cmd.Stderr = v.Logger
|
||||||
|
|
||||||
// Monitor
|
// Monitor
|
||||||
go func() {
|
go func() {
|
||||||
@ -117,9 +135,15 @@ func (v *AppInstance) Stop() error {
|
|||||||
|
|
||||||
v.Cmd = nil
|
v.Cmd = nil
|
||||||
v.Status = AppExited
|
v.Status = AppExited
|
||||||
|
v.Logger.Close()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v *AppInstance) Logs() string {
|
func (v *AppInstance) Logs() string {
|
||||||
return v.Logger.String()
|
file, err := os.Open(v.LogPath)
|
||||||
|
if err != nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
raw, _ := io.ReadAll(file)
|
||||||
|
return string(raw)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user