-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf(log): Migrate logging backend to zerolog
- Loading branch information
Showing
28 changed files
with
235 additions
and
271 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
Oops, something went wrong.