mirror of
https://github.com/restic/rest-server.git
synced 2025-10-19 07:33:21 +00:00
Merge pull request #217 from HeikoSchlittermann/feat-log-to-stdout
feat: allow logging to stdout, stderr
This commit is contained in:
commit
c064e4c1ed
3 changed files with 25 additions and 4 deletions
13
changelog/unreleased/pull-217
Normal file
13
changelog/unreleased/pull-217
Normal file
|
@ -0,0 +1,13 @@
|
|||
Feature: Log to stdout using the `--log -` option
|
||||
|
||||
Logging to stdout was possible using `--log /dev/stdout`. However,
|
||||
when the rest server is run as a different user, for example, using
|
||||
|
||||
`sudo -u restic rest-server [...] --log /dev/stdout`
|
||||
|
||||
this did not work due to permission issues.
|
||||
|
||||
For logging to stdout, the `--log` option now supports the special
|
||||
filename `-` which also works in these cases.
|
||||
|
||||
https://github.com/restic/rest-server/pull/217
|
|
@ -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")
|
||||
|
|
14
mux.go
14
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)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue