2020-02-15 05:49:54 +00:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-08-17 13:15:36 +00:00
|
|
|
"time"
|
2020-02-15 05:49:54 +00:00
|
|
|
|
2021-08-17 13:15:36 +00:00
|
|
|
"github.com/fatih/color"
|
2020-06-02 18:12:53 +00:00
|
|
|
"github.com/urfave/cli/v2"
|
2020-06-05 22:59:01 +00:00
|
|
|
"golang.org/x/xerrors"
|
2020-02-15 05:49:54 +00:00
|
|
|
)
|
|
|
|
|
2021-03-23 23:19:22 +00:00
|
|
|
var LogCmd = &cli.Command{
|
2020-02-15 05:49:54 +00:00
|
|
|
Name: "log",
|
|
|
|
Usage: "Manage logging",
|
|
|
|
Subcommands: []*cli.Command{
|
2021-03-23 23:19:22 +00:00
|
|
|
LogList,
|
|
|
|
LogSetLevel,
|
2021-08-17 13:15:36 +00:00
|
|
|
LogAlerts,
|
2020-02-15 05:49:54 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-03-23 23:19:22 +00:00
|
|
|
var LogList = &cli.Command{
|
2020-02-15 05:49:54 +00:00
|
|
|
Name: "list",
|
|
|
|
Usage: "List log systems",
|
|
|
|
Action: func(cctx *cli.Context) error {
|
2020-08-26 20:51:32 +00:00
|
|
|
api, closer, err := GetAPI(cctx)
|
2020-02-15 05:49:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer closer()
|
|
|
|
|
|
|
|
ctx := ReqContext(cctx)
|
|
|
|
|
|
|
|
systems, err := api.LogList(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, system := range systems {
|
|
|
|
fmt.Println(system)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-03-23 23:19:22 +00:00
|
|
|
var LogSetLevel = &cli.Command{
|
2020-02-15 05:49:54 +00:00
|
|
|
Name: "set-level",
|
|
|
|
Usage: "Set log level",
|
2020-03-04 21:46:00 +00:00
|
|
|
ArgsUsage: "[level]",
|
2020-02-15 05:49:54 +00:00
|
|
|
Description: `Set the log level for logging systems:
|
|
|
|
|
|
|
|
The system flag can be specified multiple times.
|
|
|
|
|
2020-09-07 18:45:34 +00:00
|
|
|
eg) log set-level --system chain --system chainxchg debug
|
2020-02-15 05:49:54 +00:00
|
|
|
|
|
|
|
Available Levels:
|
|
|
|
debug
|
|
|
|
info
|
|
|
|
warn
|
|
|
|
error
|
|
|
|
|
|
|
|
Environment Variables:
|
|
|
|
GOLOG_LOG_LEVEL - Default log level for all log systems
|
|
|
|
GOLOG_LOG_FMT - Change output log format (json, nocolor)
|
2020-08-21 00:02:43 +00:00
|
|
|
GOLOG_FILE - Write logs to file
|
|
|
|
GOLOG_OUTPUT - Specify whether to output to file, stderr, stdout or a combination, i.e. file+stderr
|
2020-02-15 05:49:54 +00:00
|
|
|
`,
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.StringSliceFlag{
|
|
|
|
Name: "system",
|
|
|
|
Usage: "limit to log system",
|
|
|
|
Value: &cli.StringSlice{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Action: func(cctx *cli.Context) error {
|
2020-08-26 20:51:32 +00:00
|
|
|
api, closer, err := GetAPI(cctx)
|
2020-02-15 05:49:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer closer()
|
|
|
|
ctx := ReqContext(cctx)
|
|
|
|
|
|
|
|
if !cctx.Args().Present() {
|
|
|
|
return fmt.Errorf("level is required")
|
|
|
|
}
|
|
|
|
|
|
|
|
systems := cctx.StringSlice("system")
|
|
|
|
if len(systems) == 0 {
|
|
|
|
var err error
|
|
|
|
systems, err = api.LogList(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, system := range systems {
|
|
|
|
if err := api.LogSetLevel(ctx, system, cctx.Args().First()); err != nil {
|
2021-02-11 11:00:26 +00:00
|
|
|
return xerrors.Errorf("setting log level on %s: %v", system, err)
|
2020-02-15 05:49:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
2021-08-17 13:15:36 +00:00
|
|
|
|
|
|
|
var LogAlerts = &cli.Command{
|
|
|
|
Name: "alerts",
|
|
|
|
Usage: "Get alert states",
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "all",
|
|
|
|
Usage: "get all (active and inactive) alerts",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Action: func(cctx *cli.Context) error {
|
|
|
|
api, closer, err := GetAPI(cctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer closer()
|
|
|
|
|
|
|
|
ctx := ReqContext(cctx)
|
|
|
|
|
|
|
|
alerts, err := api.LogAlerts(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("getting alerts: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
all := cctx.Bool("all")
|
|
|
|
|
|
|
|
for _, alert := range alerts {
|
|
|
|
if !all && !alert.Active {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
active := color.RedString("active ")
|
|
|
|
if !alert.Active {
|
|
|
|
active = color.GreenString("inactive")
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("%s %s:%s\n", active, alert.Type.System, alert.Type.Subsystem)
|
|
|
|
if alert.LastResolved != nil {
|
|
|
|
fmt.Printf(" last resolved at %s; reason: %s\n", alert.LastResolved.Time.Truncate(time.Millisecond), alert.LastResolved.Message)
|
|
|
|
}
|
|
|
|
if alert.LastActive != nil {
|
|
|
|
fmt.Printf(" %s %s; reason: %s\n", color.YellowString("last raised at"), alert.LastActive.Time.Truncate(time.Millisecond), alert.LastActive.Message)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|