forked from ligato/cn-infra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin_impl_log_manager.go
183 lines (155 loc) · 5.17 KB
/
plugin_impl_log_manager.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// Copyright (c) 2017 Cisco and/or its affiliates.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package logmanager
import (
"fmt"
"net/http"
"github.com/gorilla/mux"
"github.com/unrolled/render"
"github.com/ligato/cn-infra/config"
"github.com/ligato/cn-infra/core"
"github.com/ligato/cn-infra/logging"
"github.com/ligato/cn-infra/rpc/rest"
)
// LoggerData encapsulates parameters of a logger represented as strings.
type LoggerData struct {
Logger string `json:"logger"`
Level string `json:"level"`
}
// Variable names in logger registry URLs
const (
loggerVarName = "logger"
levelVarName = "level"
)
// Plugin allows to manage log levels of the loggers using HTTP.
type Plugin struct {
Deps
*Conf
}
// Deps groups dependencies injected into the plugin so that they are
// logically separated from other plugin fields.
type Deps struct {
Log logging.PluginLogger //inject
PluginName core.PluginName //inject
config.PluginConfig //inject
LogRegistry logging.Registry // inject
HTTP rest.HTTPHandlers // inject
}
// NewConf creates default configuration with InfoLevel & empty loggers.
// Suitable also for usage in flavor to programmatically specify default behavior.
func NewConf() *Conf {
return &Conf{
DefaultLevel: "",
Loggers: []ConfLogger{},
}
}
// Conf is a binding that supports to define default log levels for multiple loggers
type Conf struct {
DefaultLevel string
Loggers []ConfLogger
}
// ConfLogger is configuration of a particular logger.
// Currently we support only logger level.
type ConfLogger struct {
Name string
Level string //debug, info, warning, error, fatal, panic
}
// Init does nothing
func (lm *Plugin) Init() error {
if lm.PluginConfig != nil {
if lm.Conf == nil {
lm.Conf = NewConf()
}
_, err := lm.PluginConfig.GetValue(lm.Conf)
if err != nil {
return err
}
lm.Log.Debugf("logs config: %+v", lm.Conf)
if lm.Conf.DefaultLevel != "" {
if err := lm.LogRegistry.SetLevel("default", lm.Conf.DefaultLevel); err != nil {
lm.Log.Warn("setting default log level failed:", err)
} else {
lm.Log.Debugf("default log level to %q", lm.Conf.DefaultLevel)
}
}
// try to set log levels (note, not all of them might exist yet)
for _, cfgLogger := range lm.Conf.Loggers {
if err := lm.LogRegistry.SetLevel(cfgLogger.Name, cfgLogger.Level); err != nil {
//intentionally just log warn & not propagate the error (it is minor thing to interrupt startup)
lm.Log.Warn("setting level failed:", err)
}
}
}
return nil
}
// AfterInit is called at plugin initialization. It register the following handlers:
// - List all registered loggers:
// > curl -X GET http://localhost:<port>/log/list
// - Set log level for a registered logger:
// > curl -X PUT http://localhost:<port>/log/<logger-name>/<log-level>
func (lm *Plugin) AfterInit() error {
if lm.HTTP != nil {
lm.HTTP.RegisterHTTPHandler(fmt.Sprintf("/log/{%s}/{%s:debug|info|warning|error|fatal|panic}",
loggerVarName, levelVarName), lm.logLevelHandler, "PUT")
lm.HTTP.RegisterHTTPHandler("/log/list", lm.listLoggersHandler, "GET")
}
return nil
}
// Close is called at plugin cleanup phase.
func (lm *Plugin) Close() error {
return nil
}
// ListLoggers lists all registered loggers.
func (lm *Plugin) listLoggers() []LoggerData {
loggers := []LoggerData{}
lgs := lm.LogRegistry.ListLoggers()
for lg, lvl := range lgs {
ld := LoggerData{
Logger: lg,
Level: lvl,
}
loggers = append(loggers, ld)
}
return loggers
}
// setLoggerLogLevel modifies the log level of the all loggers in a plugin
func (lm *Plugin) setLoggerLogLevel(name string, level string) error {
lm.Log.Debugf("SetLogLevel name '%s', level '%s'", name, level)
return lm.LogRegistry.SetLevel(name, level)
}
// logLevelHandler processes requests to set log level on loggers in a plugin
func (lm *Plugin) logLevelHandler(formatter *render.Render) http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
lm.Log.Infof("Path: %s", req.URL.Path)
vars := mux.Vars(req)
if vars == nil {
formatter.JSON(w, http.StatusNotFound, struct{}{})
return
}
err := lm.setLoggerLogLevel(vars[loggerVarName], vars[levelVarName])
if err != nil {
formatter.JSON(w, http.StatusNotFound,
struct{ Error string }{err.Error()})
return
}
formatter.JSON(w, http.StatusOK,
LoggerData{Logger: vars[loggerVarName], Level: vars[levelVarName]})
}
}
// listLoggersHandler processes requests to list all registered loggers
func (lm *Plugin) listLoggersHandler(formatter *render.Render) http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
formatter.JSON(w, http.StatusOK, lm.listLoggers())
}
}