add logFile flag to change the default log file path (#1336)
parent
256441692a
commit
265e0561aa
|
|
@ -25,7 +25,7 @@ func printInfo() {
|
||||||
|
|
||||||
printLogo(color.Cyan)
|
printLogo(color.Cyan)
|
||||||
printTuple(fmat, "Configuration", config.K9sConfigFile, color.Cyan)
|
printTuple(fmat, "Configuration", config.K9sConfigFile, color.Cyan)
|
||||||
printTuple(fmat, "Logs", config.K9sLogs, color.Cyan)
|
printTuple(fmat, "Logs", config.DefaultLogFile, color.Cyan)
|
||||||
printTuple(fmat, "Screen Dumps", config.K9sDumpDir, color.Cyan)
|
printTuple(fmat, "Screen Dumps", config.K9sDumpDir, color.Cyan)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
18
cmd/root.go
18
cmd/root.go
|
|
@ -2,6 +2,7 @@ package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os"
|
||||||
"runtime/debug"
|
"runtime/debug"
|
||||||
|
|
||||||
"github.com/derailed/k9s/internal/client"
|
"github.com/derailed/k9s/internal/client"
|
||||||
|
|
@ -62,6 +63,17 @@ func run(cmd *cobra.Command, args []string) {
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
config.EnsurePath(*k9sFlags.LogFile, config.DefaultDirMod)
|
||||||
|
mod := os.O_CREATE | os.O_APPEND | os.O_WRONLY
|
||||||
|
file, err := os.OpenFile(*k9sFlags.LogFile, mod, config.DefaultFileMod)
|
||||||
|
defer func() {
|
||||||
|
_ = file.Close()
|
||||||
|
}()
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
log.Logger = log.Output(zerolog.ConsoleWriter{Out: file})
|
||||||
|
|
||||||
zerolog.SetGlobalLevel(parseLevel(*k9sFlags.LogLevel))
|
zerolog.SetGlobalLevel(parseLevel(*k9sFlags.LogLevel))
|
||||||
app := view.NewApp(loadConfiguration())
|
app := view.NewApp(loadConfiguration())
|
||||||
if err := app.Init(version, *k9sFlags.RefreshRate); err != nil {
|
if err := app.Init(version, *k9sFlags.RefreshRate); err != nil {
|
||||||
|
|
@ -150,6 +162,12 @@ func initK9sFlags() {
|
||||||
config.DefaultLogLevel,
|
config.DefaultLogLevel,
|
||||||
"Specify a log level (info, warn, debug, error, fatal, panic, trace)",
|
"Specify a log level (info, warn, debug, error, fatal, panic, trace)",
|
||||||
)
|
)
|
||||||
|
rootCmd.Flags().StringVarP(
|
||||||
|
k9sFlags.LogFile,
|
||||||
|
"logFile", "",
|
||||||
|
config.DefaultLogFile,
|
||||||
|
"Specify the log file",
|
||||||
|
)
|
||||||
rootCmd.Flags().BoolVar(
|
rootCmd.Flags().BoolVar(
|
||||||
k9sFlags.Headless,
|
k9sFlags.Headless,
|
||||||
"headless",
|
"headless",
|
||||||
|
|
|
||||||
|
|
@ -20,8 +20,6 @@ const K9sConfig = "K9SCONFIG"
|
||||||
var (
|
var (
|
||||||
// K9sConfigFile represents K9s config file location.
|
// K9sConfigFile represents K9s config file location.
|
||||||
K9sConfigFile = filepath.Join(K9sHome(), "config.yml")
|
K9sConfigFile = filepath.Join(K9sHome(), "config.yml")
|
||||||
// K9sLogs represents K9s log.
|
|
||||||
K9sLogs = filepath.Join(os.TempDir(), fmt.Sprintf("k9s-%s.log", MustK9sUser()))
|
|
||||||
// K9sDumpDir represents a directory where K9s screen dumps will be persisted.
|
// K9sDumpDir represents a directory where K9s screen dumps will be persisted.
|
||||||
K9sDumpDir = filepath.Join(os.TempDir(), fmt.Sprintf("k9s-screens-%s", MustK9sUser()))
|
K9sDumpDir = filepath.Join(os.TempDir(), fmt.Sprintf("k9s-screens-%s", MustK9sUser()))
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,11 @@
|
||||||
package config
|
package config
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
// DefaultRefreshRate represents the refresh interval.
|
// DefaultRefreshRate represents the refresh interval.
|
||||||
DefaultRefreshRate = 2 // secs
|
DefaultRefreshRate = 2 // secs
|
||||||
|
|
@ -11,10 +17,14 @@ const (
|
||||||
DefaultCommand = ""
|
DefaultCommand = ""
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// DefaultLogFile represents the default K9s log file.
|
||||||
|
var DefaultLogFile = filepath.Join(os.TempDir(), fmt.Sprintf("k9s-%s.log", MustK9sUser()))
|
||||||
|
|
||||||
// Flags represents K9s configuration flags.
|
// Flags represents K9s configuration flags.
|
||||||
type Flags struct {
|
type Flags struct {
|
||||||
RefreshRate *int
|
RefreshRate *int
|
||||||
LogLevel *string
|
LogLevel *string
|
||||||
|
LogFile *string
|
||||||
Headless *bool
|
Headless *bool
|
||||||
Logoless *bool
|
Logoless *bool
|
||||||
Command *string
|
Command *string
|
||||||
|
|
@ -29,6 +39,7 @@ func NewFlags() *Flags {
|
||||||
return &Flags{
|
return &Flags{
|
||||||
RefreshRate: intPtr(DefaultRefreshRate),
|
RefreshRate: intPtr(DefaultRefreshRate),
|
||||||
LogLevel: strPtr(DefaultLogLevel),
|
LogLevel: strPtr(DefaultLogLevel),
|
||||||
|
LogFile: strPtr(DefaultLogFile),
|
||||||
Headless: boolPtr(false),
|
Headless: boolPtr(false),
|
||||||
Logoless: boolPtr(false),
|
Logoless: boolPtr(false),
|
||||||
Command: strPtr(DefaultCommand),
|
Command: strPtr(DefaultCommand),
|
||||||
|
|
|
||||||
21
main.go
21
main.go
|
|
@ -2,20 +2,12 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
"os"
|
|
||||||
|
|
||||||
"github.com/derailed/k9s/cmd"
|
"github.com/derailed/k9s/cmd"
|
||||||
"github.com/derailed/k9s/internal/config"
|
|
||||||
"github.com/rs/zerolog"
|
|
||||||
"github.com/rs/zerolog/log"
|
|
||||||
_ "k8s.io/client-go/plugin/pkg/client/auth"
|
_ "k8s.io/client-go/plugin/pkg/client/auth"
|
||||||
"k8s.io/klog/v2"
|
"k8s.io/klog/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
|
||||||
config.EnsurePath(config.K9sLogs, config.DefaultDirMod)
|
|
||||||
}
|
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
klog.InitFlags(nil)
|
klog.InitFlags(nil)
|
||||||
|
|
||||||
|
|
@ -31,21 +23,8 @@ func init() {
|
||||||
if err := flag.Set("v", "0"); err != nil {
|
if err := flag.Set("v", "0"); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
if err := flag.Set("log_file", config.K9sLogs); err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
mod := os.O_CREATE | os.O_APPEND | os.O_WRONLY
|
|
||||||
file, err := os.OpenFile(config.K9sLogs, mod, config.DefaultFileMod)
|
|
||||||
defer func() {
|
|
||||||
_ = file.Close()
|
|
||||||
}()
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
log.Logger = log.Output(zerolog.ConsoleWriter{Out: file})
|
|
||||||
|
|
||||||
cmd.Execute()
|
cmd.Execute()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue