-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: set configurable log level and log format
- Loading branch information
1 parent
ab48167
commit 6f9887b
Showing
1 changed file
with
39 additions
and
0 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
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() | ||
} |