Skip to content

Commit

Permalink
feat: add global JSON log output
Browse files Browse the repository at this point in the history
  • Loading branch information
kamikazechaser committed Nov 30, 2024
1 parent b4f569b commit ad5b0eb
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 16 deletions.
20 changes: 14 additions & 6 deletions cmd/ge-publish/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var (
)

func main() {
container := container.NewContainer()
baseContainer := container.NewContainer()

app := &cli.App{
Name: "ge-publish",
Expand All @@ -25,7 +25,7 @@ func main() {
Name: "publish",
Aliases: []string{"p"},
Usage: "Publish smart contracts",
Subcommands: container.RegisterPublishCommands(),
Subcommands: baseContainer.RegisterPublishCommands(),
},
},
Flags: []cli.Flag{
Expand Down Expand Up @@ -77,12 +77,20 @@ func main() {
Usage: "Verbose logging",
EnvVars: []string{"DEBUG"},
},
&cli.BoolFlag{
Name: "json",
Value: false,
Usage: "JSON logging",
EnvVars: []string{"JSON"},
},
},
Before: func(cCtx *cli.Context) error {
if cCtx.Bool("vv") {
container.UseDebugMode()
}
container.Logg.Debug("ge-publish debug mode",
baseContainer.OverrideLogger(container.LoggOpts{
Debug: cCtx.Bool("vv"),
JSON: cCtx.Bool("json"),
})

baseContainer.Logg.Debug("ge-publish debug mode",
"version", cCtx.App.Version,
"rpc_endpoint", cCtx.String("rpc"),
"chainid", cCtx.Int64("chainid"),
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.23.1
require (
github.com/ethereum/go-ethereum v1.14.8
github.com/grassrootseconomics/ethutils v1.3.0
github.com/kamikazechaser/common v0.2.0
github.com/kamikazechaser/common v1.0.0
github.com/lmittmann/w3 v0.17.0
github.com/urfave/cli/v2 v2.27.4
)
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ github.com/holiman/uint256 v1.3.1 h1:JfTzmih28bittyHM8z360dCjIA9dbPIBlcTI6lmctQs
github.com/holiman/uint256 v1.3.1/go.mod h1:EOMSn4q6Nyt9P6efbI3bueV4e1b3dGlUCXeiRV4ng7E=
github.com/kamikazechaser/common v0.2.0 h1:bqi5UaMTDm/wtZlJEvQDNhsLVJP4Beg+HKWeQ+dhpss=
github.com/kamikazechaser/common v0.2.0/go.mod h1:I1LEc8+W+g/KHZWARc1gMhuSa2STbQgfL4Hao6I/ZwY=
github.com/kamikazechaser/common v1.0.0 h1:a/47O/TyQb417CUwsBDI7zlnJEnmhJz9czSPirpjlLg=
github.com/kamikazechaser/common v1.0.0/go.mod h1:I1LEc8+W+g/KHZWARc1gMhuSa2STbQgfL4Hao6I/ZwY=
github.com/klauspost/compress v1.16.0 h1:iULayQNOReoYUe+1qtKOqw9CwJv3aNQu8ivo7lw1HU4=
github.com/klauspost/compress v1.16.0/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
Expand Down
29 changes: 20 additions & 9 deletions internal/container/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,34 @@ import (
)

type (
LoggOpts struct {
JSON bool
Debug bool
}
Container struct {
Logg *slog.Logger
}
)

var loggOpts = logg.LoggOpts{
FormatType: logg.Human,
LogLevel: slog.LevelInfo,
}

func NewContainer() *Container {
return &Container{
Logg: logg.NewLogg(logg.LoggOpts{
FormatType: logg.Human,
LogLevel: slog.LevelInfo,
}),
Logg: logg.NewLogg(loggOpts),
}
}

func (c *Container) UseDebugMode() {
c.Logg = logg.NewLogg(logg.LoggOpts{
FormatType: logg.Human,
LogLevel: slog.LevelDebug,
})
func (c *Container) OverrideLogger(o LoggOpts) {
if o.JSON {
loggOpts.FormatType = logg.JSON
}

if o.Debug {
loggOpts.LogLevel = slog.LevelDebug
}

c.Logg = logg.NewLogg(loggOpts)
}

0 comments on commit ad5b0eb

Please sign in to comment.