Skip to content

Commit d1e1d6c

Browse files
committed
control log level via env var
Signed-off-by: Eric Pickard <piceri@github.com>
1 parent 8a1993f commit d1e1d6c

2 files changed

Lines changed: 23 additions & 1 deletion

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ Two modes of authentication are supported:
6565

6666
| Variable | Description | Default |
6767
|------------------------|--------------------------------------------|------------------------------------------------------|
68+
| `LOG_LEVEL` | log level: DEBUG, INFO, WARN, ERROR | `INFO` |
6869
| `ORG` | GitHub organization name | (required) |
6970
| `BASE_URL` | API base URL | `api.github.com` |
7071
| `DN_TEMPLATE` | Deployment name template | `{{namespace}}/{{deploymentName}}/{{containerName}}` |

cmd/deployment-tracker/main.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"net/http"
1111
"os"
1212
"os/signal"
13+
"strings"
1314
"syscall"
1415
"time"
1516

@@ -65,8 +66,13 @@ func main() {
6566

6667
// init logging
6768
log.SetFlags(log.LstdFlags | log.Lshortfile | log.LUTC)
68-
opts := slog.HandlerOptions{Level: slog.LevelInfo}
69+
logLevelStr := getEnvOrDefault("LOG_LEVEL", "INFO")
70+
level, msg := parseLogLevel(logLevelStr)
71+
opts := slog.HandlerOptions{Level: level}
6972
slog.SetDefault(slog.New(slog.NewJSONHandler(os.Stdout, &opts)))
73+
if msg != "" {
74+
slog.Warn(msg)
75+
}
7076

7177
var ghAppPrivateKey []byte
7278
if b64Key := os.Getenv("GH_APP_PRIVATE_KEY"); b64Key != "" {
@@ -220,3 +226,18 @@ func createK8sConfig(kubeconfig string) (*rest.Config, error) {
220226
}
221227
return clientcmd.BuildConfigFromFlags("", homeDir+"/.kube/config")
222228
}
229+
230+
func parseLogLevel(logLevel string) (slog.Level, string) {
231+
switch strings.ToUpper(logLevel) {
232+
case "DEBUG":
233+
return slog.LevelDebug, ""
234+
case "INFO":
235+
return slog.LevelInfo, ""
236+
case "WARN":
237+
return slog.LevelWarn, ""
238+
case "ERROR":
239+
return slog.LevelError, ""
240+
default:
241+
return slog.LevelInfo, fmt.Sprintf("%s is an unsupported log level (DEBUG, WARN, INFO, ERROR), using INFO...", logLevel)
242+
}
243+
}

0 commit comments

Comments
 (0)