Skip to content

Commit

Permalink
perf(log): Migrate logging backend to zerolog
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe565 committed Apr 28, 2024
1 parent cbb74bf commit cdba115
Show file tree
Hide file tree
Showing 28 changed files with 235 additions and 271 deletions.
2 changes: 1 addition & 1 deletion cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func preRun(cmd *cobra.Command, _ []string) error {
if err := loadFlagEnvs(cmd.Flags()); err != nil {
return err
}
config.InitLog(cmd.Flags())
config.InitLog(cmd)
return nil
}

Expand Down
2 changes: 0 additions & 2 deletions cmd/get/stream/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (

"github.com/gabe565/ascii-movie/internal/config"
"github.com/gabe565/ascii-movie/internal/server"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -69,7 +68,6 @@ func run(cmd *cobra.Command, _ []string) error {

resp, err := http.DefaultClient.Do(req)
if err != nil {
log.Error("Failed to connect to API. Is the server running?")
return fmt.Errorf("failed to connect to API: %w", err)
}
defer func() {
Expand Down
4 changes: 2 additions & 2 deletions cmd/ls/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

"github.com/dustin/go-humanize"
"github.com/gabe565/ascii-movie/internal/movie"
log "github.com/sirupsen/logrus"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
)

Expand All @@ -33,7 +33,7 @@ func run(cmd *cobra.Command, args []string) error {
for _, arg := range args {
movieInfo, err := movie.GetInfo(nil, arg)
if err != nil {
log.WithError(err).WithField("path", arg).Warn("failed to get movie info")
log.Warn().Err(err).Str("path", arg).Msg("Failed to get movie info")
continue
}
movieInfos = append(movieInfos, movieInfo)
Expand Down
7 changes: 4 additions & 3 deletions cmd/play/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import (
tea "github.com/charmbracelet/bubbletea"
"github.com/gabe565/ascii-movie/internal/config"
"github.com/gabe565/ascii-movie/internal/movie"
log "github.com/sirupsen/logrus"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
)

Expand All @@ -25,7 +26,7 @@ func NewCommand() *cobra.Command {

func run(cmd *cobra.Command, args []string) error {
if !cmd.Flags().Changed(config.LogLevelFlag) {
log.SetLevel(log.WarnLevel)
zerolog.SetGlobalLevel(zerolog.WarnLevel)
}

var path string
Expand All @@ -39,7 +40,7 @@ func run(cmd *cobra.Command, args []string) error {
}

program := tea.NewProgram(
movie.NewPlayer(&m, nil, nil),
movie.NewPlayer(&m, log.Level(zerolog.ErrorLevel), nil),
tea.WithAltScreen(),
tea.WithMouseCellMotion(),
)
Expand Down
4 changes: 2 additions & 2 deletions docs/ascii-movie.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ Command line ASCII movie player.

```
-h, --help help for ascii-movie
--log-format string log formatter (text, json) (default "text")
-l, --log-level string log level (trace, debug, info, warning, error, fatal, panic) (default "info")
--log-format string log formatter (auto, color, plain, json) (default "auto")
-l, --log-level string log level (trace, debug, info, warn, error, fatal, panic) (default "info")
```

### SEE ALSO
Expand Down
4 changes: 2 additions & 2 deletions docs/ascii-movie_get.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ Fetches data from a running server.
### Options inherited from parent commands

```
--log-format string log formatter (text, json) (default "text")
-l, --log-level string log level (trace, debug, info, warning, error, fatal, panic) (default "info")
--log-format string log formatter (auto, color, plain, json) (default "auto")
-l, --log-level string log level (trace, debug, info, warn, error, fatal, panic) (default "info")
```

### SEE ALSO
Expand Down
4 changes: 2 additions & 2 deletions docs/ascii-movie_get_stream.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ ascii-movie get stream [flags]

```
--api-address string API address (default "http://127.0.0.1:1977")
--log-format string log formatter (text, json) (default "text")
-l, --log-level string log level (trace, debug, info, warning, error, fatal, panic) (default "info")
--log-format string log formatter (auto, color, plain, json) (default "auto")
-l, --log-level string log level (trace, debug, info, warn, error, fatal, panic) (default "info")
```

### SEE ALSO
Expand Down
4 changes: 2 additions & 2 deletions docs/ascii-movie_ls.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ ascii-movie ls [PATH]... [flags]
### Options inherited from parent commands

```
--log-format string log formatter (text, json) (default "text")
-l, --log-level string log level (trace, debug, info, warning, error, fatal, panic) (default "info")
--log-format string log formatter (auto, color, plain, json) (default "auto")
-l, --log-level string log level (trace, debug, info, warn, error, fatal, panic) (default "info")
```

### SEE ALSO
Expand Down
4 changes: 2 additions & 2 deletions docs/ascii-movie_play.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ ascii-movie play [movie] [flags]
### Options inherited from parent commands

```
--log-format string log formatter (text, json) (default "text")
-l, --log-level string log level (trace, debug, info, warning, error, fatal, panic) (default "info")
--log-format string log formatter (auto, color, plain, json) (default "auto")
-l, --log-level string log level (trace, debug, info, warn, error, fatal, panic) (default "info")
```

### SEE ALSO
Expand Down
4 changes: 2 additions & 2 deletions docs/ascii-movie_serve.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ ascii-movie serve [movie] [flags]
### Options inherited from parent commands

```
--log-format string log formatter (text, json) (default "text")
-l, --log-level string log level (trace, debug, info, warning, error, fatal, panic) (default "info")
--log-format string log formatter (auto, color, plain, json) (default "auto")
-l, --log-level string log level (trace, debug, info, warn, error, fatal, panic) (default "info")
```

### SEE ALSO
Expand Down
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ require (
github.com/charmbracelet/wish v1.4.0
github.com/dustin/go-humanize v1.0.1
github.com/lrstanley/bubblezone v0.0.0-20240125042004-b7bafc493195
github.com/mattn/go-isatty v0.0.20
github.com/muesli/termenv v0.15.2
github.com/prometheus/client_golang v1.19.0
github.com/sirupsen/logrus v1.9.3
github.com/rs/zerolog v1.32.0
github.com/spf13/cobra v1.8.0
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.9.0
Expand All @@ -37,7 +38,7 @@ require (
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-localereader v0.0.1 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 // indirect
Expand Down
19 changes: 12 additions & 7 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,20 @@ github.com/charmbracelet/x/exp/term v0.0.0-20240408110044-525ba71bb562 h1:jCSNgV
github.com/charmbracelet/x/exp/term v0.0.0-20240408110044-525ba71bb562/go.mod h1:yQqGHmheaQfkqiJWjklPHVAq1dKbk8uGbcoS/lcKCJ0=
github.com/containerd/console v1.0.4 h1:F2g4+oChYvBTsASRTz8NP6iIAi97J3TtSAsLbIFn4ro=
github.com/containerd/console v1.0.4/go.mod h1:YynlIjWYF8myEu6sdkwKIvGQq+cOckRm6So2avqoYAk=
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/cpuguy83/go-md2man/v2 v2.0.4 h1:wfIWP927BUkWJb2NmU/kNDYIBTh/ziUX91+lVfRxZq4=
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/creack/pty v1.1.21 h1:1/QdRyBaHHJP61QkWMXlOIBfsgdDeeKfK8SYVUWJKf0=
github.com/creack/pty v1.1.21/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr4O4=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
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/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
github.com/go-logfmt/logfmt v0.6.0 h1:wGYYu3uicYdqXVgoYbvnkrPVXkuLM1p1ifugDMEdRi4=
github.com/go-logfmt/logfmt v0.6.0/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
Expand All @@ -51,6 +52,10 @@ github.com/lrstanley/bubblezone v0.0.0-20240125042004-b7bafc493195 h1:zcxmFnwisG
github.com/lrstanley/bubblezone v0.0.0-20240125042004-b7bafc493195/go.mod h1:v5lEwWaguF1o2MW/ucO0ZIA/IZymdBYJJ+2cMRLE7LU=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-localereader v0.0.1 h1:ygSAOl7ZXTx4RdPYinUpg6W99U8jWvWi9Ye2JC/oIi4=
Expand All @@ -66,6 +71,7 @@ github.com/muesli/reflow v0.3.0 h1:IFsN6K9NfGtjeggFP+68I4chLZV2yIKsXJFNZ+eWh6s=
github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKtnHY/8=
github.com/muesli/termenv v0.15.2 h1:GohcuySI0QmI3wN8Ok9PtKGkgkFIk7y6Vpb5PvrY+Wo=
github.com/muesli/termenv v0.15.2/go.mod h1:Epx+iuz8sNs7mNKhxzH4fWXGNpZwUaJKRS1noLXviQ8=
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/prometheus/client_golang v1.19.0 h1:ygXvpU1AoN1MhdzckN+PyD9QJOSD4x7kmXYlnfbA6JU=
Expand All @@ -82,16 +88,15 @@ github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8=
github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4=
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.32.0 h1:keLypqrlIjaFsbmJOBdB/qvyF8KEtCWHwobLp5l/mQ0=
github.com/rs/zerolog v1.32.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0=
github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho=
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.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/crypto v0.22.0 h1:g1v0xeRhjcugydODzvb3mEM9SQ0HGp9s/nh3COQ/C30=
Expand All @@ -100,9 +105,10 @@ golang.org/x/exp v0.0.0-20240416160154-fe59bbe5cc7f h1:99ci1mjWVBWwJiEKYY6jWa4d2
golang.org/x/exp v0.0.0-20240416160154-fe59bbe5cc7f/go.mod h1:/lliqkxwWAhPjf5oSOIJup2XcqJaw8RGS6k3TGEc7GI=
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o=
golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.19.0 h1:+ThwsDv+tYfnJFhF4L8jITxu1tdTWRTZpdsWgEgjL6Q=
Expand All @@ -114,6 +120,5 @@ google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHh
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
106 changes: 69 additions & 37 deletions internal/config/flag_log.go
Original file line number Diff line number Diff line change
@@ -1,86 +1,118 @@
package config

import (
log "github.com/sirupsen/logrus"
"fmt"
"io"
"os"
"time"

"github.com/charmbracelet/lipgloss"
"github.com/mattn/go-isatty"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
flag "github.com/spf13/pflag"
)

const (
LogLevelFlag = "log-level"
DefaultLogLevel = log.InfoLevel
DefaultLogLevel = zerolog.InfoLevel

LogFormatFlag = "log-format"
DefaultLogFormat = "text"
DefaultLogFormat = "auto"
)

func RegisterLogFlags(cmd *cobra.Command) {
cmd.PersistentFlags().StringP(
LogLevelFlag,
"l",
DefaultLogLevel.String(),
"log level (trace, debug, info, warning, error, fatal, panic)",
"log level (trace, debug, info, warn, error, fatal, panic)",
)
if err := cmd.RegisterFlagCompletionFunc(
LogLevelFlag,
func(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
return []string{
log.TraceLevel.String(),
log.DebugLevel.String(),
log.InfoLevel.String(),
log.WarnLevel.String(),
log.ErrorLevel.String(),
log.FatalLevel.String(),
log.PanicLevel.String(),
zerolog.TraceLevel.String(),
zerolog.DebugLevel.String(),
zerolog.InfoLevel.String(),
zerolog.WarnLevel.String(),
zerolog.ErrorLevel.String(),
zerolog.FatalLevel.String(),
zerolog.PanicLevel.String(),
}, cobra.ShellCompDirectiveNoFileComp
},
); err != nil {
panic(err)
}

cmd.PersistentFlags().String(LogFormatFlag, DefaultLogFormat, "log formatter (text, json)")
cmd.PersistentFlags().String(LogFormatFlag, DefaultLogFormat, "log formatter (auto, color, plain, json)")
if err := cmd.RegisterFlagCompletionFunc(
LogFormatFlag,
func(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
return []string{"text", "json"}, cobra.ShellCompDirectiveNoFileComp
return []string{"auto", "color", "plain", "json"}, cobra.ShellCompDirectiveNoFileComp
},
); err != nil {
panic(err)
}
}

func InitLog(flags *flag.FlagSet) {
logLevel, err := flags.GetString(LogLevelFlag)
if err != nil {
panic(err)
func logLevel(level string) zerolog.Level {
parsedLevel, err := zerolog.ParseLevel(level)
if err != nil || parsedLevel == zerolog.NoLevel {
if level == "warning" {
parsedLevel = zerolog.WarnLevel
} else {
log.Warn().Str("value", level).Msg("Invalid log level. Defaulting to info.")
parsedLevel = zerolog.InfoLevel
}
}
return parsedLevel
}

if parsedLevel, err := log.ParseLevel(logLevel); err == nil {
log.SetLevel(parsedLevel)
} else {
parsedLevel = log.InfoLevel
func logFormat(out io.Writer, format string) io.Writer {
switch format {
case "json", "j":
return out
default:
style := lipgloss.NewStyle().Bold(true)
var useColor bool
switch format {
case "auto", "text":
if w, ok := out.(*os.File); ok {
useColor = isatty.IsTerminal(w.Fd())
}
case "color":
useColor = true
case "plain":
default:
log.Warn().Str("value", format).Msg("Invalid log formatter. Defaulting to auto.")
}

log.WithField(LogLevelFlag, logLevel).Warn("invalid log level. defaulting to info.")
if err = flags.Set(LogLevelFlag, "info"); err != nil {
panic(err)
return zerolog.ConsoleWriter{
Out: out,
NoColor: !useColor,
TimeFormat: time.DateTime,
FormatMessage: func(i interface{}) string {
msg := fmt.Sprintf("%-25s", i)
if useColor {
return style.Render(msg)
}
return msg
},
}
log.SetLevel(parsedLevel)
}
}

logFormat, err := flags.GetString(LogFormatFlag)
func InitLog(cmd *cobra.Command) {
level, err := cmd.Flags().GetString("log-level")
if err != nil {
panic(err)
}
zerolog.SetGlobalLevel(logLevel(level))

switch logFormat {
case "text", "txt", "t":
log.SetFormatter(&log.TextFormatter{})
case "json", "j":
log.SetFormatter(&log.JSONFormatter{})
default:
log.WithField(LogFormatFlag, logFormat).Warn("invalid log formatter. defaulting to text.")
if err = flags.Set(LogFormatFlag, "text"); err != nil {
panic(err)
}
format, err := cmd.Flags().GetString("log-format")
if err != nil {
panic(err)
}
log.Logger = log.Output(logFormat(cmd.ErrOrStderr(), format))
}
Loading

0 comments on commit cdba115

Please sign in to comment.