-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
log_controller.go
105 lines (86 loc) · 2.74 KB
/
log_controller.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
package web
import (
"fmt"
"net/http"
"strconv"
"github.com/gin-gonic/gin"
"go.uber.org/zap/zapcore"
"github.com/smartcontractkit/chainlink/v2/core/logger/audit"
"github.com/smartcontractkit/chainlink/v2/core/services/chainlink"
"github.com/smartcontractkit/chainlink/v2/core/web/presenters"
)
// LogController manages the logger config
type LogController struct {
App chainlink.Application
}
type LogPatchRequest struct {
Level string `json:"level"`
SqlEnabled *bool `json:"sqlEnabled"`
}
// Get retrieves the current log config settings
func (cc *LogController) Get(c *gin.Context) {
var svcs, lvls []string
svcs = append(svcs, "Global")
lvls = append(lvls, cc.App.GetConfig().Log().Level().String())
svcs = append(svcs, "IsSqlEnabled")
lvls = append(lvls, strconv.FormatBool(cc.App.GetConfig().Database().LogSQL()))
response := &presenters.ServiceLogConfigResource{
JAID: presenters.JAID{
ID: "log",
},
ServiceName: svcs,
LogLevel: lvls,
DefaultLogLevel: cc.App.GetConfig().Log().DefaultLevel().String(),
}
jsonAPIResponse(c, response, "log")
}
// Patch sets a log level and enables sql logging for the logger
func (cc *LogController) Patch(c *gin.Context) {
request := &LogPatchRequest{}
if err := c.ShouldBindJSON(request); err != nil {
jsonAPIError(c, http.StatusUnprocessableEntity, err)
return
}
// Build log config response
var svcs, lvls []string
// Validate request params
if request.Level == "" && request.SqlEnabled == nil {
jsonAPIError(c, http.StatusBadRequest, fmt.Errorf("please check request params, no params configured"))
return
}
if request.Level != "" {
var ll zapcore.Level
err := ll.UnmarshalText([]byte(request.Level))
if err != nil {
jsonAPIError(c, http.StatusBadRequest, err)
return
}
if err := cc.App.SetLogLevel(ll); err != nil {
jsonAPIError(c, http.StatusInternalServerError, err)
return
}
}
svcs = append(svcs, "Global")
lvls = append(lvls, cc.App.GetConfig().Log().Level().String())
if request.SqlEnabled != nil {
cc.App.GetConfig().SetLogSQL(*request.SqlEnabled)
}
svcs = append(svcs, "IsSqlEnabled")
lvls = append(lvls, strconv.FormatBool(cc.App.GetConfig().Database().LogSQL()))
response := &presenters.ServiceLogConfigResource{
JAID: presenters.JAID{
ID: "log",
},
ServiceName: svcs,
LogLevel: lvls,
}
cc.App.GetAuditLogger().Audit(audit.GlobalLogLevelSet, map[string]interface{}{"logLevel": request.Level})
if request.Level == "debug" {
if request.SqlEnabled != nil && *request.SqlEnabled {
cc.App.GetAuditLogger().Audit(audit.ConfigSqlLoggingEnabled, map[string]interface{}{})
} else {
cc.App.GetAuditLogger().Audit(audit.ConfigSqlLoggingDisabled, map[string]interface{}{})
}
}
jsonAPIResponse(c, response, "log")
}