1 // Copyright (c) 2016 Uber Technologies, Inc.
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:
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
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
28 "go.uber.org/zap/zapcore"
31 // ServeHTTP is a simple JSON endpoint that can report on or change the current
34 // GET requests return a JSON description of the current logging level. PUT
35 // requests change the logging level and expect a payload like:
38 // It's perfectly safe to change the logging level while a program is running.
39 func (lvl AtomicLevel) ServeHTTP(w http.ResponseWriter, r *http.Request) {
40 type errorResponse struct {
41 Error string `json:"error"`
44 Level *zapcore.Level `json:"level"`
47 enc := json.NewEncoder(w)
52 current := lvl.Level()
53 enc.Encode(payload{Level: ¤t})
58 if errmess := func() string {
59 if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
60 return fmt.Sprintf("Request body must be well-formed JSON: %v", err)
63 return "Must specify a logging level."
67 w.WriteHeader(http.StatusBadRequest)
68 enc.Encode(errorResponse{Error: errmess})
72 lvl.SetLevel(*req.Level)
76 w.WriteHeader(http.StatusMethodNotAllowed)
77 enc.Encode(errorResponse{
78 Error: "Only GET and PUT are supported.",