From 8b05fff21a975e7be2fe86f459d42e78df481689 Mon Sep 17 00:00:00 2001 From: Djadih <13135116+Djadih@users.noreply.github.com> Date: Tue, 23 May 2023 12:40:26 -0500 Subject: [PATCH] Add showcolors flag to enable/disable colored log prints for non-TTYs --- Makefile | 4 ++++ cmd/go-quai/main.go | 1 + cmd/utils/flags.go | 7 +++++++ log/logger.go | 20 +++++++++++--------- network.env.dist | 3 +++ 5 files changed, 26 insertions(+), 9 deletions(-) diff --git a/Makefile b/Makefile index 8930d57e02..7f1600b114 100644 --- a/Makefile +++ b/Makefile @@ -64,6 +64,10 @@ ifeq ($(QUAI_STATS),true) BASE_CMD += --quaistats ${STATS_NAME}:${STATS_PASS}@${STATS_HOST} endif +ifeq ($(SHOW_COLORS),true) + BASE_CMD += --showcolors +endif + # Build suburl strings for slice specific subclient groups # WARNING: Only connect to dom/sub clients over a trusted network. ifeq ($(REGION),2) diff --git a/cmd/go-quai/main.go b/cmd/go-quai/main.go index 88444d8366..0b42981c5e 100644 --- a/cmd/go-quai/main.go +++ b/cmd/go-quai/main.go @@ -120,6 +120,7 @@ var ( utils.GpoPercentileFlag, utils.GpoMaxGasPriceFlag, utils.GpoIgnoreGasPriceFlag, + utils.ShowColorsFlag, configFileFlag, utils.RegionFlag, utils.ZoneFlag, diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index 4eaca2c0a7..312b021f53 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -599,6 +599,13 @@ var ( Usage: "Password to authorize access to the database", Value: metrics.DefaultConfig.InfluxDBPassword, } + + // Output flags + ShowColorsFlag = cli.BoolFlag{ + Name: "showcolors", + Usage: "Enable colorized logging", + } + // Tags are part of every measurement sent to InfluxDB. Queries on tags are faster in InfluxDB. // For example `host` tag could be used so that we can group all nodes and average a measurement // across all of them, but also so that we can select a specific node and inspect its measurements. diff --git a/log/logger.go b/log/logger.go index 8050e512c3..37016c98b3 100644 --- a/log/logger.go +++ b/log/logger.go @@ -17,13 +17,7 @@ type Logger = *logrus.Logger var Log Logger = logrus.New() func init() { - Log.Formatter = &logrus.TextFormatter{ - ForceColors: true, - PadLevelText: true, - FullTimestamp: true, - TimestampFormat: "01-02|15:04:05", - CallerPrettyfier: callerPrettyfier, - } + } func ConfigureLogger(ctx *cli.Context) { @@ -39,14 +33,22 @@ func ConfigureLogger(ctx *cli.Context) { if ctx.GlobalIsSet("zone") { zoneNum := ctx.GlobalString("zone") - log_filename = filepath.Join(log_filename, "zone-" + regionNum + "-" + zoneNum) + log_filename = filepath.Join(log_filename, "zone-"+regionNum+"-"+zoneNum) } else if ctx.GlobalIsSet("region") { - log_filename = filepath.Join(log_filename, "region-" + regionNum) + log_filename = filepath.Join(log_filename, "region-"+regionNum) } else { log_filename = filepath.Join(log_filename, "prime") } log_filename += ".log" + Log.Formatter = &logrus.TextFormatter{ + ForceColors: ctx.GlobalBool("showcolors"), + PadLevelText: true, + FullTimestamp: true, + TimestampFormat: "01-02|15:04:05", + CallerPrettyfier: callerPrettyfier, + } + Log.SetOutput(&lumberjack.Logger{ Filename: log_filename, MaxSize: 5, // megabytes diff --git a/network.env.dist b/network.env.dist index f2a1774970..6f22436205 100644 --- a/network.env.dist +++ b/network.env.dist @@ -122,3 +122,6 @@ STATS_NAME= STATS_PASS= STATS_HOST= ENABLE_PPROF=false + +# Output format variables +SHOW_COLORS=true