/
controlloglevel.go
126 lines (93 loc) · 3.89 KB
/
controlloglevel.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
// Copyright 2016-2020 Granitic. All rights reserved.
// Use of this source code is governed by an Apache 2.0 license that can be found in the LICENSE file at the root of this project.
package logger
import (
"fmt"
"github.com/graniticio/granitic/v2/ctl"
"github.com/graniticio/granitic/v2/facility/runtimectl"
"github.com/graniticio/granitic/v2/instance"
"github.com/graniticio/granitic/v2/logging"
"github.com/graniticio/granitic/v2/ws"
"sort"
)
const (
// LogLevelComponentName is the name of the component able to alter log levels at runtime
LogLevelComponentName = instance.FrameworkPrefix + "CommandLogLevel"
llCommandName = "log-level"
llSummary = "Views or sets a specific logging threshold for application or framework components."
llUsage = "log-level [component level] [-fw true]"
llHelp = "With no qualifier, this command shows a list of application components that have a specific logging threshold set. When a " +
"component and a level are specified as qualifiers, the component's logging threshold is set at the specified level."
llHelpTwo = "Valid values for level are ALL, TRACE, DEBUG, INFO, WARN, ERROR, FATAL (case insensitive)."
llHelpThree = "Setting the level to ALL has special behaviour - it efffectively removes the specific logging threshold for the component. The global log threshold will then apply to that component."
llHelpFour = "If the '-fw true' argument is supplied without qualifiers, a list of built-in framework components and their associated log levels will be shown."
)
type logLevelCommand struct {
FrameworkLogger logging.Logger
FrameworkManager *logging.ComponentLoggerManager
ApplicationManager *logging.ComponentLoggerManager
}
func (c *logLevelCommand) ExecuteCommand(qualifiers []string, args map[string]string) (*ctl.CommandOutput, []*ws.CategorisedError) {
if len(qualifiers) == 0 {
return c.showCurrentLevel(args)
}
return c.setLevel(qualifiers)
}
func (c *logLevelCommand) setLevel(qualifiers []string) (*ctl.CommandOutput, []*ws.CategorisedError) {
var err error
var ll logging.LogLevel
if len(qualifiers) < 2 {
return nil, []*ws.CategorisedError{ctl.NewCommandClientError("You must provide a component name and a loging level.")}
}
name := qualifiers[0]
label := qualifiers[1]
if ll, err = logging.LogLevelFromLabel(label); err != nil {
return nil, []*ws.CategorisedError{ctl.NewCommandClientError(err.Error())}
}
logger := c.ApplicationManager.LoggerByName(name)
if logger == nil {
logger = c.FrameworkManager.LoggerByName(name)
}
if logger == nil {
m := fmt.Sprintf("Component %s does not exist or does not have a logger attached.", name)
return nil, []*ws.CategorisedError{ctl.NewCommandClientError(m)}
}
logger.SetLocalThreshold(ll)
return new(ctl.CommandOutput), nil
}
func (c *logLevelCommand) showCurrentLevel(args map[string]string) (*ctl.CommandOutput, []*ws.CategorisedError) {
var comps []*logging.ComponentLevel
var err error
var frameworkLevels bool
if frameworkLevels, err = runtimectl.OperateOnFramework(args); err != nil {
return nil, []*ws.CategorisedError{ctl.NewCommandClientError(err.Error())}
}
if frameworkLevels {
comps = c.FrameworkManager.CurrentLevels()
} else {
comps = c.ApplicationManager.CurrentLevels()
}
sort.Sort(logging.ByName{ComponentLevels: comps})
filtered := make([][]string, 0)
for _, cl := range comps {
if cl.Level != logging.All {
filtered = append(filtered, []string{cl.Name, logging.LabelFromLevel(cl.Level)})
}
}
co := new(ctl.CommandOutput)
co.OutputBody = filtered
co.RenderHint = ctl.Columns
return co, nil
}
func (c *logLevelCommand) Name() string {
return llCommandName
}
func (c *logLevelCommand) Summmary() string {
return llSummary
}
func (c *logLevelCommand) Usage() string {
return llUsage
}
func (c *logLevelCommand) Help() []string {
return []string{llHelp, llHelpTwo, llHelpThree, llHelpFour}
}