forked from acorn-io/cmd
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlogserver.go
84 lines (74 loc) · 2.34 KB
/
logserver.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Adapted from https://github.com/rancher/log/blob/52031d45f5fdb71cecb9a314865624b42514dbed/server/server.go
// The only real differences here are that we are using logrus directly instead of a wrapper around it,
// and that we are using an abstract namespace (non-file) socket.
// This is only compatible with our fork of the client: https://github.com/acorn-io/loglevel
package logserver
import (
"fmt"
"net"
"net/http"
"os"
"github.com/sirupsen/logrus"
)
var (
DefaultSocketLocation = "\x00logserver"
)
// Server structure is used to the store backend information
type Server struct {
SocketLocation string
Debug bool
}
// StartServerWithDefaults starts the server with default values. If the ACORN_LOG_LEVEL environment variable is set,
// it will be parsed and used to set the log level.
func StartServerWithDefaults() {
if level, err := logrus.ParseLevel(os.Getenv("ACORN_LOG_LEVEL")); err == nil {
logrus.SetLevel(level)
} else {
logrus.SetLevel(logrus.InfoLevel)
}
s := Server{
SocketLocation: DefaultSocketLocation,
}
s.Start()
}
// Start the server
func (s *Server) Start() {
_ = os.Remove(s.SocketLocation)
go func() {
_ = s.ListenAndServe()
}()
}
// ListenAndServe is used to setup handlers and
// start listening on the specified location
func (s *Server) ListenAndServe() error {
server := http.Server{}
http.HandleFunc("/v1/loglevel", s.loglevel)
socketListener, err := net.Listen("unix", s.SocketLocation)
if err != nil {
logrus.Errorf("Failed to start logserver: %v", err)
return err
}
return server.Serve(socketListener)
}
func (s *Server) loglevel(rw http.ResponseWriter, req *http.Request) {
// curl -X POST -d "level=debug" localhost:12345/v1/loglevel
logrus.Debugf("Received loglevel request")
if req.Method == http.MethodGet {
level := logrus.GetLevel().String()
_, _ = rw.Write([]byte(fmt.Sprintf("%s\n", level)))
}
if req.Method == http.MethodPost {
if err := req.ParseForm(); err != nil {
rw.WriteHeader(http.StatusInternalServerError)
_, _ = rw.Write([]byte(fmt.Sprintf("Failed to parse form: %v\n", err)))
}
level, err := logrus.ParseLevel(req.Form.Get("level"))
if err != nil {
rw.WriteHeader(http.StatusInternalServerError)
_, _ = rw.Write([]byte(fmt.Sprintf("Failed to parse loglevel: %v\n", err)))
} else {
logrus.SetLevel(level)
_, _ = rw.Write([]byte("OK\n"))
}
}
}