Skip to content

Commit

Permalink
feat: allow logging to stdout, stderr
Browse files Browse the repository at this point in the history
The --log option accepts "stdout", "stderr" as filenames. This prevents
rest-server from opending these files, it simply writs the the STDOUT or
STDERR stream provided by the caller.

**BREAKING** in case use really used "stdout", "stderr" to specify file
names, you'll need to update your rest-server invocation to use
"./stdout", "./stderr".
  • Loading branch information
HeikoSchlittermann committed Mar 23, 2023
1 parent ea461a2 commit 9345bc1
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
13 changes: 13 additions & 0 deletions changelog/unreleased/pull-217
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Feature: Allos "stdout", "stderr" as filenames 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
2 changes: 1 addition & 1 deletion cmd/rest-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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` (\"stdout\", \"stderr\" can also be used)")
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")
Expand Down
17 changes: 14 additions & 3 deletions mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package restserver

import (
"fmt"
"io"
"log"
"net/http"
"os"
Expand All @@ -21,9 +22,19 @@ 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

switch s.Log {
case "stdout":
accessLog = os.Stdout
case "stderr":
accessLog = os.Stderr
default:
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)
Expand Down

0 comments on commit 9345bc1

Please sign in to comment.