forked from vechain/thor
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ability to change log verbosity while the node is running (vechain#799)
* Replace slog.Level with slog.LevelVar * Add admin server to handle verbosity changes * Update Json logger with LevelVar * Add a GET and POST route * Fix Metrics and Admin docs * Fix grammar * Always use JSON responses * Revert Metrics docs changes * Update docs/hosting-a-node.md Co-authored-by: Darren Kelly <[email protected]> * Fix linter errors --------- Co-authored-by: Pedro Gomes <[email protected]> Co-authored-by: Darren Kelly <[email protected]>
- Loading branch information
1 parent
8ba7f54
commit c00c21e
Showing
9 changed files
with
242 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
// Copyright (c) 2024 The VeChainThor developers | ||
|
||
// Distributed under the GNU Lesser General Public License v3.0 software license, see the accompanying | ||
// file LICENSE or <https://www.gnu.org/licenses/lgpl-3.0.html> | ||
|
||
package admin | ||
|
||
import ( | ||
"encoding/json" | ||
"log/slog" | ||
"net/http" | ||
|
||
"github.com/gorilla/handlers" | ||
"github.com/gorilla/mux" | ||
"github.com/vechain/thor/v2/log" | ||
) | ||
|
||
type logLevelRequest struct { | ||
Level string `json:"level"` | ||
} | ||
|
||
type logLevelResponse struct { | ||
CurrentLevel string `json:"currentLevel"` | ||
} | ||
|
||
type errorResponse struct { | ||
ErrorCode int `json:"errorCode"` | ||
ErrorMessage string `json:"errorMessage"` | ||
} | ||
|
||
func writeError(w http.ResponseWriter, errCode int, errMsg string) { | ||
w.Header().Set("Content-Type", "application/json") | ||
w.WriteHeader(errCode) | ||
json.NewEncoder(w).Encode(errorResponse{ | ||
ErrorCode: errCode, | ||
ErrorMessage: errMsg, | ||
}) | ||
} | ||
|
||
func getLogLevelHandler(logLevel *slog.LevelVar) http.HandlerFunc { | ||
return func(w http.ResponseWriter, r *http.Request) { | ||
response := logLevelResponse{ | ||
CurrentLevel: logLevel.Level().String(), | ||
} | ||
if err := json.NewEncoder(w).Encode(response); err != nil { | ||
writeError(w, http.StatusInternalServerError, "Failed to encode response") | ||
} | ||
} | ||
} | ||
|
||
func postLogLevelHandler(logLevel *slog.LevelVar) http.HandlerFunc { | ||
return func(w http.ResponseWriter, r *http.Request) { | ||
var req logLevelRequest | ||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil { | ||
writeError(w, http.StatusBadRequest, "Invalid request body") | ||
return | ||
} | ||
|
||
switch req.Level { | ||
case "debug": | ||
logLevel.Set(log.LevelDebug) | ||
case "info": | ||
logLevel.Set(log.LevelInfo) | ||
case "warn": | ||
logLevel.Set(log.LevelWarn) | ||
case "error": | ||
logLevel.Set(log.LevelError) | ||
case "trace": | ||
logLevel.Set(log.LevelTrace) | ||
case "crit": | ||
logLevel.Set(log.LevelCrit) | ||
default: | ||
writeError(w, http.StatusBadRequest, "Invalid verbosity level") | ||
return | ||
} | ||
|
||
w.Header().Set("Content-Type", "application/json") | ||
response := logLevelResponse{ | ||
CurrentLevel: logLevel.Level().String(), | ||
} | ||
w.WriteHeader(http.StatusOK) | ||
json.NewEncoder(w).Encode(response) | ||
} | ||
} | ||
|
||
func logLevelHandler(logLevel *slog.LevelVar) http.HandlerFunc { | ||
return func(w http.ResponseWriter, r *http.Request) { | ||
switch r.Method { | ||
case http.MethodGet: | ||
getLogLevelHandler(logLevel).ServeHTTP(w, r) | ||
case http.MethodPost: | ||
postLogLevelHandler(logLevel).ServeHTTP(w, r) | ||
default: | ||
writeError(w, http.StatusMethodNotAllowed, "method not allowed") | ||
} | ||
} | ||
} | ||
|
||
func HTTPHandler(logLevel *slog.LevelVar) http.Handler { | ||
router := mux.NewRouter() | ||
router.HandleFunc("/admin/loglevel", logLevelHandler(logLevel)) | ||
return handlers.CompressHandler(router) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.