Skip to content

Commit

Permalink
Introduce human-friendly log formatting and better logger configurati…
Browse files Browse the repository at this point in the history
…on (#5)

* New version of `logger` delivering better human-readable formatting and configuration

* New version of `run` using new `logger`

* Fix tests in `parallel`

* Use `github.com/pkg/errors` to produce errors containing stack traces
  • Loading branch information
wojtek-coreum authored May 30, 2022
1 parent 005a77e commit a538c43
Show file tree
Hide file tree
Showing 16 changed files with 756 additions and 89 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ module github.com/CoreumFoundation/coreum-tools
go 1.17

require (
github.com/pkg/errors v0.9.1
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.7.0
go.uber.org/zap v1.21.0
)
Expand Down
5 changes: 4 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
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/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.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/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
Expand Down
13 changes: 7 additions & 6 deletions pkg/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package build

import (
"context"
"errors"
"fmt"
"os"
"reflect"
"strconv"
"strings"

"github.com/pkg/errors"

"github.com/CoreumFoundation/coreum-tools/pkg/ioc"
)

Expand Down Expand Up @@ -65,7 +66,7 @@ func (e *iocExecutor) Execute(ctx context.Context, paths []string) error {
}
err = err2
} else {
err = fmt.Errorf("command panicked: %v", r)
err = errors.Errorf("command panicked: %v", r)
}
errChan <- err
close(errChan)
Expand Down Expand Up @@ -130,7 +131,7 @@ func (e *iocExecutor) Execute(ctx context.Context, paths []string) error {
initDeps := make([]interface{}, 0, len(paths))
for _, p := range paths {
if e.commands[p] == nil {
return fmt.Errorf("build: command %s does not exist", p)
return errors.Errorf("build: command %s does not exist", p)
}
initDeps = append(initDeps, e.commands[p])
}
Expand Down Expand Up @@ -167,14 +168,14 @@ func Autocomplete(executor Executor) bool {
}

// Do receives configuration and runs commands
func Do(ctx context.Context, name string, executor Executor) error {
func Do(ctx context.Context, name string, paths []string, executor Executor) error {
if len(os.Args) == 1 {
if _, err := fmt.Fprintf(os.Stderr, help, name, os.Args[0]); err != nil {
return err
return errors.WithStack(err)
}
return nil
}
return execute(ctx, os.Args[1:], executor)
return execute(ctx, paths, executor)
}

func execute(ctx context.Context, paths []string, executor Executor) error {
Expand Down
5 changes: 3 additions & 2 deletions pkg/build/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package build

import (
"context"
"errors"
"testing"

"github.com/CoreumFoundation/coreum-tools/pkg/ioc"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/CoreumFoundation/coreum-tools/pkg/ioc"
)

type report map[int]string
Expand Down
5 changes: 3 additions & 2 deletions pkg/ioc/container_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package ioc

import (
"errors"
"fmt"
"testing"

"github.com/pkg/errors"
"github.com/stretchr/testify/assert"

pkgA "github.com/CoreumFoundation/coreum-tools/pkg/ioc/test/a/pkg"
pkgB "github.com/CoreumFoundation/coreum-tools/pkg/ioc/test/b/pkg"
"github.com/stretchr/testify/assert"
)

type Shape interface {
Expand Down
3 changes: 2 additions & 1 deletion pkg/libexec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os/exec"
"syscall"

"github.com/pkg/errors"
"go.uber.org/zap"

"github.com/CoreumFoundation/coreum-tools/pkg/logger"
Expand Down Expand Up @@ -44,7 +45,7 @@ func Exec(ctx context.Context, cmds ...*exec.Cmd) error {
logger.Get(ctx).Debug("Executing command", zap.Stringer("command", cmd))

if err := cmd.Start(); err != nil {
return err
return errors.WithStack(err)
}

err := parallel.Run(ctx, func(ctx context.Context, spawn parallel.SpawnFn) error {
Expand Down
75 changes: 75 additions & 0 deletions pkg/logger/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package logger

import (
"os"

"github.com/pkg/errors"
"github.com/spf13/pflag"

"github.com/CoreumFoundation/coreum-tools/pkg/must"
)

// Format defines the format of log output
type Format string

const (
// FormatConsole causes logs to be printed in human-readable form
FormatConsole Format = "console"

// FormatJSON causes logs to be printed in JSON
FormatJSON Format = "json"
)

// Config stores configuration of the logger
type Config struct {
// Format defines the format of log output
Format Format

// Verbose turns on verbose logging
Verbose bool
}

// ToolDefaultConfig stores handy default configuration used by tools run manually by humans
var ToolDefaultConfig = Config{
Format: FormatConsole,
Verbose: false,
}

// ServiceDefaultConfig stores handy default configuration used by services
var ServiceDefaultConfig = Config{
Format: FormatJSON,
Verbose: true,
}

// ConfigureWithCLI configures logger based on CLI flags
func ConfigureWithCLI(defaultConfig Config) Config {
var format string
flags := pflag.NewFlagSet("logger", pflag.ContinueOnError)
flags.ParseErrorsWhitelist.UnknownFlags = true
AddFlags(defaultConfig, flags)
// Dummy flag to turn off printing usage of this flag set
flags.BoolP("help", "h", false, "")

_ = flags.Parse(os.Args[1:])

defaultConfig.Format = Format(must.String(flags.GetString("log-format")))
defaultConfig.Verbose = must.Bool(flags.GetBool("verbose"))
if defaultConfig.Format != FormatConsole && defaultConfig.Format != FormatJSON {
panic(errors.Errorf("incorrect logging format %s", format))
}

return defaultConfig
}

// Flags returns new flag set preconfigured with logger-specific options
func Flags(defaultConfig Config, name string) *pflag.FlagSet {
flags := pflag.NewFlagSet(name, pflag.ContinueOnError)
AddFlags(defaultConfig, flags)
return flags
}

// AddFlags adds flags defined by logger
func AddFlags(defaultConfig Config, flags *pflag.FlagSet) {
flags.String("log-format", string(defaultConfig.Format), "Format of log output: console | json")
flags.BoolP("verbose", "v", defaultConfig.Verbose, "Turns on verbose logging")
}
Loading

0 comments on commit a538c43

Please sign in to comment.