http_handler.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // Copyright (c) 2016 Uber Technologies, Inc.
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4. // of this software and associated documentation files (the "Software"), to deal
  5. // in the Software without restriction, including without limitation the rights
  6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. // copies of the Software, and to permit persons to whom the Software is
  8. // furnished to do so, subject to the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be included in
  11. // all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. // THE SOFTWARE.
  20. package zap
  21. import (
  22. "encoding/json"
  23. "fmt"
  24. "net/http"
  25. "go.uber.org/zap/zapcore"
  26. )
  27. // ServeHTTP is a simple JSON endpoint that can report on or change the current
  28. // logging level.
  29. //
  30. // GET requests return a JSON description of the current logging level. PUT
  31. // requests change the logging level and expect a payload like:
  32. // {"level":"info"}
  33. //
  34. // It's perfectly safe to change the logging level while a program is running.
  35. func (lvl AtomicLevel) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  36. type errorResponse struct {
  37. Error string `json:"error"`
  38. }
  39. type payload struct {
  40. Level *zapcore.Level `json:"level"`
  41. }
  42. enc := json.NewEncoder(w)
  43. switch r.Method {
  44. case http.MethodGet:
  45. current := lvl.Level()
  46. enc.Encode(payload{Level: &current})
  47. case http.MethodPut:
  48. var req payload
  49. if errmess := func() string {
  50. if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
  51. return fmt.Sprintf("Request body must be well-formed JSON: %v", err)
  52. }
  53. if req.Level == nil {
  54. return "Must specify a logging level."
  55. }
  56. return ""
  57. }(); errmess != "" {
  58. w.WriteHeader(http.StatusBadRequest)
  59. enc.Encode(errorResponse{Error: errmess})
  60. return
  61. }
  62. lvl.SetLevel(*req.Level)
  63. enc.Encode(req)
  64. default:
  65. w.WriteHeader(http.StatusMethodNotAllowed)
  66. enc.Encode(errorResponse{
  67. Error: "Only GET and PUT are supported.",
  68. })
  69. }
  70. }