mirror of
https://github.com/prymitive/karma
synced 2026-05-07 03:26:52 +00:00
karma: add ability to configure log format
Add a rudimentary switch which would let users switch between different formats used for logging. This is needed so that software such as `logstash` could easily understand the structure of log messages and index them accordingly.
This commit is contained in:
@@ -70,6 +70,8 @@ func init() {
|
||||
pflag.Bool("log.config", true, "Log used configuration to log on startup")
|
||||
pflag.String("log.level", "info",
|
||||
"Log level, one of: debug, info, warning, error, fatal and panic")
|
||||
pflag.String("log.format", "text",
|
||||
"Log format, one of: text, json")
|
||||
|
||||
pflag.StringSlice("receivers.keep", []string{},
|
||||
"List of receivers to keep, all alerts with different receivers will be ignored")
|
||||
@@ -159,6 +161,7 @@ func (config *configSchema) Read() {
|
||||
config.Listen.Prefix = v.GetString("listen.prefix")
|
||||
config.Log.Config = v.GetBool("log.config")
|
||||
config.Log.Level = v.GetString("log.level")
|
||||
config.Log.Format = v.GetString("log.format")
|
||||
config.Receivers.Keep = v.GetStringSlice("receivers.keep")
|
||||
config.Receivers.Strip = v.GetStringSlice("receivers.strip")
|
||||
config.Sentry.Private = v.GetString("sentry.private")
|
||||
|
||||
@@ -82,6 +82,7 @@ type configSchema struct {
|
||||
Log struct {
|
||||
Config bool
|
||||
Level string
|
||||
Format string
|
||||
}
|
||||
JIRA []jiraRule
|
||||
Receivers struct {
|
||||
|
||||
9
main.go
9
main.go
@@ -158,6 +158,15 @@ func setupLogger() {
|
||||
default:
|
||||
log.Fatalf("Unknown log level '%s'", config.Config.Log.Level)
|
||||
}
|
||||
|
||||
switch config.Config.Log.Format {
|
||||
case "text":
|
||||
log.SetFormatter(&log.TextFormatter{})
|
||||
case "json":
|
||||
log.SetFormatter(&log.JSONFormatter{})
|
||||
default:
|
||||
log.Fatalf("Unknown log format '%s'", config.Config.Log.Format)
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
||||
Reference in New Issue
Block a user