Skip to content

Commit 986b9ba

Browse files
committed
feat: add log package
Signed-off-by: Daniel Hu <[email protected]>
1 parent 62e65e0 commit 986b9ba

File tree

8 files changed

+280
-5
lines changed

8 files changed

+280
-5
lines changed

cmd/git.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package cmd
22

33
import (
4+
"strings"
5+
46
"github.com/spf13/cobra"
57

68
"github.com/devstream-io/devstream/internal/pkg/git"
@@ -10,9 +12,13 @@ import (
1012
var gitCmd = &cobra.Command{
1113
Use: "git",
1214
Short: "git is used to execute git operations",
13-
Long: `git is used to execute git operations`,
15+
Long: `git is used to execute git operations
16+
1. dtm git 'git add .'
17+
2. dtm git 'git commit -s -m "commit message"'
18+
`,
1419
Run: func(cmd *cobra.Command, args []string) {
15-
git.Run()
20+
gitCmdStr := strings.Join(args, " ")
21+
git.Execute(gitCmdStr)
1622
},
1723
}
1824

cmd/root.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
)
1010

1111
var cfgFile string
12+
var OutputFormat string
1213

1314
// rootCmd represents the base command when called without any subcommands
1415
var rootCmd = &cobra.Command{

go.mod

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/devstream-io/devstream
22

3-
go 1.18
3+
go 1.20
44

55
require (
66
github.com/spf13/cobra v1.7.0
@@ -14,13 +14,15 @@ require (
1414
github.com/magiconair/properties v1.8.7 // indirect
1515
github.com/mitchellh/mapstructure v1.5.0 // indirect
1616
github.com/pelletier/go-toml/v2 v2.0.6 // indirect
17+
github.com/sirupsen/logrus v1.9.0 // indirect
1718
github.com/spf13/afero v1.9.3 // indirect
1819
github.com/spf13/cast v1.5.0 // indirect
1920
github.com/spf13/jwalterweatherman v1.1.0 // indirect
2021
github.com/spf13/pflag v1.0.5 // indirect
2122
github.com/subosito/gotenv v1.4.2 // indirect
2223
golang.org/x/sys v0.3.0 // indirect
2324
golang.org/x/text v0.5.0 // indirect
25+
gopkg.in/gookit/color.v1 v1.1.6 // indirect
2426
gopkg.in/ini.v1 v1.67.0 // indirect
2527
gopkg.in/yaml.v3 v3.0.1 // indirect
2628
)

go.sum

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@ github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:
148148
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
149149
github.com/rogpeppe/go-internal v1.6.1 h1:/FiVV8dS/e+YqF2JvO3yXRFbBLTIuSDkuC7aBOAvL+k=
150150
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
151+
github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0=
152+
github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
151153
github.com/spf13/afero v1.9.3 h1:41FoI0fD7OR7mGcKE/aOiLkGreyf8ifIOQmJANWogMk=
152154
github.com/spf13/afero v1.9.3/go.mod h1:iUV7ddyEEZPO5gA3zD4fJt6iStLlL+Lg4m2cihcDf8Y=
153155
github.com/spf13/cast v1.5.0 h1:rj3WzYc11XZaIZMPKmwP96zkFEnnAmV8s6XbB2aY32w=
@@ -307,6 +309,7 @@ golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4/go.mod h1:h1NjWce9XRLGQEsW7w
307309
golang.org/x/sys v0.0.0-20210225134936-a50acf3fe073/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
308310
golang.org/x/sys v0.0.0-20210423185535-09eb48e85fd7/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
309311
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
312+
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
310313
golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
311314
golang.org/x/sys v0.3.0 h1:w8ZOecv6NaNa/zC8944JTU3vz4u6Lagfk4RPQxv92NQ=
312315
golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
@@ -465,6 +468,8 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8
465468
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
466469
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
467470
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
471+
gopkg.in/gookit/color.v1 v1.1.6 h1:5fB10p6AUFjhd2ayq9JgmJWr9WlTrguFdw3qlYtKNHk=
472+
gopkg.in/gookit/color.v1 v1.1.6/go.mod h1:IcEkFGaveVShJ+j8ew+jwe9epHyGpJ9IrptHmW3laVY=
468473
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
469474
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
470475
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

internal/logs/log.go

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package log
2+
3+
import (
4+
"io"
5+
"os"
6+
7+
"github.com/devstream-io/devstream/cmd"
8+
"github.com/sirupsen/logrus"
9+
)
10+
11+
var (
12+
debugLog = &CliLoggerFormatter{showType: "debug"}
13+
infoLog = &CliLoggerFormatter{showType: "info"}
14+
warnLog = &CliLoggerFormatter{showType: "warn"}
15+
errorLog = &CliLoggerFormatter{showType: "error"}
16+
fatalLog = &CliLoggerFormatter{showType: "fatal"}
17+
successLog = &CliLoggerFormatter{showType: "success"}
18+
)
19+
20+
func RedirectOutput(writer io.Writer) {
21+
logrus.SetOutput(writer)
22+
}
23+
24+
func RecoverOutput() {
25+
if cmd.OutputFormat != "" {
26+
return
27+
}
28+
logrus.SetOutput(os.Stdout)
29+
}
30+
31+
// Debugf log info with color,symbol and format for a notice
32+
func Debugf(format string, args ...interface{}) {
33+
logrus.SetFormatter(debugLog)
34+
logrus.Debugf(format, args...)
35+
}
36+
37+
// Debug log info with color and symbol, for a notice
38+
func Debug(args ...interface{}) {
39+
logrus.SetFormatter(debugLog)
40+
logrus.Debug(args...)
41+
}
42+
43+
// Infof log info with color,symbol and format for a notice
44+
func Infof(format string, args ...interface{}) {
45+
logrus.SetFormatter(infoLog)
46+
logrus.Infof(format, args...)
47+
}
48+
49+
// Info log info with color and symbol, for a notice
50+
func Info(args ...interface{}) {
51+
logrus.SetFormatter(infoLog)
52+
logrus.Info(args...)
53+
}
54+
55+
// Warnf log warn with color,symbol and format for a warning event
56+
func Warnf(format string, args ...interface{}) {
57+
logrus.SetFormatter(warnLog)
58+
logrus.Warnf(format, args...)
59+
}
60+
61+
// Warn log warn with color and symbol, for a warning event
62+
func Warn(args ...interface{}) {
63+
logrus.SetFormatter(warnLog)
64+
logrus.Warn(args...)
65+
}
66+
67+
// Errorf log error with color,symbol and format for a warning event
68+
func Errorf(format string, args ...interface{}) {
69+
logrus.SetFormatter(errorLog)
70+
logrus.Errorf(format, args...)
71+
}
72+
73+
// Error log error with color adn symbol for a warning event
74+
func Error(args ...interface{}) {
75+
logrus.SetFormatter(errorLog)
76+
logrus.Error(args...)
77+
}
78+
79+
// Fatalf log fatal with color,symbol and format for a fatal event
80+
func Fatalf(format string, args ...interface{}) {
81+
logrus.SetFormatter(fatalLog)
82+
logrus.Fatalf(format, args...)
83+
}
84+
85+
// Fatal log fatal with color and symbol for a fatal event
86+
func Fatal(args ...interface{}) {
87+
logrus.SetFormatter(fatalLog)
88+
logrus.Fatal(args...)
89+
}
90+
91+
// Successf log success with color,symbol and format for a success operation
92+
func Successf(format string, args ...interface{}) {
93+
logrus.SetFormatter(successLog)
94+
logrus.Infof(format, args...)
95+
}
96+
97+
// Success log success with color and symbol, for a success operation
98+
func Success(args ...interface{}) {
99+
logrus.SetFormatter(successLog)
100+
logrus.Info(args...)
101+
}

internal/logs/symbol.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package log
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
type Symbol string
8+
9+
// Symbols struct contains all symbols
10+
type Symbols struct {
11+
Debug Symbol
12+
Info Symbol
13+
Warning Symbol
14+
Warn Symbol
15+
Error Symbol
16+
Fatal Symbol
17+
Success Symbol
18+
}
19+
20+
var normal = Symbols{
21+
Debug: Symbol("λ"),
22+
Info: Symbol("ℹ"),
23+
Success: Symbol("✔"),
24+
Warning: Symbol("⚠"),
25+
Warn: Symbol("⚠"),
26+
Error: Symbol("!!"),
27+
Fatal: Symbol("✖"),
28+
}
29+
30+
// String returns a printable representation of Symbols struct
31+
func (s Symbols) String() string {
32+
return fmt.Sprintf("Debug: %s Info: %s Success: %s Warning: %s Error: %s Fatal: %s", s.Debug, s.Info, s.Success, s.Warning, s.Error, s.Fatal)
33+
}

internal/logs/wrapper.go

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
package log
2+
3+
import (
4+
"bytes"
5+
"fmt"
6+
"runtime"
7+
8+
"github.com/sirupsen/logrus"
9+
"gopkg.in/gookit/color.v1"
10+
)
11+
12+
const (
13+
DEBUG = "[DEBUG] "
14+
WARN = "[WARN] "
15+
INFO = "[INFO] "
16+
SUCCESS = "[SUCCESS] "
17+
ERROR = "[ERROR] "
18+
FATAL = "[FATAL] "
19+
)
20+
21+
// ROOTCALLER should be main proc
22+
const ROOTCALLER = "main.main"
23+
24+
// REMOVELEVEL logrus stack level is 9, should be removed from stack trace
25+
const REMOVELEVEL = 10
26+
27+
type CliLoggerFormatter struct {
28+
level logrus.Level
29+
showType string
30+
formatLevelName string
31+
prefix string
32+
}
33+
34+
// Format implement Format interface to output custom log
35+
func (m *CliLoggerFormatter) Format(entry *logrus.Entry) ([]byte, error) {
36+
var b *bytes.Buffer
37+
if entry.Buffer != nil {
38+
b = entry.Buffer
39+
} else {
40+
b = &bytes.Buffer{}
41+
}
42+
43+
m.levelPrintRender()
44+
45+
timestamp := entry.Time.Format("2006-01-02 15:04:05")
46+
47+
if m.level == logrus.ErrorLevel && logrus.GetLevel() == logrus.DebugLevel {
48+
entry.Message = addCallStackIgnoreLogrus(entry.Message)
49+
}
50+
51+
newLog := fmt.Sprintf("%s %s %s %s\n", timestamp, m.prefix, m.formatLevelName, entry.Message)
52+
53+
_, err := b.WriteString(newLog)
54+
if err != nil {
55+
return nil, err
56+
}
57+
return b.Bytes(), nil
58+
}
59+
60+
// levelPrintRender render symbo and level according to type
61+
func (m *CliLoggerFormatter) levelPrintRender() {
62+
switch m.showType {
63+
case "debug":
64+
m.level = logrus.DebugLevel
65+
m.formatLevelName = color.Blue.Render(DEBUG)
66+
m.prefix = color.Blue.Render(normal.Debug)
67+
case "info":
68+
m.level = logrus.InfoLevel
69+
m.formatLevelName = color.FgLightBlue.Render(INFO)
70+
m.prefix = color.FgLightBlue.Render(normal.Info)
71+
case "warn":
72+
m.level = logrus.WarnLevel
73+
m.formatLevelName = color.Yellow.Render(WARN)
74+
m.prefix = color.Yellow.Render(normal.Warn)
75+
case "error":
76+
m.level = logrus.ErrorLevel
77+
m.formatLevelName = color.BgRed.Render(ERROR)
78+
m.prefix = color.Red.Render(normal.Error)
79+
case "fatal":
80+
m.level = logrus.FatalLevel
81+
m.formatLevelName = color.BgRed.Render(FATAL)
82+
m.prefix = color.Red.Render(normal.Fatal)
83+
case "success":
84+
m.level = logrus.InfoLevel
85+
m.formatLevelName = color.Green.Render(SUCCESS)
86+
m.prefix = color.Green.Render(normal.Success)
87+
}
88+
}
89+
90+
type SeparatorFormatter struct{}
91+
92+
// Format implement Format interface to output custom log
93+
func (s *SeparatorFormatter) Format(entry *logrus.Entry) ([]byte, error) {
94+
var b *bytes.Buffer
95+
if entry.Buffer != nil {
96+
b = entry.Buffer
97+
} else {
98+
b = &bytes.Buffer{}
99+
}
100+
101+
timestamp := entry.Time.Format("2006-01-02 15:04:05")
102+
newLog := fmt.Sprintf("%s %s %s %s\n",
103+
timestamp,
104+
color.Blue.Render(normal.Info),
105+
color.Blue.Render(INFO),
106+
color.Blue.Render(fmt.Sprintf("%s %s %s", "-------------------- [ ", entry.Message, " ] --------------------")))
107+
108+
_, err := b.WriteString(newLog)
109+
if err != nil {
110+
return nil, err
111+
}
112+
return b.Bytes(), nil
113+
}
114+
115+
// addCallStackIgnoreLogrus add call stack to log message without logrus stack
116+
func addCallStackIgnoreLogrus(rawMessage string) string {
117+
stackMessage := rawMessage
118+
for i := REMOVELEVEL; ; i++ {
119+
pc, file, line, _ := runtime.Caller(i)
120+
stackMessage = stackMessage + "\n -- " + file + fmt.Sprintf(" %d", line)
121+
entrance := runtime.FuncForPC(pc).Name()
122+
if entrance == ROOTCALLER || entrance == "" {
123+
break
124+
}
125+
}
126+
return stackMessage
127+
}

internal/pkg/git/git.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ package git
22

33
import "fmt"
44

5-
func Run() {
6-
fmt.Println("git called")
5+
func Execute(cmdStr string) {
6+
fmt.Println(cmdStr)
77
}

0 commit comments

Comments
 (0)