From e312acb84949bce34737f2c80fbef0a9f49fa7f5 Mon Sep 17 00:00:00 2001 From: Stephen Buttolph Date: Thu, 21 Nov 2024 12:42:01 -0500 Subject: [PATCH 1/4] Convert antithesis tests to use JSON logging --- config/flags.go | 3 ++- tests/antithesis/compose.go | 1 + utils/logging/format.go | 41 ++++++++++++++++++++++--------------- 3 files changed, 27 insertions(+), 18 deletions(-) diff --git a/config/flags.go b/config/flags.go index d8397ce40a2c..180b06fbccec 100644 --- a/config/flags.go +++ b/config/flags.go @@ -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" @@ -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}") 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.") diff --git a/tests/antithesis/compose.go b/tests/antithesis/compose.go index 3b25eb48d535..b1f00fa6cbe1 100644 --- a/tests/antithesis/compose.go +++ b/tests/antithesis/compose.go @@ -155,6 +155,7 @@ func newComposeProject(network *tmpnet.Network, nodeImageName string, workloadIm config.NetworkNameKey: constants.LocalName, config.LogLevelKey: logging.Debug.String(), config.LogDisplayLevelKey: logging.Trace.String(), + config.LogFormatKey: logging.JSONString, config.HTTPHostKey: "0.0.0.0", config.PublicIPKey: address, config.StakingTLSKeyContentKey: tlsKey, diff --git a/utils/logging/format.go b/utils/logging/format.go index 53313c3dad8f..7e7f4ec215e8 100644 --- a/utils/logging/format.go +++ b/utils/logging/format.go @@ -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{ @@ -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 { From 0f726caf95d9b48316a4f1e30b99fa9f7339ae2c Mon Sep 17 00:00:00 2001 From: Stephen Buttolph Date: Thu, 21 Nov 2024 12:44:30 -0500 Subject: [PATCH 2/4] Expose logging format constants --- tests/antithesis/compose.go | 1 - tests/fixture/bootstrapmonitor/cmd/main.go | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/antithesis/compose.go b/tests/antithesis/compose.go index b1f00fa6cbe1..3b25eb48d535 100644 --- a/tests/antithesis/compose.go +++ b/tests/antithesis/compose.go @@ -155,7 +155,6 @@ func newComposeProject(network *tmpnet.Network, nodeImageName string, workloadIm config.NetworkNameKey: constants.LocalName, config.LogLevelKey: logging.Debug.String(), config.LogDisplayLevelKey: logging.Trace.String(), - config.LogFormatKey: logging.JSONString, config.HTTPHostKey: "0.0.0.0", config.PublicIPKey: address, config.StakingTLSKeyContentKey: tlsKey, diff --git a/tests/fixture/bootstrapmonitor/cmd/main.go b/tests/fixture/bootstrapmonitor/cmd/main.go index 1e04fd882ad6..099ed213a6ff 100644 --- a/tests/fixture/bootstrapmonitor/cmd/main.go +++ b/tests/fixture/bootstrapmonitor/cmd/main.go @@ -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", From 863ceec3e30ce30b5bbfa690f69063b15a640582 Mon Sep 17 00:00:00 2001 From: Stephen Buttolph Date: Thu, 21 Nov 2024 12:52:35 -0500 Subject: [PATCH 3/4] nit --- config/flags.go | 2 +- tests/fixture/bootstrapmonitor/cmd/main.go | 2 +- utils/logging/format.go | 2 ++ 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/config/flags.go b/config/flags.go index 180b06fbccec..307d6c25ec7c 100644 --- a/config/flags.go +++ b/config/flags.go @@ -143,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, 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}") + 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.") diff --git a/tests/fixture/bootstrapmonitor/cmd/main.go b/tests/fixture/bootstrapmonitor/cmd/main.go index 099ed213a6ff..ea93b1e0d81c 100644 --- a/tests/fixture/bootstrapmonitor/cmd/main.go +++ b/tests/fixture/bootstrapmonitor/cmd/main.go @@ -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", 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}") + rootCmd.PersistentFlags().StringVar(&rawLogFormat, "log-format", logging.AutoString, logging.FormatDescription) versionCmd := &cobra.Command{ Use: "version", diff --git a/utils/logging/format.go b/utils/logging/format.go index 7e7f4ec215e8..c591f15ece84 100644 --- a/utils/logging/format.go +++ b/utils/logging/format.go @@ -24,6 +24,8 @@ const ( 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]" ) From 2ffd0339250b9a70f846dde0ea9c4d6ea1188c55 Mon Sep 17 00:00:00 2001 From: Stephen Buttolph Date: Thu, 21 Nov 2024 12:59:00 -0500 Subject: [PATCH 4/4] Expose json logs from antithesis nodes --- tests/antithesis/compose.go | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/antithesis/compose.go b/tests/antithesis/compose.go index 3b25eb48d535..b1f00fa6cbe1 100644 --- a/tests/antithesis/compose.go +++ b/tests/antithesis/compose.go @@ -155,6 +155,7 @@ func newComposeProject(network *tmpnet.Network, nodeImageName string, workloadIm config.NetworkNameKey: constants.LocalName, config.LogLevelKey: logging.Debug.String(), config.LogDisplayLevelKey: logging.Trace.String(), + config.LogFormatKey: logging.JSONString, config.HTTPHostKey: "0.0.0.0", config.PublicIPKey: address, config.StakingTLSKeyContentKey: tlsKey,