github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/cli/util/log.go (about)

     1  /*
     2  Copyright (C) 2022-2023 ApeCloud Co., Ltd
     3  
     4  This file is part of KubeBlocks project
     5  
     6  This program is free software: you can redistribute it and/or modify
     7  it under the terms of the GNU Affero General Public License as published by
     8  the Free Software Foundation, either version 3 of the License, or
     9  (at your option) any later version.
    10  
    11  This program is distributed in the hope that it will be useful
    12  but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  GNU Affero General Public License for more details.
    15  
    16  You should have received a copy of the GNU Affero General Public License
    17  along with this program.  If not, see <http://www.gnu.org/licenses/>.
    18  */
    19  
    20  package util
    21  
    22  import (
    23  	"flag"
    24  	"fmt"
    25  	"path/filepath"
    26  	"strings"
    27  	"time"
    28  
    29  	"github.com/spf13/pflag"
    30  	"k8s.io/klog/v2"
    31  
    32  	"github.com/1aal/kubeblocks/pkg/cli/types"
    33  )
    34  
    35  func EnableLogToFile(fs *pflag.FlagSet) error {
    36  	logFile, err := getCliLogFile()
    37  	if err != nil {
    38  		return err
    39  	}
    40  
    41  	setFlag := func(kv map[string]string) {
    42  		for k, v := range kv {
    43  			_ = fs.Set(k, v)
    44  		}
    45  	}
    46  
    47  	if klog.V(1).Enabled() {
    48  		// if log is enabled, write log to standard output and log file
    49  		setFlag(map[string]string{
    50  			"alsologtostderr": "true",
    51  			"logtostderr":     "false",
    52  			"log-file":        logFile,
    53  		})
    54  	} else {
    55  		// if log is not enabled, enable it and write log to file
    56  		setFlag(map[string]string{
    57  			"v":               "1",
    58  			"logtostderr":     "false",
    59  			"alsologtostderr": "false",
    60  			"log-file":        logFile,
    61  		})
    62  	}
    63  	return nil
    64  }
    65  
    66  func getCliLogFile() (string, error) {
    67  	homeDir, err := GetCliHomeDir()
    68  	if err != nil {
    69  		return "", err
    70  	}
    71  	return filepath.Join(homeDir, fmt.Sprintf("%s-%s.log", types.DefaultLogFilePrefix, time.Now().Format("2006-01-02"))), nil
    72  }
    73  
    74  // AddKlogFlags adds flags from k8s.io/klog
    75  // marks the flags as hidden to avoid showing them in help
    76  func AddKlogFlags(fs *pflag.FlagSet) {
    77  	local := flag.NewFlagSet("klog", flag.ExitOnError)
    78  	klog.InitFlags(local)
    79  	local.VisitAll(func(f *flag.Flag) {
    80  		f.Name = strings.ReplaceAll(f.Name, "_", "-")
    81  		if fs.Lookup(f.Name) != nil {
    82  			return
    83  		}
    84  		newFlag := pflag.PFlagFromGoFlag(f)
    85  		newFlag.Hidden = true
    86  		fs.AddFlag(newFlag)
    87  	})
    88  }