-
Notifications
You must be signed in to change notification settings - Fork 26
/
log.go
78 lines (65 loc) · 1.8 KB
/
log.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
package command
import (
"errors"
"fmt"
"github.com/ipni/storetheindex/admin/client"
"github.com/urfave/cli/v2"
)
var subsystemsCmd = &cli.Command{
Name: "subsystems",
Usage: "Lists the available logging subsystems",
Action: listLogSubsystemsAction,
}
var setLevelCmd = &cli.Command{
Name: "setlevel",
Usage: "Sets log level for logging subsystems",
Description: `Configures log level for logging subystems specified as pairs of arguments.
Subsystem may be specified as a regular expression. For example the
following command will set the level for all subsystems to debug:
storetheindex log setlevel '.*' debug`,
ArgsUsage: "<sub-system> <level> [<sub-system> <level>]...",
Action: setLogLevelAction,
}
var LogCmd = &cli.Command{
Name: "log",
Usage: "Show log subsystems and modify log levels",
Flags: []cli.Flag{indexerHostFlag},
Subcommands: []*cli.Command{
subsystemsCmd,
setLevelCmd,
},
}
func setLogLevelAction(cctx *cli.Context) error {
cl, err := client.New(cliIndexer(cctx, "admin"))
if err != nil {
return err
}
args := cctx.Args()
if !args.Present() {
return errors.New("at least one <sub-system> <level> argument pair must be specified")
}
if args.Len()%2 != 0 {
return fmt.Errorf("<sub-system> <level> argument must be specified as pairs")
}
sysLvl := make(map[string]string, args.Len()/2)
for i := 0; i < args.Len(); i += 2 {
subsys := args.Get(i)
level := args.Get(i + 1)
sysLvl[subsys] = level
}
return cl.SetLogLevels(cctx.Context, sysLvl)
}
func listLogSubsystemsAction(cctx *cli.Context) error {
cl, err := client.New(cliIndexer(cctx, "admin"))
if err != nil {
return err
}
subSystems, err := cl.ListLogSubSystems(cctx.Context)
if err != nil {
return err
}
for _, ss := range subSystems {
_, _ = fmt.Fprintln(cctx.App.Writer, ss)
}
return nil
}