Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor logging.Format to expose constants #3561

Merged
merged 3 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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, "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}")
StephenButtolph marked this conversation as resolved.
Show resolved Hide resolved
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, "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}")

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

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

AutoString = "auto"
PlainString = "plain"
ColorsString = "colors"
JSONString = "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 +64,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
Loading