Skip to content

Commit 0481b3e

Browse files
arvind-choudhary-hHarness
authored andcommitted
updated logging (#115)
* fc11a4 []: updated logging
1 parent bc233dc commit 0481b3e

File tree

1 file changed

+8
-24
lines changed

1 file changed

+8
-24
lines changed

cmd/hc/main.go

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ import (
3030
var version = "dev"
3131

3232
func main() {
33-
var logFilePath string
3433
var showVersion bool
34+
var verbose bool
3535

3636
rootCmd := &cobra.Command{
3737
Use: "hc",
@@ -69,27 +69,12 @@ func main() {
6969
}
7070
}
7171

72-
logFileDefaultOutputPath := fmt.Sprintf("logs_%s.txt", time.Now().Format("20060102_150405"))
73-
if logFilePath == "" {
74-
logFilePath = logFileDefaultOutputPath
75-
}
76-
77-
// Ensure the directory exists
78-
logDir := filepath.Dir(logFilePath)
79-
if err := os.MkdirAll(logDir, 0755); err != nil {
80-
fmt.Printf("Warning: Could not create log directory: %v\n", err)
81-
}
82-
83-
// Open the log file
84-
logFile, err := os.OpenFile(logFilePath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
85-
if err != nil {
86-
fmt.Printf("Warning: Could not open log file: %v\n", err)
87-
} else {
88-
// Set up log writer with timestamp format
72+
// Set up logging if verbose mode is enabled
73+
if verbose {
8974
logWriter := zerolog.ConsoleWriter{
90-
Out: logFile,
75+
Out: os.Stderr,
9176
TimeFormat: time.RFC3339,
92-
NoColor: true,
77+
NoColor: false,
9378
}
9479
log.Logger = log.Output(logWriter).Hook(types.ErrorHook{})
9580
}
@@ -115,12 +100,11 @@ func main() {
115100
rootCmd.PersistentFlags().StringVar(&config.Global.ProjectID, "project", "", "Project (overrides saved config)")
116101
rootCmd.PersistentFlags().StringVar(&config.Global.Format, "format", "table", "Format of the result")
117102

118-
// Add log file path flag
119-
rootCmd.PersistentFlags().StringVar(&logFilePath, "log-file", "",
120-
"Path to store logs (if not provided, logging is disabled)")
103+
// Add verbose flag
104+
rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "Enable verbose logging to console")
121105

122106
// Add version flag
123-
rootCmd.Flags().BoolVarP(&showVersion, "version", "v", false, "Print version information")
107+
rootCmd.Flags().BoolVar(&showVersion, "version", false, "Print version information")
124108

125109
// Load auth config
126110
authConfig, err := loadAuthConfig()

0 commit comments

Comments
 (0)