go.ligato.io/vpp-agent/v3@v3.5.0/cmd/agentctl/commands/log.go (about) 1 // Copyright (c) 2019 Cisco and/or its affiliates. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at: 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package commands 16 17 import ( 18 "context" 19 "fmt" 20 "io" 21 "sort" 22 "strings" 23 "text/tabwriter" 24 25 "github.com/spf13/cobra" 26 27 "go.ligato.io/vpp-agent/v3/cmd/agentctl/api/types" 28 agentcli "go.ligato.io/vpp-agent/v3/cmd/agentctl/cli" 29 ) 30 31 func NewLogCommand(cli agentcli.Cli) *cobra.Command { 32 cmd := &cobra.Command{ 33 Use: "log", 34 Short: "Manage agent logging", 35 } 36 cmd.AddCommand( 37 newLogListCommand(cli), 38 newLogSetCommand(cli), 39 ) 40 return cmd 41 } 42 43 func newLogListCommand(cli agentcli.Cli) *cobra.Command { 44 var ( 45 opts LogListOptions 46 ) 47 cmd := &cobra.Command{ 48 Use: "list [logger]", 49 Aliases: []string{"ls"}, 50 Short: "List agent loggers", 51 Args: cobra.MaximumNArgs(1), 52 RunE: func(cmd *cobra.Command, args []string) error { 53 if len(args) > 0 { 54 opts.Name = args[0] 55 } 56 return RunLogList(cli, opts) 57 }, 58 } 59 return cmd 60 } 61 62 type LogListOptions struct { 63 Name string 64 } 65 66 func RunLogList(cli agentcli.Cli, opts LogListOptions) error { 67 ctx, cancel := context.WithCancel(context.Background()) 68 defer cancel() 69 70 loggers, err := cli.Client().LoggerList(ctx) 71 if err != nil { 72 return err 73 } 74 75 if len(loggers) == 0 { 76 return fmt.Errorf("no logger found") 77 } 78 79 var filtered []types.Logger 80 for _, value := range loggers { 81 if opts.Name == "" || strings.Contains(value.Logger, opts.Name) { 82 filtered = append(filtered, value) 83 } 84 } 85 sort.Sort(sortedLoggers(filtered)) 86 printLoggerList(cli.Out(), filtered) 87 88 return nil 89 } 90 91 func printLoggerList(out io.Writer, list []types.Logger) { 92 w := tabwriter.NewWriter(out, 0, 0, 1, ' ', 0) 93 fmt.Fprintf(w, "LOGGER\tLEVEL\t\n") 94 for _, l := range list { 95 fmt.Fprintf(w, "%s\t%s\t\n", l.Logger, l.Level) 96 } 97 if err := w.Flush(); err != nil { 98 return 99 } 100 } 101 102 type sortedLoggers []types.Logger 103 104 func (ll sortedLoggers) Len() int { 105 return len(ll) 106 } 107 108 func (ll sortedLoggers) Less(i, j int) bool { 109 return ll[i].Logger < ll[j].Logger 110 } 111 112 func (ll sortedLoggers) Swap(i, j int) { 113 ll[i], ll[j] = ll[j], ll[i] 114 } 115 116 func newLogSetCommand(cli agentcli.Cli) *cobra.Command { 117 opts := LogSetOptions{} 118 cmd := &cobra.Command{ 119 Use: "set <logger> <debug|info|warning|error|fatal|panic>", 120 Short: "Set agent logger level", 121 Args: cobra.ExactArgs(2), 122 RunE: func(cmd *cobra.Command, args []string) error { 123 opts.Logger = args[0] 124 opts.Level = args[1] 125 return RunLogSet(cli, opts) 126 }, 127 } 128 return cmd 129 } 130 131 type LogSetOptions struct { 132 Logger string 133 Level string 134 } 135 136 func RunLogSet(cli agentcli.Cli, opts LogSetOptions) error { 137 ctx, cancel := context.WithCancel(context.Background()) 138 defer cancel() 139 140 err := cli.Client().LoggerSet(ctx, opts.Logger, opts.Level) 141 if err != nil { 142 return err 143 } 144 145 fmt.Fprintf(cli.Out(), "logger %s has been set to level %s\n", opts.Logger, opts.Level) 146 147 return nil 148 }