Skip to content

Commit ba22188

Browse files
committed
replace zap logger with logrus
1 parent 66f9185 commit ba22188

File tree

6 files changed

+54
-101
lines changed

6 files changed

+54
-101
lines changed

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ go 1.14
55
require (
66
github.com/BurntSushi/xgb v0.0.0-20200324125942-20f126ea2843 // indirect
77
github.com/BurntSushi/xgbutil v0.0.0-20190907113008-ad855c713046
8-
go.uber.org/zap v1.15.0
8+
github.com/sirupsen/logrus v1.6.0
99
)

go.sum

+6-40
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,12 @@
1-
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
21
github.com/BurntSushi/xgb v0.0.0-20200324125942-20f126ea2843 h1:3iF31c7rp7nGZVDv7YQ+VxOgpipVfPKotLXykjZmwM8=
32
github.com/BurntSushi/xgb v0.0.0-20200324125942-20f126ea2843/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
43
github.com/BurntSushi/xgbutil v0.0.0-20190907113008-ad855c713046 h1:O/r2Sj+8QcMF7V5IcmiE2sMFV2q3J47BEirxbXJAdzA=
54
github.com/BurntSushi/xgbutil v0.0.0-20190907113008-ad855c713046/go.mod h1:uw9h2sd4WWHOPdJ13MQpwK5qYWKYDumDqxWWIknEQ+k=
6-
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
75
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
8-
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
9-
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
10-
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
11-
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
12-
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
13-
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
6+
github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
147
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
15-
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
16-
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
17-
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
18-
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
19-
go.uber.org/atomic v1.6.0 h1:Ezj3JGmsOnG1MoRWQkPBsKLe9DwWD9QeXzTRzzldNVk=
20-
go.uber.org/atomic v1.6.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ=
21-
go.uber.org/multierr v1.5.0 h1:KCa4XfM8CWFCpxXRGok+Q0SS/0XBhMDbHHGABQLvD2A=
22-
go.uber.org/multierr v1.5.0/go.mod h1:FeouvMocqHpRaaGuG9EjoKcStLC43Zu/fmqdUMPcKYU=
23-
go.uber.org/tools v0.0.0-20190618225709-2cfd321de3ee/go.mod h1:vJERXedbb3MVM5f9Ejo0C68/HhF8uaILCdgjnY+goOA=
24-
go.uber.org/zap v1.15.0 h1:ZZCA22JRF2gQE5FoNmhmrf7jeJJ2uhqDUNRYKm8dvmM=
25-
go.uber.org/zap v1.15.0/go.mod h1:Mb2vm2krFEG5DV0W9qcHBYFtp/Wku1cvYaqPsS/WYfc=
26-
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
27-
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
28-
golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
29-
golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e/go.mod h1:mXi4GBBbnImb6dmsKGUJ2LatrhH/nqhxcFungHvyanc=
30-
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
31-
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
32-
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
33-
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
34-
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
35-
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
36-
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
37-
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
38-
golang.org/x/tools v0.0.0-20190621195816-6e04913cbbac/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
39-
golang.org/x/tools v0.0.0-20191029041327-9cc4af7d6b2c/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
40-
golang.org/x/tools v0.0.0-20191029190741-b9c20aec41a5/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
41-
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
42-
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
43-
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
44-
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
45-
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
46-
honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg=
8+
github.com/sirupsen/logrus v1.6.0 h1:UBcNElsrwanuuMsnGSlYmtmgbb23qDR5dG+6X6Oo89I=
9+
github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88=
10+
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
11+
golang.org/x/sys v0.0.0-20190422165155-953cdadca894 h1:Cz4ceDQGXuKRnVBDTS23GTn/pU5OE2C0WrNTOYK1Uuc=
12+
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=

listener.go

+15-14
Original file line numberDiff line numberDiff line change
@@ -7,44 +7,45 @@ import (
77
"os/exec"
88
"strings"
99
"syscall"
10+
"time"
1011

1112
"github.com/BurntSushi/xgbutil"
1213
"github.com/BurntSushi/xgbutil/keybind"
1314
"github.com/BurntSushi/xgbutil/mousebind"
1415
"github.com/BurntSushi/xgbutil/xevent"
15-
"go.uber.org/zap"
16+
"github.com/sirupsen/logrus"
1617
)
1718

1819
// listenKeybinding does connect a keybinding/mousebinding to the Xorg server
19-
func listenKeybinding(X *xgbutil.XUtil, errs chan<- error, evtType int, shell, keybinding, do string) (err error) {
20+
func listenKeybinding(X *xgbutil.XUtil, errs chan<- error, evtType int, shell, keybinding, command string) (err error) {
2021
switch evtType {
2122
case evtKeyPress:
2223
binding := keybind.KeyPressFun(func(xu *xgbutil.XUtil, event xevent.KeyPressEvent) {
23-
go doAction(errs, shell, do)
24+
go execCommand(errs, shell, command)
2425
})
2526

26-
zap.L().Debug("adding key press event", zap.String("binding", keybinding), zap.String("do", do), zap.Error(err))
27+
logger.WithFields(logrus.Fields{"binding": keybinding, "command": command}).WithError(err).Debug("adding key press event")
2728
err = binding.Connect(X, X.RootWin(), keybinding, true)
2829
case evtKeyRelease:
2930
binding := keybind.KeyReleaseFun(func(xu *xgbutil.XUtil, event xevent.KeyReleaseEvent) {
30-
go doAction(errs, shell, do)
31+
go execCommand(errs, shell, command)
3132
})
3233

33-
zap.L().Debug("adding key release event", zap.String("binding", keybinding), zap.String("do", do), zap.Error(err))
34+
logger.WithFields(logrus.Fields{"binding": keybinding, "command": command}).WithError(err).Debug("adding key release event")
3435
err = binding.Connect(X, X.RootWin(), keybinding, true)
3536
case evtButtonPress:
3637
binding := mousebind.ButtonPressFun(func(xu *xgbutil.XUtil, event xevent.ButtonPressEvent) {
37-
go doAction(errs, shell, do)
38+
go execCommand(errs, shell, command)
3839
})
3940

40-
zap.L().Debug("adding button press event", zap.String("binding", keybinding), zap.String("do", do), zap.Error(err))
41+
logger.WithFields(logrus.Fields{"binding": keybinding, "command": command}).WithError(err).Debug("adding button press event")
4142
err = binding.Connect(X, X.RootWin(), keybinding, false, true)
4243
case evtButtonRelease:
4344
binding := mousebind.ButtonReleaseFun(func(xu *xgbutil.XUtil, event xevent.ButtonReleaseEvent) {
44-
go doAction(errs, shell, do)
45+
go execCommand(errs, shell, command)
4546
})
4647

47-
zap.L().Debug("adding button release event", zap.String("binding", keybinding), zap.String("do", do), zap.Error(err))
48+
logger.WithFields(logrus.Fields{"binding": keybinding, "command": command}).WithError(err).Debug("adding button release event")
4849
err = binding.Connect(X, X.RootWin(), keybinding, false, true)
4950
default:
5051
err = errors.New("wrong event type passed")
@@ -53,18 +54,18 @@ func listenKeybinding(X *xgbutil.XUtil, errs chan<- error, evtType int, shell, k
5354
return
5455
}
5556

56-
// do a given shell command
57-
func doAction(err chan<- error, shell, do string) {
57+
// execCommand executes a command in givel shell
58+
func execCommand(err chan<- error, shell, command string) {
5859
writer := new(bytes.Buffer)
5960
cmd := exec.Command(shell)
60-
cmd.Stdin = strings.NewReader(do)
61+
cmd.Stdin = strings.NewReader(command)
6162
cmd.Stdout = os.Stdout
6263
cmd.Stderr = writer
6364
cmd.SysProcAttr = &syscall.SysProcAttr{
6465
Foreground: false,
6566
Setsid: true,
6667
}
67-
zap.L().Debug("now executing a command", zap.String("command", do))
68+
logger.WithTime(time.Now()).WithField("command", command).Debug("now executing a command")
6869
err <- cmd.Start()
6970
if e := cmd.Wait(); e != nil {
7071
prefixLen := len(shell) + 2

logger.go

+11-27
Original file line numberDiff line numberDiff line change
@@ -4,38 +4,22 @@ import (
44
"os"
55
"strconv"
66

7-
"go.uber.org/zap"
8-
"go.uber.org/zap/zapcore"
7+
"github.com/sirupsen/logrus"
98
)
109

10+
var logger = logrus.New()
11+
1112
// init the logger before main function
1213
func init() {
13-
var config zap.Config
14-
15-
doExtra := func(envVar string) bool {
16-
if env, err := strconv.ParseBool(os.Getenv(envVar)); env && err == nil {
17-
return true
18-
}
19-
return false
20-
}
14+
formatter := &logrus.TextFormatter{DisableTimestamp: true, ForceQuote: true}
2115

22-
if doExtra("DEV") {
23-
config = zap.NewDevelopmentConfig()
16+
if env, err := strconv.ParseBool(os.Getenv("DEBUG")); env && err == nil {
17+
logger.SetLevel(logrus.DebugLevel)
18+
logger.ReportCaller = true
19+
formatter.DisableTimestamp = false
20+
logger.SetFormatter(formatter)
2421
} else {
25-
config = zap.NewProductionConfig()
26-
}
27-
28-
if doExtra("DEBUG") {
29-
config.Level = zap.NewAtomicLevelAt(zap.DebugLevel)
22+
logger.SetLevel(logrus.InfoLevel)
23+
logger.SetFormatter(formatter)
3024
}
31-
32-
config.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
33-
config.DisableStacktrace = true
34-
35-
logger, err := config.Build()
36-
if err != nil {
37-
panic(err)
38-
}
39-
40-
zap.ReplaceGlobals(logger)
4125
}

main.go

+17-15
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
"github.com/BurntSushi/xgbutil/keybind"
1717
"github.com/BurntSushi/xgbutil/mousebind"
1818
"github.com/BurntSushi/xgbutil/xevent"
19-
"go.uber.org/zap"
19+
"github.com/sirupsen/logrus"
2020
)
2121

2222
var usage = `NAME
@@ -83,23 +83,26 @@ func main() {
8383

8484
if opts.config != nil {
8585
if validPath, err = isPathToConfigValid(*opts.config); !(err == nil && validPath) {
86-
zap.L().Fatal("path to the config is not valid", zap.String("path", *opts.config), zap.Bool("valid", validPath), zap.Error(err))
86+
logger.WithFields(logrus.Fields{
87+
"path": *opts.config,
88+
"valid": validPath,
89+
}).WithError(err).Fatal("path to the config is not valid")
8790
}
8891
configFilePath = *opts.config
8992
} else {
9093
configFilePath, _, err = getDefaultConfigPath()
9194
if err != nil {
92-
zap.L().Fatal("can not get default config path", zap.Error(err))
95+
logger.WithError(err).Fatal("can not get config path")
9396
}
9497

9598
if validPath, err = isPathToConfigValid(configFilePath); !(err == nil && validPath) {
9699
if os.IsNotExist(err) {
97100
err = createDefaultConfig()
98101
if err != nil {
99-
zap.L().Fatal("can not create default config", zap.String("path", configFilePath), zap.Error(err))
102+
logger.WithField("path", configFilePath).Fatal("can not create default config")
100103
}
101104
} else {
102-
zap.L().Fatal("path to the config is not valid", zap.String("path", configFilePath), zap.Bool("valid", validPath), zap.Error(err))
105+
logger.WithFields(logrus.Fields{"path": configFilePath, "valid": validPath}).WithError(err).Fatal("path to the config is not valid")
103106
}
104107
}
105108
}
@@ -116,7 +119,7 @@ func main() {
116119

117120
shell, err = parse(configFilePath, &data)
118121
if err != nil {
119-
zap.L().Fatal("failed to parse config", zap.String("file", configFilePath), zap.Error(err))
122+
logger.WithField("file", configFilePath).WithError(err).Fatal("failed to parse config")
120123
}
121124

122125
if opts.parseTime {
@@ -145,7 +148,7 @@ func main() {
145148
if opts.kill || opts.reload {
146149
execName, err := os.Executable()
147150
if err != nil {
148-
zap.L().Fatal("can not get executable", zap.Error(err))
151+
logger.WithError(err).Fatal("can not get executable")
149152
}
150153

151154
if opts.kill {
@@ -177,8 +180,7 @@ func main() {
177180
os.Exit(0)
178181
}
179182

180-
zap.L().Debug("starting dxhd", zap.String("version", version))
181-
zap.L().Debug("custom path might be provided", zap.String("path", configFilePath))
183+
logger.WithFields(logrus.Fields{"version": version, "path": configFilePath}).Debug("starting dxhd")
182184

183185
// catch these signals
184186
signals := make(chan os.Signal, 1)
@@ -193,13 +195,13 @@ toplevel:
193195
if len(data) == 0 {
194196
shell, err = parse(configFilePath, &data)
195197
if err != nil {
196-
zap.L().Fatal("failed to parse config", zap.String("file", configFilePath), zap.Error(err))
198+
logger.WithField("file", configFilePath).WithError(err).Fatal("failed to parse config")
197199
}
198200
}
199201

200202
X, err := xgbutil.NewConn()
201203
if err != nil {
202-
zap.L().Fatal("can not open connection to Xorg", zap.Error(err))
204+
logger.WithError(err).Fatal("can not open connection to Xorg")
203205
}
204206

205207
keybind.Initialize(X)
@@ -208,7 +210,7 @@ toplevel:
208210
for _, d := range data {
209211
err = listenKeybinding(X, errs, d.evtType, shell, d.binding.String(), d.action.String())
210212
if err != nil {
211-
zap.L().Info("can not register a keybinding", zap.String("keybinding", d.binding.String()), zap.Error(err))
213+
logger.WithField("keybinding", d.binding.String()).WithError(err).Warn("can not register a keybinding")
212214
}
213215
}
214216

@@ -220,18 +222,18 @@ toplevel:
220222
select {
221223
case err = <-errs:
222224
if err != nil {
223-
zap.L().Info("a command resulted into an error", zap.Error(err))
225+
logger.WithError(err).Warn("a command resulted into an error")
224226
}
225227
continue
226228
case sig := <-signals:
227229
keybind.Detach(X, X.RootWin())
228230
mousebind.Detach(X, X.RootWin())
229231
xevent.Quit(X)
230232
if sig == syscall.SIGUSR1 || sig == syscall.SIGUSR2 {
231-
zap.L().Debug("user defined signal received, reloading")
233+
logger.Debug("user defined signal received, reloading")
232234
continue toplevel
233235
}
234-
zap.L().Info("signal received, shutting down", zap.String("signal", sig.String()))
236+
logger.WithField("signal", sig.String()).Info("signal received, shutting down")
235237
if env, err := strconv.ParseBool(os.Getenv("STACKTRACE")); env && err == nil {
236238
buf := make([]byte, 1<<20)
237239
stackLen := runtime.Stack(buf, true)

parser.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"strconv"
1111
"strings"
1212

13-
"go.uber.org/zap"
13+
"github.com/sirupsen/logrus"
1414
)
1515

1616
// event definitions
@@ -79,7 +79,7 @@ func parse(file string, data *[]filedata) (shell string, err error) {
7979
if err == nil {
8080
err = e
8181
} else {
82-
zap.L().Debug("failed to close config file", zap.Error(e))
82+
logger.WithError(err).Debug("failed to close config file")
8383
}
8484
}
8585
}()
@@ -151,8 +151,8 @@ func parse(file string, data *[]filedata) (shell string, err error) {
151151
if wasKeybinding {
152152
if datum[index].binding.Len() != 0 {
153153
datum[index].binding.Reset()
154-
zap.L().Info("overwriting older keybinding", zap.String("file", file), zap.Int("line", lineNumber))
155-
zap.L().Debug("overwriting keybinding", zap.String("old", datum[index].binding.String()), zap.String("new", lineStr))
154+
logger.WithFields(logrus.Fields{"file": file, "line": lineNumber}).Info("overwriting keybinding")
155+
logger.WithFields(logrus.Fields{"old": datum[index].binding.String(), "new": lineStr}).Debug("overwriting keybinding")
156156
}
157157
}
158158

0 commit comments

Comments
 (0)