Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding pretty structured logging support #265

Merged
merged 1 commit into from
May 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 53 additions & 15 deletions cmd/mockery/mockery.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
package main

import (
"context"
"flag"
"fmt"
"os"
"regexp"
"runtime/pprof"
"strings"
"syscall"
"time"

"github.com/pkg/errors"
"github.com/rs/zerolog"
"github.com/vektra/mockery/mockery"
"golang.org/x/crypto/ssh/terminal"
)

const regexMetadataChars = "\\.+*?()|[]{}^$"
Expand All @@ -33,6 +38,7 @@ type Config struct {
buildTags string
fFileName string
fStructName string
fLogLevel string
}

func main() {
Expand All @@ -46,26 +52,31 @@ func main() {
if config.quiet {
// if "quiet" flag is set, set os.Stdout to /dev/null to suppress all output to Stdout
os.Stdout = os.NewFile(uintptr(syscall.Stdout), os.DevNull)
config.fLogLevel = ""
}

log, err := getLogger(config.fLogLevel)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to initialize logger: %v\n", err)
os.Exit(1)
}
log.Info().Msgf("Starting mockery")
ctx := log.WithContext(context.Background())

if config.fVersion {
fmt.Println(mockery.SemVer)
return
} else if config.fName != "" && config.fAll {
fmt.Fprintln(os.Stderr, "Specify -name or -all, but not both")
os.Exit(1)
log.Fatal().Msgf("Specify -name or -all, but not both")
} else if (config.fFileName != "" || config.fStructName != "") && config.fAll {
fmt.Fprintln(os.Stderr, "Cannot specify -filename or -structname with -all")
os.Exit(1)
log.Fatal().Msgf("Cannot specify -filename or -structname with -all")
} else if config.fName != "" {
recursive = config.fRecursive
if strings.ContainsAny(config.fName, regexMetadataChars) {
if filter, err = regexp.Compile(config.fName); err != nil {
fmt.Fprintln(os.Stderr, "Invalid regular expression provided to -name")
os.Exit(1)
log.Fatal().Err(err).Msgf("Invalid regular expression provided to -name")
} else if config.fFileName != "" || config.fStructName != "" {
fmt.Fprintln(os.Stderr, "Cannot specify -filename or -structname with regex in -name")
os.Exit(1)
log.Fatal().Msgf("Cannot specify -filename or -structname with regex in -name")
}
} else {
filter = regexp.MustCompile(fmt.Sprintf("^%s$", config.fName))
Expand All @@ -75,8 +86,7 @@ func main() {
recursive = true
filter = regexp.MustCompile(".*")
} else {
fmt.Fprintln(os.Stderr, "Use -name to specify the name of the interface or -all for all interfaces found")
os.Exit(1)
log.Fatal().Msgf("Use -name to specify the name of the interface or -all for all interfaces found")
}

if config.fkeepTree {
Expand All @@ -86,8 +96,7 @@ func main() {
if config.fProfile != "" {
f, err := os.Create(config.fProfile)
if err != nil {
os.Exit(1)
return
log.Fatal().Err(err).Msgf("Failed to create profile file")
}

pprof.StartCPUProfile(f)
Expand Down Expand Up @@ -125,11 +134,10 @@ func main() {
BuildTags: strings.Split(config.buildTags, " "),
}

generated := walker.Walk(visitor)
generated := walker.Walk(ctx, visitor)

if config.fName != "" && !generated {
fmt.Printf("Unable to find %s in any go files under this path\n", config.fName)
os.Exit(1)
log.Fatal().Msgf("Unable to find '%s' in any go files under this path", config.fName)
}
}

Expand All @@ -156,8 +164,38 @@ func parseConfigFromArgs(args []string) Config {
flagSet.StringVar(&config.buildTags, "tags", "", "space-separated list of additional build tags to use")
flagSet.StringVar(&config.fFileName, "filename", "", "name of generated file (only works with -name and no regex)")
flagSet.StringVar(&config.fStructName, "structname", "", "name of generated struct (only works with -name and no regex)")
flagSet.StringVar(&config.fLogLevel, "log-level", "info", "Level of logging")

flagSet.Parse(args[1:])

return config
}

type timeHook struct{}

func (t timeHook) Run(e *zerolog.Event, level zerolog.Level, msg string) {
e.Time("time", time.Now())
}

func getLogger(levelStr string) (zerolog.Logger, error) {
level, err := zerolog.ParseLevel(levelStr)
if err != nil {
return zerolog.Logger{}, errors.Wrapf(err, "Couldn't parse log level")
}
out := os.Stderr
writer := zerolog.ConsoleWriter{
Out: out,
TimeFormat: time.RFC822,
}
if !terminal.IsTerminal(int(out.Fd())) {
writer.NoColor = true
}
log := zerolog.New(writer).
Hook(timeHook{}).
Level(level).
With().
Str("version", mockery.SemVer).
Logger()

return log, nil
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ go 1.14

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pkg/errors v0.8.1
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rs/zerolog v1.18.0
github.com/stretchr/objx v0.1.1 // indirect
github.com/stretchr/testify v1.2.2
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550
golang.org/x/tools v0.0.0-20200323144430-8dcfad9e016e
)
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rs/xid v1.2.1/go.mod h1:+uKXf+4Djp6Md1KODXJxgGQPKngRmWyn10oCKFzNHOQ=
github.com/rs/zerolog v1.18.0 h1:CbAm3kP2Tptby1i9sYy2MGRg0uxIN9cyDb59Ys7W8z8=
github.com/rs/zerolog v1.18.0/go.mod h1:9nvC1axdVrAHcu/s9taAVfBuIdTZLVQmKQyvrUjF5+I=
github.com/stretchr/objx v0.1.1 h1:2vfRuCMp5sSVIDSqO8oNnWJq7mPa6KVP3iPIwFBuy8A=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/zenazn/goji v0.9.0/go.mod h1:7S9M489iMyHBNxwZnk9/EHS098H4/F6TATF2mIxtB1Q=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550 h1:ObdrDkeb4kJdCP557AjRjq69pTHfNouLtWZG7j9rPN8=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/mod v0.2.0 h1:KU7oHjnv3XNWfa5COkzUifxZmxp1TyI7ImMXqFxLwvQ=
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
Expand All @@ -17,10 +25,12 @@ golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLL
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d h1:+R4KGOnez64A81RvjARKc4UT5/tI9ujCIVX+P5KiHuI=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20181112210238-4b1f3b6b1646 h1:JEEoTsNEpPwxsebhPLC6P2jNr+6RFZLY4elUBVcMb+I=
golang.org/x/tools v0.0.0-20181112210238-4b1f3b6b1646/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190828213141-aed303cbaa74/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20200323144430-8dcfad9e016e h1:ssd5ulOvVWlh4kDSUF2SqzmMeWfjmwDXM+uGw/aQjRE=
golang.org/x/tools v0.0.0-20200323144430-8dcfad9e016e/go.mod h1:Sl4aGygMT6LrqrWclx+PTx3U+LnKx/seiNR+3G19Ar8=
Expand Down
Loading