Skip to content

Commit

Permalink
feat: set configurable log level and log format
Browse files Browse the repository at this point in the history
  • Loading branch information
mircea-pavel-anton committed Aug 12, 2024
1 parent ab48167 commit 6f9887b
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,48 @@
package main

import (
"os"
"strings"

"github.com/mirceanton/talswitcher/cmd"
log "github.com/sirupsen/logrus"
)

func SetLogLevel() {
logLevel := os.Getenv("TALSWITCHER_LOG_LEVEL")
if logLevel == "" {
logLevel = "info"
}

level, err := log.ParseLevel(strings.ToLower(logLevel))
if err != nil {
log.Fatalf("Invalid log level: %v", err)
}
log.SetLevel(level)
}

func SetLogFormat() {
logFormat := os.Getenv("TALSWITCHER_LOG_FORMAT")
if logFormat == "" {
logFormat = "text"
}

switch strings.ToLower(logFormat) {
case "json":
log.SetFormatter(&log.JSONFormatter{})
case "text":
log.SetFormatter(&log.TextFormatter{FullTimestamp: true})

default:
log.Fatalf("Invalid logformat: %v", logFormat)
}
}

func init() {
SetLogLevel()
SetLogFormat()
}

func main() {
cmd.Execute()
}

0 comments on commit 6f9887b

Please sign in to comment.