Skip to content

Commit

Permalink
🚀 support setting custom log formatter using config (#304)
Browse files Browse the repository at this point in the history
  • Loading branch information
abahmed authored May 30, 2024
1 parent 576389c commit 18a3f58
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 3 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ kubectl apply -f https://raw.githubusercontent.com/abahmed/kwatch/v0.8.5/deploy/
| `app.proxyURL` | used in outgoing http(s) requests except Kubernetes requests to cluster optionally |
| `app.clusterName` | used in notifications to indicate which cluster has issue |
| `app.disableStartupMessage` | If set to true, welcome message will not be sent to notification channels |
| `app.logFormatter` | used for setting custom formatter when app prints logs: text, json (default: text) |


### Upgrader

Expand Down Expand Up @@ -201,7 +203,7 @@ If you want to enable Microsoft Teams, provide the channel webhook.
|:---------------------------------|:------------------------------------------------|
| `alert.teams.webhook` | webhook Microsoft team |
| `alert.teams.title` | Customized title in Microsoft teams message |
| `alert.teams.text` | Customized title in Microsoft teams message |
| `alert.teams.text` | Customized title in Microsoft teams message |

#### Rocket Chat

Expand Down
3 changes: 3 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ type App struct {
// DisableUpdateCheck if set to true, welcome message will not be
// sent to configured notification channels
DisableStartupMessage bool `yaml:"disableStartupMessage"`

// LogFormatter used for setting custom formatter when app prints logs
LogFormatter string `yaml:"logFormatter"`
}

// Upgrader confing struct
Expand Down
3 changes: 3 additions & 0 deletions config/defaultConfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package config

func DefaultConfig() *Config {
return &Config{
App: App{
LogFormatter: "text",
},
IgnoreFailedGracefulShutdown: true,
PvcMonitor: PvcMonitor{
Enabled: true,
Expand Down
14 changes: 12 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ import (
)

func main() {
logrus.Infof(fmt.Sprintf(constant.WelcomeMsg, version.Short()))

config, err := config.LoadConfig()
if err != nil {
logrus.Fatalf("failed to load config: %s", err.Error())
}
setLogFormatter(config.App.LogFormatter)

logrus.Infof(fmt.Sprintf(constant.WelcomeMsg, version.Short()))

// create kubernetes client
client := client.Create(&config.App)
Expand Down Expand Up @@ -61,3 +62,12 @@ func main() {
// start watcher
watcher.Start(client, namespace, h.ProcessPod)
}

func setLogFormatter(formatter string) {
switch formatter {
case "json":
logrus.SetFormatter(&logrus.JSONFormatter{})
default:
logrus.SetFormatter(&logrus.TextFormatter{})
}
}

0 comments on commit 18a3f58

Please sign in to comment.