Code refactoring for logs to file

This commit is contained in:
Alex X
2025-02-24 15:21:37 +03:00
parent ac798d9d6d
commit 7fd0ec8ce6
2 changed files with 6 additions and 14 deletions
-1
View File
@@ -1213,7 +1213,6 @@ log:
rtsp: warn rtsp: warn
streams: error streams: error
webrtc: fatal webrtc: fatal
output: stdout # Available output options are: stdout, stderr, or a file path.
``` ```
## Security ## Security
+6 -13
View File
@@ -3,6 +3,7 @@ package app
import ( import (
"io" "io"
"os" "os"
"strings"
"github.com/mattn/go-isatty" "github.com/mattn/go-isatty"
"github.com/rs/zerolog" "github.com/rs/zerolog"
@@ -19,25 +20,17 @@ var MemoryLog = newBuffer(16)
func NewLogger(config map[string]string) zerolog.Logger { func NewLogger(config map[string]string) zerolog.Logger {
var writer io.Writer var writer io.Writer
switch config["output"] { switch output, path, _ := strings.Cut(config["output"], ":"); output {
case "stderr": case "stderr":
writer = os.Stderr writer = os.Stderr
case "stdout": case "stdout":
writer = os.Stdout writer = os.Stdout
case "file": case "file":
filePath := config["file"] if path == "" {
if filePath == "" { path = "go2rtc.log"
filePath = "go2rtc.log"
} }
file, err := os.OpenFile(filePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) // if fail - only MemoryLog will be available
if err != nil { writer, _ = os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
os.Stdout.WriteString("Error: Failed to open log file: " + err.Error() + ". Log output is set to stdout now.\n")
writer = os.Stdout
} else {
writer = file
}
default:
writer = os.Stdout
} }
timeFormat := config["time"] timeFormat := config["time"]