-
Notifications
You must be signed in to change notification settings - Fork 492
/
logs.go
112 lines (104 loc) · 3.23 KB
/
logs.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
/*
* Copyright (c) 2017, MegaEase
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http:https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package commandv2
import (
"bufio"
"fmt"
"io"
"net/http"
"github.com/megaease/easegress/v2/cmd/client/general"
"github.com/spf13/cobra"
)
// LogsCmd returns logs command.
func LogsCmd() *cobra.Command {
var n int
var follow bool
examples := []general.Example{
{Desc: "Print the most recent 500 logs by default.", Command: "egctl logs"},
{Desc: "Print the most recent 100 logs.", Command: "egctl logs --tail 100"},
{Desc: "Print all logs.", Command: "egctl logs --tail -1"},
{Desc: "Print the most recent 500 logs and streaming the log.", Command: "egctl logs -f"},
}
cmd := &cobra.Command{
Use: "logs",
Short: "Print the logs of Easegress server",
Args: cobra.NoArgs,
Example: createMultiExample(examples),
Run: func(cmd *cobra.Command, args []string) {
query := fmt.Sprintf("?tail=%d&follow=%v", n, follow)
p := general.LogsURL + query
reader, err := general.HandleReqWithStreamResp(http.MethodGet, p, nil)
if err != nil {
general.ExitWithError(err)
}
defer reader.Close()
r := bufio.NewReader(reader)
for {
bytes, err := r.ReadBytes('\n')
if err != nil {
if err != io.EOF {
general.ExitWithError(err)
}
return
}
fmt.Print(string(bytes))
}
},
}
cmd.Flags().IntVar(&n, "tail", 500, "Lines of recent log file to display. Defaults to 500, use -1 to show all lines")
cmd.Flags().BoolVarP(&follow, "follow", "f", false, "Specify if the logs should be streamed.")
cmd.AddCommand(setLogLevelCmd())
cmd.AddCommand(getLogLevelCmd())
return cmd
}
func setLogLevelCmd() *cobra.Command {
examples := []general.Example{
{Desc: "Set log level to info", Command: "egctl logs set-level info"},
{Desc: "Set log level to debug", Command: "egctl logs set-level debug"},
}
cmd := &cobra.Command{
Use: "set-level",
Short: "Set Easegress log level",
Example: createMultiExample(examples),
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
level := args[0]
p := general.LogsLevelURL + "/" + level
if _, err := general.HandleRequest(http.MethodPut, p, nil); err != nil {
general.ExitWithError(err)
}
fmt.Println("Set log level to", level)
},
}
return cmd
}
func getLogLevelCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "get-level",
Short: "Get Easegress log level",
Example: createExample("Get current log level.", "egctl logs get-level"),
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
body, err := general.HandleRequest(http.MethodGet, general.LogsLevelURL, nil)
if err != nil {
general.ExitWithError(err)
}
fmt.Println(string(body))
},
}
return cmd
}