feat(settings): make log level configurable from UI (#704)

* feat(settings): set log level

* fix: light theme colors

* fix: light theme colors size unit
This commit is contained in:
ze0s 2023-02-11 22:37:06 +01:00 committed by GitHub
parent 8cb4a0244c
commit ac276868fb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 310 additions and 30 deletions

View file

@ -281,7 +281,9 @@ func (c *AppConfig) UpdateConfig() error {
func (c *AppConfig) processLines(lines []string) []string {
// keep track of not found values to append at bottom
var (
foundLineUpdate = false
foundLineUpdate = false
foundLineLogLevel = false
foundLineLogPath = false
)
for i, line := range lines {
@ -290,6 +292,18 @@ func (c *AppConfig) processLines(lines []string) []string {
lines[i] = fmt.Sprintf("checkForUpdates = %t", c.Config.CheckForUpdates)
foundLineUpdate = true
}
if !foundLineLogLevel && strings.Contains(line, "logLevel =") {
lines[i] = fmt.Sprintf(`logLevel = "%s"`, c.Config.LogLevel)
foundLineLogLevel = true
}
if !foundLineLogPath && strings.Contains(line, "logPath =") {
if c.Config.LogPath == "" {
lines[i] = `#logPath = ""`
} else {
lines[i] = fmt.Sprintf("logPath = \"%s\"", c.Config.LogPath)
}
foundLineLogPath = true
}
}
// append missing vars to bottom
@ -299,5 +313,27 @@ func (c *AppConfig) processLines(lines []string) []string {
lines = append(lines, fmt.Sprintf("checkForUpdates = %t", c.Config.CheckForUpdates))
}
if !foundLineLogLevel {
lines = append(lines, "# Log level")
lines = append(lines, "#")
lines = append(lines, `# Default: "DEBUG"`)
lines = append(lines, "#")
lines = append(lines, `# Options: "ERROR", "DEBUG", "INFO", "WARN", "TRACE"`)
lines = append(lines, "#")
lines = append(lines, fmt.Sprintf(`logLevel = "%s"`, c.Config.LogLevel))
}
if !foundLineLogPath {
lines = append(lines, "# Log Path")
lines = append(lines, "#")
lines = append(lines, "# Optional")
lines = append(lines, "#")
if c.Config.LogPath == "" {
lines = append(lines, `#logPath = ""`)
} else {
lines = append(lines, fmt.Sprintf(`logPath = "%s"`, c.Config.LogPath))
}
}
return lines
}

View file

@ -1,10 +1,11 @@
package config
import (
"reflect"
"sync"
"testing"
"github.com/stretchr/testify/assert"
"github.com/autobrr/autobrr/internal/domain"
)
@ -25,20 +26,20 @@ func TestAppConfig_processLines(t *testing.T) {
{
name: "append missing",
fields: fields{
Config: &domain.Config{CheckForUpdates: true},
Config: &domain.Config{CheckForUpdates: true, LogLevel: "TRACE"},
m: sync.Mutex{},
},
args: args{[]string{}},
want: []string{"# Check for updates", "#", "checkForUpdates = true"},
want: []string{"# Check for updates", "#", "checkForUpdates = true", "# Log level", "#", "# Default: \"DEBUG\"", "#", "# Options: \"ERROR\", \"DEBUG\", \"INFO\", \"WARN\", \"TRACE\"", "#", `logLevel = "TRACE"`, "# Log Path", "#", "# Optional", "#", "#logPath = \"\""},
},
{
name: "update existing",
fields: fields{
Config: &domain.Config{CheckForUpdates: true},
Config: &domain.Config{CheckForUpdates: true, LogLevel: "TRACE"},
m: sync.Mutex{},
},
args: args{[]string{"# Check for updates", "#", "#checkForUpdates = false"}},
want: []string{"# Check for updates", "#", "checkForUpdates = true"},
args: args{[]string{"# Check for updates", "#", "checkForUpdates = false", "# Log level", "#", "# Default: \"DEBUG\"", "#", "# Options: \"ERROR\", \"DEBUG\", \"INFO\", \"WARN\", \"TRACE\"", "#", `logLevel = "TRACE"`, "# Log Path", "#", "# Optional", "#", "#logPath = \"\""}},
want: []string{"# Check for updates", "#", "checkForUpdates = true", "# Log level", "#", "# Default: \"DEBUG\"", "#", "# Options: \"ERROR\", \"DEBUG\", \"INFO\", \"WARN\", \"TRACE\"", "#", `logLevel = "TRACE"`, "# Log Path", "#", "# Optional", "#", "#logPath = \"\""},
},
}
for _, tt := range tests {
@ -47,9 +48,8 @@ func TestAppConfig_processLines(t *testing.T) {
Config: tt.fields.Config,
m: tt.fields.m,
}
if got := c.processLines(tt.args.lines); !reflect.DeepEqual(got, tt.want) {
t.Errorf("processLines() = %v, want %v", got, tt.want)
}
assert.Equalf(t, tt.want, c.processLines(tt.args.lines), tt.name)
})
}
}

View file

@ -16,6 +16,8 @@ type configJson struct {
Port int `json:"port"`
LogLevel string `json:"log_level"`
LogPath string `json:"log_path"`
LogMaxSize int `json:"log_max_size"`
LogMaxBackups int `json:"log_max_backups"`
BaseURL string `json:"base_url"`
CheckForUpdates bool `json:"check_for_updates"`
Version string `json:"version"`
@ -49,6 +51,8 @@ func (h configHandler) getConfig(w http.ResponseWriter, r *http.Request) {
Port: h.cfg.Config.Port,
LogLevel: h.cfg.Config.LogLevel,
LogPath: h.cfg.Config.LogPath,
LogMaxSize: h.cfg.Config.LogMaxSize,
LogMaxBackups: h.cfg.Config.LogMaxBackups,
BaseURL: h.cfg.Config.BaseURL,
CheckForUpdates: h.cfg.Config.CheckForUpdates,
Version: h.server.version,
@ -71,6 +75,14 @@ func (h configHandler) updateConfig(w http.ResponseWriter, r *http.Request) {
h.cfg.Config.CheckForUpdates = *data.CheckForUpdates
}
if data.LogLevel != nil {
h.cfg.Config.LogLevel = *data.LogLevel
}
if data.LogPath != nil {
h.cfg.Config.LogPath = *data.LogPath
}
if err := h.cfg.UpdateConfig(); err != nil {
render.Status(r, http.StatusInternalServerError)
render.JSON(w, r, errorResponse{