Skip to content

Commit ad5b0eb

Browse files
feat: add global JSON log output
1 parent b4f569b commit ad5b0eb

File tree

4 files changed

+37
-16
lines changed

4 files changed

+37
-16
lines changed

cmd/ge-publish/cli.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ var (
1414
)
1515

1616
func main() {
17-
container := container.NewContainer()
17+
baseContainer := container.NewContainer()
1818

1919
app := &cli.App{
2020
Name: "ge-publish",
@@ -25,7 +25,7 @@ func main() {
2525
Name: "publish",
2626
Aliases: []string{"p"},
2727
Usage: "Publish smart contracts",
28-
Subcommands: container.RegisterPublishCommands(),
28+
Subcommands: baseContainer.RegisterPublishCommands(),
2929
},
3030
},
3131
Flags: []cli.Flag{
@@ -77,12 +77,20 @@ func main() {
7777
Usage: "Verbose logging",
7878
EnvVars: []string{"DEBUG"},
7979
},
80+
&cli.BoolFlag{
81+
Name: "json",
82+
Value: false,
83+
Usage: "JSON logging",
84+
EnvVars: []string{"JSON"},
85+
},
8086
},
8187
Before: func(cCtx *cli.Context) error {
82-
if cCtx.Bool("vv") {
83-
container.UseDebugMode()
84-
}
85-
container.Logg.Debug("ge-publish debug mode",
88+
baseContainer.OverrideLogger(container.LoggOpts{
89+
Debug: cCtx.Bool("vv"),
90+
JSON: cCtx.Bool("json"),
91+
})
92+
93+
baseContainer.Logg.Debug("ge-publish debug mode",
8694
"version", cCtx.App.Version,
8795
"rpc_endpoint", cCtx.String("rpc"),
8896
"chainid", cCtx.Int64("chainid"),

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ go 1.23.1
55
require (
66
github.com/ethereum/go-ethereum v1.14.8
77
github.com/grassrootseconomics/ethutils v1.3.0
8-
github.com/kamikazechaser/common v0.2.0
8+
github.com/kamikazechaser/common v1.0.0
99
github.com/lmittmann/w3 v0.17.0
1010
github.com/urfave/cli/v2 v2.27.4
1111
)

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ github.com/holiman/uint256 v1.3.1 h1:JfTzmih28bittyHM8z360dCjIA9dbPIBlcTI6lmctQs
8080
github.com/holiman/uint256 v1.3.1/go.mod h1:EOMSn4q6Nyt9P6efbI3bueV4e1b3dGlUCXeiRV4ng7E=
8181
github.com/kamikazechaser/common v0.2.0 h1:bqi5UaMTDm/wtZlJEvQDNhsLVJP4Beg+HKWeQ+dhpss=
8282
github.com/kamikazechaser/common v0.2.0/go.mod h1:I1LEc8+W+g/KHZWARc1gMhuSa2STbQgfL4Hao6I/ZwY=
83+
github.com/kamikazechaser/common v1.0.0 h1:a/47O/TyQb417CUwsBDI7zlnJEnmhJz9czSPirpjlLg=
84+
github.com/kamikazechaser/common v1.0.0/go.mod h1:I1LEc8+W+g/KHZWARc1gMhuSa2STbQgfL4Hao6I/ZwY=
8385
github.com/klauspost/compress v1.16.0 h1:iULayQNOReoYUe+1qtKOqw9CwJv3aNQu8ivo7lw1HU4=
8486
github.com/klauspost/compress v1.16.0/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE=
8587
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=

internal/container/container.go

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,34 @@ import (
77
)
88

99
type (
10+
LoggOpts struct {
11+
JSON bool
12+
Debug bool
13+
}
1014
Container struct {
1115
Logg *slog.Logger
1216
}
1317
)
1418

19+
var loggOpts = logg.LoggOpts{
20+
FormatType: logg.Human,
21+
LogLevel: slog.LevelInfo,
22+
}
23+
1524
func NewContainer() *Container {
1625
return &Container{
17-
Logg: logg.NewLogg(logg.LoggOpts{
18-
FormatType: logg.Human,
19-
LogLevel: slog.LevelInfo,
20-
}),
26+
Logg: logg.NewLogg(loggOpts),
2127
}
2228
}
2329

24-
func (c *Container) UseDebugMode() {
25-
c.Logg = logg.NewLogg(logg.LoggOpts{
26-
FormatType: logg.Human,
27-
LogLevel: slog.LevelDebug,
28-
})
30+
func (c *Container) OverrideLogger(o LoggOpts) {
31+
if o.JSON {
32+
loggOpts.FormatType = logg.JSON
33+
}
34+
35+
if o.Debug {
36+
loggOpts.LogLevel = slog.LevelDebug
37+
}
38+
39+
c.Logg = logg.NewLogg(loggOpts)
2940
}

0 commit comments

Comments
 (0)