-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce human-friendly log formatting and better logger configurati…
…on (#5) * New version of `logger` delivering better human-readable formatting and configuration * New version of `run` using new `logger` * Fix tests in `parallel` * Use `github.com/pkg/errors` to produce errors containing stack traces
- Loading branch information
1 parent
005a77e
commit a538c43
Showing
16 changed files
with
756 additions
and
89 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package logger | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/spf13/pflag" | ||
|
||
"github.com/CoreumFoundation/coreum-tools/pkg/must" | ||
) | ||
|
||
// Format defines the format of log output | ||
type Format string | ||
|
||
const ( | ||
// FormatConsole causes logs to be printed in human-readable form | ||
FormatConsole Format = "console" | ||
|
||
// FormatJSON causes logs to be printed in JSON | ||
FormatJSON Format = "json" | ||
) | ||
|
||
// Config stores configuration of the logger | ||
type Config struct { | ||
// Format defines the format of log output | ||
Format Format | ||
|
||
// Verbose turns on verbose logging | ||
Verbose bool | ||
} | ||
|
||
// ToolDefaultConfig stores handy default configuration used by tools run manually by humans | ||
var ToolDefaultConfig = Config{ | ||
Format: FormatConsole, | ||
Verbose: false, | ||
} | ||
|
||
// ServiceDefaultConfig stores handy default configuration used by services | ||
var ServiceDefaultConfig = Config{ | ||
Format: FormatJSON, | ||
Verbose: true, | ||
} | ||
|
||
// ConfigureWithCLI configures logger based on CLI flags | ||
func ConfigureWithCLI(defaultConfig Config) Config { | ||
var format string | ||
flags := pflag.NewFlagSet("logger", pflag.ContinueOnError) | ||
flags.ParseErrorsWhitelist.UnknownFlags = true | ||
AddFlags(defaultConfig, flags) | ||
// Dummy flag to turn off printing usage of this flag set | ||
flags.BoolP("help", "h", false, "") | ||
|
||
_ = flags.Parse(os.Args[1:]) | ||
|
||
defaultConfig.Format = Format(must.String(flags.GetString("log-format"))) | ||
defaultConfig.Verbose = must.Bool(flags.GetBool("verbose")) | ||
if defaultConfig.Format != FormatConsole && defaultConfig.Format != FormatJSON { | ||
panic(errors.Errorf("incorrect logging format %s", format)) | ||
} | ||
|
||
return defaultConfig | ||
} | ||
|
||
// Flags returns new flag set preconfigured with logger-specific options | ||
func Flags(defaultConfig Config, name string) *pflag.FlagSet { | ||
flags := pflag.NewFlagSet(name, pflag.ContinueOnError) | ||
AddFlags(defaultConfig, flags) | ||
return flags | ||
} | ||
|
||
// AddFlags adds flags defined by logger | ||
func AddFlags(defaultConfig Config, flags *pflag.FlagSet) { | ||
flags.String("log-format", string(defaultConfig.Format), "Format of log output: console | json") | ||
flags.BoolP("verbose", "v", defaultConfig.Verbose, "Turns on verbose logging") | ||
} |
Oops, something went wrong.