diff --git a/CHANGELOG.md b/CHANGELOG.md index b891aa0..4997889 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ The following emojis are used to highlight certain changes: ### Added - Simple end-to-end test to check that trustless-gateway-domains are set correctly. +- HTTP API to dynamically list logging subsystems and modify logging levels for subsystems. ### Changed diff --git a/README.md b/README.md index 7435a6c..9fd6b75 100644 --- a/README.md +++ b/README.md @@ -134,6 +134,14 @@ Example cURL commmand to run GC: curl -v --data '{"BytesToFree": 1099511627776}' http://127.0.0.1:8091/mgr/gc +## Logging + +While the logging can be controlled via [environment variable](./docs/environment-variables.md#logging) it is also +possible to dynamically modify the logging at runtime. + +- `http://$RAINBOW_CTL_LISTEN_ADDRESS/mgr/log/level?subsystem=&level=` will set the logging level for a subsystem +- `http://$RAINBOW_CTL_LISTEN_ADDRESS/mgr/log/ls` will return a comma separated list of available logging subsystems + ## Deployment Suggested method for self-hosting is to run a [prebuilt Docker image](#docker). diff --git a/handlers.go b/handlers.go index 05ba9c8..49efa61 100644 --- a/handlers.go +++ b/handlers.go @@ -8,9 +8,11 @@ import ( "os" "runtime" "strconv" + "strings" "github.com/ipfs/boxo/blockstore" leveldb "github.com/ipfs/go-ds-leveldb" + "github.com/ipfs/go-log/v2" _ "embed" _ "net/http/pprof" @@ -47,6 +49,30 @@ func makeMetricsAndDebuggingHandler() *http.ServeMux { return mux } +func addLogHandlers(mux *http.ServeMux) { + mux.HandleFunc("/mgr/log/level", func(w http.ResponseWriter, r *http.Request) { + defer r.Body.Close() + + q := r.URL.Query() + subsystem := q.Get("subsystem") + level := q.Get("level") + + if subsystem == "" || level == "" { + http.Error(w, fmt.Sprintf("both subsystem and level must be passed"), http.StatusBadRequest) + return + } + + if err := log.SetLogLevel(subsystem, level); err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + }) + mux.HandleFunc("/mgr/log/ls", func(w http.ResponseWriter, r *http.Request) { + _, _ = w.Write([]byte(strings.Join(log.GetSubsystems(), ","))) + return + }) +} + func GCHandler(gnd *Node) func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) { defer r.Body.Close() diff --git a/main.go b/main.go index 443a41e..b9c0e59 100644 --- a/main.go +++ b/main.go @@ -505,6 +505,7 @@ share the same seed as long as the indexes are different. apiMux := makeMetricsAndDebuggingHandler() apiMux.HandleFunc("/mgr/gc", GCHandler(gnd)) + addLogHandlers(apiMux) apiSrv := &http.Server{ Addr: ctlListen,