add grpc and tls logging to stderr as default
@@ -25,7 +25,6 @@ import (
"io/ioutil"
"log"
"os"
- "path/filepath"
"strings"
"time"
@@ -36,6 +35,7 @@ import (
"golang.org/x/crypto/ssh/terminal"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
+ "google.golang.org/grpc/grpclog"
)
const (
@@ -55,29 +55,20 @@ var rootCmd = &cobra.Command{
PersistentPreRun: func(cmd *cobra.Command, args []string) {
if viper.GetBool("nolog") {
f = myWriteCloser{}
- return
- }
- if viper.GetBool("logstdout") {
- log.SetFlags(log.LstdFlags | log.Lmicroseconds)
- f = os.Stdout
- return
}
+ if viper.GetString("log-file") == "" {
+ f = os.Stderr
+ } else {
var err error
- logFile := viper.GetString("log-file")
- if logFile == "" {
- logFile = fmt.Sprintf("%s/.gnmi/gnmiClient.log", os.Getenv("HOME"))
- viper.Set("log-file", logFile)
- }
- if err = os.MkdirAll(filepath.Dir(logFile), 0755); err != nil {
- fmt.Printf("could not create log directory '%s':%v\n", filepath.Dir(logFile), err)
- return
- }
f, err = os.OpenFile(viper.GetString("log-file"), os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
log.Fatalf("error opening file: %v", err)
}
- log.SetFlags(log.LstdFlags | log.Lmicroseconds)
log.SetOutput(f)
+ }
+ log.SetFlags(log.LstdFlags | log.Lmicroseconds)
+ logger := log.New(f, "", log.LstdFlags|log.Lmicroseconds)
+ grpclog.SetLogger(logger)
},
PersistentPostRun: func(cmd *cobra.Command, args []string) {
if !viper.GetBool("nolog") && !viper.GetBool("logstdout") {
