-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
log.go
55 lines (47 loc) · 1.32 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
package log
import (
"context"
"fmt"
"io"
"log"
"strings"
"github.com/DavidGamba/go-getoptions"
)
var Logger = log.New(io.Discard, "log ", log.LstdFlags)
// NewCommand - Populate Options definition
func NewCommand(parent *getoptions.GetOpt) *getoptions.GetOpt {
opt := parent.NewCommand("log", "Show application logs")
opt.String("level", "INFO", opt.Description("filter debug level"), opt.ValidValues("ERROR", "DEBUG", "INFO"))
opt.SetCommandFn(Run)
return opt
}
// Run - Command entry point
func Run(ctx context.Context, opt *getoptions.GetOpt, args []string) error {
Logger.Printf("log args: %v\n", args)
filterLevel := opt.Value("level").(string)
logLines := []string{
`1900/01/01 01:01:01 INFO beginning of logs`,
`1900/01/01 01:01:02 DEBUG user 'david.gamba' failed login attempt`,
`1900/01/01 01:01:03 INFO user 'david.gamba' logged in`,
`1900/01/01 01:01:04 ERROR request by user 'david.gamba' crashed the system`,
}
if !opt.Called("level") {
fmt.Println(strings.Join(logLines, "\n"))
} else {
for _, e := range logLines {
switch filterLevel {
case "DEBUG":
fmt.Println(e)
case "INFO":
if strings.Contains(e, "INFO") || strings.Contains(e, "ERROR") {
fmt.Println(e)
}
case "ERROR":
if strings.Contains(e, "ERROR") {
fmt.Println(e)
}
}
}
}
return nil
}