Skip to content

Commit 11df311

Browse files
authored
Merge pull request #326 from wakatime/bugfix/logging#caller
Fix logging caller
2 parents c8d409b + c3d9bda commit 11df311

File tree

4 files changed

+51
-60
lines changed

4 files changed

+51
-60
lines changed

cmd/legacy/logfile/logfile.go

+9-7
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ import (
55
"os"
66
"path/filepath"
77

8+
"github.com/wakatime/wakatime-cli/pkg/vipertools"
9+
810
"github.com/mitchellh/go-homedir"
911
"github.com/spf13/viper"
10-
"github.com/wakatime/wakatime-cli/pkg/vipertools"
1112
)
1213

1314
const defaultFile = ".wakatime.log"
@@ -20,6 +21,11 @@ type Params struct {
2021

2122
// LoadParams loads needed data from the configuration file.
2223
func LoadParams(v *viper.Viper) (Params, error) {
24+
var debug bool
25+
if b := v.GetBool("settings.debug"); v.IsSet("settings.debug") {
26+
debug = b
27+
}
28+
2329
logFile, _ := vipertools.FirstNonEmptyString(v, "log-file", "logfile", "settings.log_file")
2430

2531
if logFile != "" {
@@ -30,7 +36,8 @@ func LoadParams(v *viper.Viper) (Params, error) {
3036
}
3137

3238
return Params{
33-
File: p,
39+
File: p,
40+
Verbose: v.GetBool("verbose") || debug,
3441
}, nil
3542
}
3643

@@ -54,11 +61,6 @@ func LoadParams(v *viper.Viper) (Params, error) {
5461
}
5562
}
5663

57-
var debug bool
58-
if b := v.GetBool("settings.debug"); v.IsSet("settings.debug") {
59-
debug = b
60-
}
61-
6264
return Params{
6365
File: filepath.Join(home, defaultFile),
6466
Verbose: v.GetBool("verbose") || debug,

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ require (
1515
github.com/spf13/cobra v1.1.1
1616
github.com/spf13/pflag v1.0.5
1717
github.com/spf13/viper v1.7.1
18-
github.com/stretchr/testify v1.6.1
18+
github.com/stretchr/testify v1.7.0
1919
github.com/yookoala/realpath v1.0.0
2020
go.etcd.io/bbolt v1.3.5
2121
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 // indirect

go.sum

+4
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,8 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV
204204
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
205205
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
206206
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
207+
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
208+
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
207209
github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s=
208210
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
209211
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
@@ -290,6 +292,8 @@ golang.org/x/sys v0.0.0-20200202164722-d101bd2416d5/go.mod h1:h1NjWce9XRLGQEsW7w
290292
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
291293
golang.org/x/sys v0.0.0-20201201145000-ef89a241ccb3 h1:kzM6+9dur93BcC2kVlYl34cHU+TYZLanmpSJHVMmL64=
292294
golang.org/x/sys v0.0.0-20201201145000-ef89a241ccb3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
295+
golang.org/x/sys v0.0.0-20210218155724-8ebf48af031b h1:lAZ0/chPUDWwjqosYR0X4M490zQhMsiJ4K3DbA7o+3g=
296+
golang.org/x/sys v0.0.0-20210218155724-8ebf48af031b/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
293297
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
294298
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
295299
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=

pkg/log/log.go

+37-52
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,41 @@
11
package log
22

33
import (
4+
"fmt"
45
"io"
56
"os"
7+
"runtime"
8+
"strings"
69

710
"github.com/wakatime/wakatime-cli/pkg/version"
811

912
l "github.com/sirupsen/logrus"
1013
)
1114

12-
// nolint
13-
var logEntry = new()
15+
// nolint:gochecknoglobals
16+
var (
17+
logEntry = new()
18+
// Debugf logs a message at level Debug.
19+
Debugf = logEntry.Debugf
20+
// Infof logs a message at level Info.
21+
Infof = logEntry.Infof
22+
// Warnf logs a message at level Warn.
23+
Warnf = logEntry.Warnf
24+
// Errorf logs a message at level Error.
25+
Errorf = logEntry.Errorf
26+
// Fatalf logs a message at level Fatal then the process will exit with status set to 1.
27+
Fatalf = logEntry.Fatalf
28+
// Debugln logs a message at level Debug.
29+
Debugln = logEntry.Debugln
30+
// Infoln logs a message at level Info.
31+
Infoln = logEntry.Infoln
32+
// Warnln logs a message at level Warn.
33+
Warnln = logEntry.Warnln
34+
// Errorln logs a message at level Error.
35+
Errorln = logEntry.Errorln
36+
// Fatalln logs a message at level Fatal then the process will exit with status set to 1.
37+
Fatalln = logEntry.Fatalln
38+
)
1439

1540
func new() *l.Entry {
1641
entry := l.NewEntry(&l.Logger{
@@ -22,6 +47,16 @@ func new() *l.Entry {
2247
l.FieldKeyMsg: "message",
2348
},
2449
DisableHTMLEscape: true,
50+
CallerPrettyfier: func(f *runtime.Frame) (string, string) {
51+
// Simplifies function description by removing package name from it.
52+
lastSlash := strings.LastIndexByte(f.Function, '/')
53+
if lastSlash < 0 {
54+
lastSlash = 0
55+
}
56+
lastDot := strings.LastIndexByte(f.Function[lastSlash:], '.') + lastSlash
57+
58+
return f.Function[lastDot+1:], fmt.Sprintf("%s:%d", f.File, f.Line)
59+
},
2560
},
2661
Level: l.InfoLevel,
2762
ExitFunc: os.Exit,
@@ -55,53 +90,3 @@ func WithField(key string, value interface{}) {
5590
func WithFields(fields map[string]interface{}) {
5691
logEntry.WithFields(fields)
5792
}
58-
59-
// Debugf logs a message at level Debug.
60-
func Debugf(format string, args ...interface{}) {
61-
logEntry.Debugf(format, args...)
62-
}
63-
64-
// Infof logs a message at level Info.
65-
func Infof(format string, args ...interface{}) {
66-
logEntry.Infof(format, args...)
67-
}
68-
69-
// Warnf logs a message at level Warn.
70-
func Warnf(format string, args ...interface{}) {
71-
logEntry.Warnf(format, args...)
72-
}
73-
74-
// Errorf logs a message at level Error.
75-
func Errorf(format string, args ...interface{}) {
76-
logEntry.Errorf(format, args...)
77-
}
78-
79-
// Fatalf logs a message at level Fatal then the process will exit with status set to 1.
80-
func Fatalf(format string, args ...interface{}) {
81-
logEntry.Fatalf(format, args...)
82-
}
83-
84-
// Debugln logs a message at level Debug.
85-
func Debugln(args ...interface{}) {
86-
logEntry.Debugln(args...)
87-
}
88-
89-
// Infoln logs a message at level Info.
90-
func Infoln(args ...interface{}) {
91-
logEntry.Infoln(args...)
92-
}
93-
94-
// Warnln logs a message at level Warn.
95-
func Warnln(args ...interface{}) {
96-
logEntry.Warnln(args...)
97-
}
98-
99-
// Errorln logs a message at level Error.
100-
func Errorln(args ...interface{}) {
101-
logEntry.Errorln(args...)
102-
}
103-
104-
// Fatalln logs a message at level Fatal then the process will exit with status set to 1.
105-
func Fatalln(args ...interface{}) {
106-
logEntry.Fatalln(args...)
107-
}

0 commit comments

Comments
 (0)