From 9f074d8b3a5868eca3b4f7504c951aaa20231d3f Mon Sep 17 00:00:00 2001 From: "Heiko Schlittermann (HS12-RIPE)" Date: Thu, 23 Mar 2023 19:19:18 +0100 Subject: [PATCH] feat: allow logging to stdout The --log option accepts "-" as filename. This prevents rest-server from opening the log file, it simply writes to the STDOUT stream provided by the caller. **BREAKING** in case use really used "-" to specify a file named "-" you'll need to update your rest-server invocation to use "./-". --- changelog/unreleased/pull-217 | 13 +++++++++++++ cmd/rest-server/main.go | 2 +- mux.go | 14 +++++++++++--- 3 files changed, 25 insertions(+), 4 deletions(-) create mode 100644 changelog/unreleased/pull-217 diff --git a/changelog/unreleased/pull-217 b/changelog/unreleased/pull-217 new file mode 100644 index 0000000..902d09c --- /dev/null +++ b/changelog/unreleased/pull-217 @@ -0,0 +1,13 @@ +Feature: Allows "-" as filename for the `--log` option. + +When (e.g. for debugging purpose) the rest server is invoked like + + sudo -u restic rest-server … --log /dev/stdout + +it tries to open `/dev/stdout` (O_CREATE, O_WRITE, O_APPEND). This +operation fails, as in the above invocation, `/dev/stdout` is owned by +the caller (here: root) and only writable for the caller. Subprocesses +get just the filedescriptor. Using `/proc/self/fd/1` didn't work either, +for the same reasons. + +https://github.com/restic/rest-server/pull/217 diff --git a/cmd/rest-server/main.go b/cmd/rest-server/main.go index e1baa39..ccace4d 100644 --- a/cmd/rest-server/main.go +++ b/cmd/rest-server/main.go @@ -46,7 +46,7 @@ func init() { flags.StringVar(&cpuProfile, "cpu-profile", cpuProfile, "write CPU profile to file") flags.BoolVar(&server.Debug, "debug", server.Debug, "output debug messages") flags.StringVar(&server.Listen, "listen", server.Listen, "listen address") - flags.StringVar(&server.Log, "log", server.Log, "write HTTP requests in the combined log format to the specified `filename`") + flags.StringVar(&server.Log, "log", server.Log, "write HTTP requests in the combined log format to the specified `filename` (use \"-\" for logging to stdout)") flags.Int64Var(&server.MaxRepoSize, "max-size", server.MaxRepoSize, "the maximum size of the repository in bytes") flags.StringVar(&server.Path, "path", server.Path, "data directory") flags.BoolVar(&server.TLS, "tls", server.TLS, "turn on TLS support") diff --git a/mux.go b/mux.go index 43e73b8..77fcdb4 100644 --- a/mux.go +++ b/mux.go @@ -2,6 +2,7 @@ package restserver import ( "fmt" + "io" "log" "net/http" "os" @@ -21,9 +22,16 @@ func (s *Server) debugHandler(next http.Handler) http.Handler { } func (s *Server) logHandler(next http.Handler) http.Handler { - accessLog, err := os.OpenFile(s.Log, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644) - if err != nil { - log.Fatalf("error: %v", err) + var accessLog io.Writer + + if s.Log == "-" { + accessLog = os.Stdout + } else { + var err error + accessLog, err = os.OpenFile(s.Log, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644) + if err != nil { + log.Fatalf("error: %v", err) + } } return handlers.CombinedLoggingHandler(accessLog, next)