Skip to content

Commit

Permalink
Refactor logging.Format to expose constants (#3561)
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenButtolph authored Nov 21, 2024
1 parent 37da447 commit dbf95e0
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 19 deletions.
3 changes: 2 additions & 1 deletion config/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/ava-labs/avalanchego/utils/compression"
"github.com/ava-labs/avalanchego/utils/constants"
"github.com/ava-labs/avalanchego/utils/dynamicip"
"github.com/ava-labs/avalanchego/utils/logging"
"github.com/ava-labs/avalanchego/utils/ulimit"
"github.com/ava-labs/avalanchego/utils/units"
"github.com/ava-labs/avalanchego/vms/components/gas"
Expand Down Expand Up @@ -142,7 +143,7 @@ func addNodeFlags(fs *pflag.FlagSet) {
fs.String(LogsDirKey, defaultLogDir, "Logging directory for Avalanche")
fs.String(LogLevelKey, "info", "The log level. Should be one of {verbo, debug, trace, info, warn, error, fatal, off}")
fs.String(LogDisplayLevelKey, "", "The log display level. If left blank, will inherit the value of log-level. Otherwise, should be one of {verbo, debug, trace, info, warn, error, fatal, off}")
fs.String(LogFormatKey, "auto", "The structure of log format. Defaults to 'auto' which formats terminal-like logs, when the output is a terminal. Otherwise, should be one of {auto, plain, colors, json}")
fs.String(LogFormatKey, logging.AutoString, logging.FormatDescription)
fs.Uint(LogRotaterMaxSizeKey, 8, "The maximum file size in megabytes of the log file before it gets rotated.")
fs.Uint(LogRotaterMaxFilesKey, 7, "The maximum number of old log files to retain. 0 means retain all old log files.")
fs.Uint(LogRotaterMaxAgeKey, 0, "The maximum number of days to retain old log files based on the timestamp encoded in their filename. 0 means retain all old log files.")
Expand Down
2 changes: 1 addition & 1 deletion tests/fixture/bootstrapmonitor/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func main() {
rootCmd.PersistentFlags().StringVar(&podName, "pod-name", os.Getenv("POD_NAME"), "The name of the pod")
rootCmd.PersistentFlags().StringVar(&nodeContainerName, "node-container-name", "", "The name of the node container in the pod")
rootCmd.PersistentFlags().StringVar(&dataDir, "data-dir", "", "The path of the data directory used for the bootstrap job")
rootCmd.PersistentFlags().StringVar(&rawLogFormat, "log-format", "auto", "The structure of log format. Defaults to 'auto' which formats terminal-like logs, when the output is a terminal. Otherwise, should be one of {auto, plain, colors, json}")
rootCmd.PersistentFlags().StringVar(&rawLogFormat, "log-format", logging.AutoString, logging.FormatDescription)

versionCmd := &cobra.Command{
Use: "version",
Expand Down
43 changes: 26 additions & 17 deletions utils/logging/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,29 @@ import (

// Format modes available
const (
Plain Format = iota
Auto Format = iota
Plain
Colors
JSON

AutoString = "auto"
PlainString = "plain"
ColorsString = "colors"
JSONString = "json"

FormatDescription = "The structure of log format. Defaults to 'auto' which formats terminal-like logs, when the output is a terminal. Otherwise, should be one of {auto, plain, colors, json}"

termTimeFormat = "[01-02|15:04:05.000]"
)

var (
formatJSON = []string{
`"auto"`,
`"plain"`,
`"colors"`,
`"json"`,
}

errUnknownFormat = errors.New("unknown format")

defaultEncoderConfig = zapcore.EncoderConfig{
Expand Down Expand Up @@ -51,34 +66,28 @@ type Format int

// ToFormat chooses a highlighting mode
func ToFormat(h string, fd uintptr) (Format, error) {
switch strings.ToUpper(h) {
case "PLAIN":
return Plain, nil
case "COLORS":
return Colors, nil
case "JSON":
return JSON, nil
case "AUTO":
switch strings.ToLower(h) {
case AutoString:
if !term.IsTerminal(int(fd)) {
return Plain, nil
}
return Colors, nil
case PlainString:
return Plain, nil
case ColorsString:
return Colors, nil
case JSONString:
return JSON, nil
default:
return Plain, fmt.Errorf("unknown format mode: %s", h)
}
}

func (f Format) MarshalJSON() ([]byte, error) {
switch f {
case Plain:
return []byte(`"PLAIN"`), nil
case Colors:
return []byte(`"COLORS"`), nil
case JSON:
return []byte(`"JSON"`), nil
default:
if f < 0 || int(f) >= len(formatJSON) {
return nil, errUnknownFormat
}
return []byte(formatJSON[f]), nil
}

func (f Format) WrapPrefix(prefix string) string {
Expand Down

0 comments on commit dbf95e0

Please sign in to comment.