cli: Refactor the logger setup

This commit is contained in:
James Shubin
2024-03-22 01:21:04 -04:00
parent d2188609e4
commit 8cf7719476
3 changed files with 16 additions and 13 deletions

View File

@@ -31,25 +31,12 @@ package util
import ( import (
"fmt" "fmt"
"log"
"os"
"time" "time"
) )
// Hello is a simple helper function to print a hello message and time. // Hello is a simple helper function to print a hello message and time.
func Hello(program, version string, flags Flags) { func Hello(program, version string, flags Flags) {
var start = time.Now().UnixNano() var start = time.Now().UnixNano()
// TODO: Move these log package initialization steps to the top main.go?
logFlags := log.LstdFlags
if flags.Debug {
logFlags = logFlags + log.Lshortfile
}
logFlags = logFlags - log.Ldate // remove the date for now
log.SetFlags(logFlags)
log.SetOutput(os.Stderr)
if program == "" { if program == "" {
program = "<unknown>" program = "<unknown>"
} }

View File

@@ -31,6 +31,8 @@
package util package util
import ( import (
"log"
"os"
"strings" "strings"
"github.com/purpleidea/mgmt/util/errwrap" "github.com/purpleidea/mgmt/util/errwrap"
@@ -81,3 +83,16 @@ func SafeProgram(program string) string {
//} //}
return program return program
} }
// LogSetup changes some of the core logger package settings.
func LogSetup(debug bool) {
// TODO: Move these log package initialization steps to the top main.go?
logFlags := log.LstdFlags
if debug {
logFlags = logFlags + log.Lshortfile
}
logFlags = logFlags - log.Ldate // remove the date for now
log.SetFlags(logFlags)
log.SetOutput(os.Stderr)
}

View File

@@ -74,6 +74,7 @@ func main() {
return // for safety return // for safety
} }
cliUtil.LogSetup(debug)
data := &cliUtil.Data{ data := &cliUtil.Data{
Program: program, Program: program,
Version: version, Version: version,