Skip to content
This repository has been archived by the owner on Oct 28, 2022. It is now read-only.

Commit

Permalink
Merge pull request #120 from karimra/log-config-file
Browse files Browse the repository at this point in the history
log config file used
  • Loading branch information
karimra authored Sep 1, 2020
2 parents e0fdfa1 + e9e3e8c commit 2f73e29
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,15 @@ var rootCmd = &cobra.Command{
Use: "gnmic",
Short: "run gnmi rpcs from the terminal (https://gnmic.kmrd.dev)",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
debug := viper.GetBool("debug")
if viper.GetString("log-file") != "" {
var err error
f, err = os.OpenFile(viper.GetString("log-file"), os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
logger.Fatalf("error opening file: %v", err)
}
} else {
if viper.GetBool("debug") {
if debug {
viper.Set("log", true)
}
switch viper.GetBool("log") {
Expand All @@ -78,13 +79,25 @@ var rootCmd = &cobra.Command{
}
}
loggingFlags := log.LstdFlags | log.Lmicroseconds
if viper.GetBool("debug") {
if debug {
loggingFlags |= log.Llongfile
}
logger = log.New(f, "gnmic ", loggingFlags)
if viper.GetBool("debug") {
if debug {
grpclog.SetLogger(logger) //lint:ignore SA1019 see https://github.com/karimra/gnmic/issues/59
}
cfgFile := viper.ConfigFileUsed()
if len(cfgFile) != 0 {
logger.Printf("using config file %s", cfgFile)
if debug {
b, err := ioutil.ReadFile(cfgFile)
if err != nil {
logger.Printf("failed reading config file %s: %v", cfgFile, err)
return
}
logger.Printf("config file:\n%s", string(b))
}
}
},
PersistentPostRun: func(cmd *cobra.Command, args []string) {
if !viper.GetBool("log") || viper.GetString("log-file") != "" {
Expand Down

0 comments on commit 2f73e29

Please sign in to comment.