github.com/fafucoder/cilium@v1.6.11/cilium/cmd/endpoint_log.go (about)

     1  // Copyright 2017 Authors of Cilium
     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 cmd
    16  
    17  import (
    18  	"fmt"
    19  	"os"
    20  	"text/tabwriter"
    21  
    22  	"github.com/cilium/cilium/pkg/command"
    23  
    24  	"github.com/spf13/cobra"
    25  )
    26  
    27  // endpointLogCmd represents the endpoint_log command
    28  var endpointLogCmd = &cobra.Command{
    29  	Use:     "log <endpoint id>",
    30  	Short:   "View endpoint status log",
    31  	Example: "cilium endpoint log 5421",
    32  	Run: func(cmd *cobra.Command, args []string) {
    33  		requireEndpointID(cmd, args)
    34  		getEndpointLog(cmd, args)
    35  	},
    36  }
    37  
    38  func init() {
    39  	endpointCmd.AddCommand(endpointLogCmd)
    40  	command.AddJSONOutput(endpointLogCmd)
    41  }
    42  
    43  func getEndpointLog(cmd *cobra.Command, args []string) {
    44  	requireEndpointID(cmd, args)
    45  	eID := args[0]
    46  	epLog, err := client.EndpointLogGet(eID)
    47  	if err != nil {
    48  		Fatalf("Cannot get endpoint log %s: %s\n", eID, err)
    49  	}
    50  
    51  	if command.OutputJSON() {
    52  		if err := command.PrintOutput(epLog); err != nil {
    53  			os.Exit(1)
    54  		}
    55  	} else {
    56  		w := tabwriter.NewWriter(os.Stdout, 2, 0, 3, ' ', 0)
    57  		fmt.Fprintf(w, "%s\t%s\t%s\t%v\n", "Timestamp", "Status", "State", "Message")
    58  		for _, entry := range epLog {
    59  			fmt.Fprintf(w, "%s\t%s\t%s\t%v\n", entry.Timestamp, entry.Code, entry.State, entry.Message)
    60  		}
    61  		w.Flush()
    62  	}
    63  }