Skip to content

Commit 015f228

Browse files
committed
add log levels support
1 parent 0bf79d6 commit 015f228

File tree

1 file changed

+40
-1
lines changed

1 file changed

+40
-1
lines changed

cmd/node-joiner/main.go

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
package main
22

33
import (
4+
"fmt"
5+
"io"
6+
"os"
7+
48
"github.com/sirupsen/logrus"
59
"github.com/spf13/cobra"
10+
terminal "golang.org/x/term"
611

12+
"github.com/openshift/installer/cmd/openshift-install/command"
713
"github.com/openshift/installer/pkg/nodejoiner"
814
)
915

@@ -33,14 +39,47 @@ func main() {
3339
}
3440

3541
rootCmd := &cobra.Command{
36-
Use: "node-joiner",
42+
Use: "node-joiner",
43+
PersistentPreRun: runRootCmd,
3744
}
3845
rootCmd.PersistentFlags().String("kubeconfig", "", "Path to the kubeconfig file.")
3946
rootCmd.PersistentFlags().String("dir", ".", "assets directory")
47+
rootCmd.PersistentFlags().String("log-level", "info", "log level (e.g. \"debug | info | warn | error\")")
4048

4149
rootCmd.AddCommand(nodesAddCmd)
4250
rootCmd.AddCommand(nodesMonitorCmd)
4351
if err := rootCmd.Execute(); err != nil {
4452
logrus.Fatal(err)
4553
}
4654
}
55+
56+
func runRootCmd(cmd *cobra.Command, args []string) {
57+
logrus.SetOutput(io.Discard)
58+
logrus.SetLevel(logrus.TraceLevel)
59+
60+
logLevel, err := cmd.Flags().GetString("log-level")
61+
if err != nil {
62+
logrus.Fatal(err)
63+
}
64+
65+
level, err := logrus.ParseLevel(logLevel)
66+
if err != nil {
67+
level = logrus.InfoLevel
68+
}
69+
70+
logrus.AddHook(command.NewFileHookWithNewlineTruncate(os.Stderr, level, &logrus.TextFormatter{
71+
// Setting ForceColors is necessary because logrus.TextFormatter determines
72+
// whether or not to enable colors by looking at the output of the logger.
73+
// In this case, the output is io.Discard, which is not a terminal.
74+
// Overriding it here allows the same check to be done, but against the
75+
// hook's output instead of the logger's output.
76+
ForceColors: terminal.IsTerminal(int(os.Stderr.Fd())),
77+
DisableTimestamp: true,
78+
DisableLevelTruncation: true,
79+
DisableQuote: true,
80+
}))
81+
82+
if err != nil {
83+
logrus.Fatal(fmt.Errorf("invalid log-level: %w", err))
84+
}
85+
}

0 commit comments

Comments
 (0)