You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficcontrol.apache.org by zr...@apache.org on 2021/09/21 22:08:54 UTC

[trafficcontrol] 03/09: update print statements in t3c to use go logger library (#6052)

This is an automated email from the ASF dual-hosted git repository.

zrhoffman pushed a commit to branch 6.0.x
in repository https://gitbox.apache.org/repos/asf/trafficcontrol.git

commit 803d75c29303ef99258a3150e19e49449e47ede2
Author: Joe Pappano <jo...@cable.comcast.com>
AuthorDate: Thu Sep 16 15:52:56 2021 -0400

    update print statements in t3c to use go logger library (#6052)
    
    * Updated print statements to use go logging library for Maple.
    
    * Updated print statements to use go logging library for Maple.
    
    * Put print statement back and changed other log statement to print. Moved comment to top of func.
    
    * Removed unused func
    
    * Put back to print highly unlikely--version calls will ever need run thru a log parser.
    
    * Changed to be single new line.
    
    * Changed back to fmt.Println.
    
    * Added log.Init
    
    * Changed back to fmt.Fprintf()
    
    * Put info log strings into an array and will print them once loggers are initialized.
    
    * fixed formatting errors
    
    (cherry picked from commit 9766956ee96fbc9e66896ce2865de6e9cf7c15c7)
---
 cache-config/t3c-apply/config/config.go            | 14 ++++++++-----
 cache-config/t3c-apply/t3c-apply.go                |  2 +-
 cache-config/t3c-apply/torequest/torequest.go      |  2 +-
 cache-config/t3c-check/t3c-check.go                | 13 ++++++------
 cache-config/t3c-diff/t3c-diff.go                  | 15 +++++++-------
 cache-config/t3c-generate/plugin/plugin.go         |  5 +++--
 cache-config/t3c-generate/t3c-generate.go          |  2 +-
 .../update-to-client/update-to-client.go           | 10 +++++----
 cache-config/t3c-request/config/config.go          | 24 +++++++++++-----------
 cache-config/t3c/t3c.go                            | 13 ++++++------
 10 files changed, 55 insertions(+), 45 deletions(-)

diff --git a/cache-config/t3c-apply/config/config.go b/cache-config/t3c-apply/config/config.go
index ba33260..1c4f416 100644
--- a/cache-config/t3c-apply/config/config.go
+++ b/cache-config/t3c-apply/config/config.go
@@ -215,6 +215,7 @@ func GetTSPackageHome() string {
 
 func GetCfg() (Cfg, error) {
 	var err error
+	toInfoLog := []string{}
 
 	cacheHostNamePtr := getopt.StringLong("cache-host-name", 'H', "", "Host name of the cache to generate config for. Must be the server host name in Traffic Ops, not a URL, and not the FQDN")
 	retriesPtr := getopt.IntLong("num-retries", 'r', 3, "[number] retry connection to Traffic Ops URL [number] times, default is 3")
@@ -418,18 +419,18 @@ If any of the related flags are also set, they override the mode's default behav
 	var tsHome = ""
 	if *tsHomePtr != "" {
 		tsHome = *tsHomePtr
-		fmt.Printf("set TSHome from command line: '%s'\n\n", TSHome)
+		toInfoLog = append(toInfoLog, fmt.Sprintf("set TSHome from command line: '%s'", TSHome))
 	}
 	if *tsHomePtr == "" { // evironment or rpm check.
 		tsHome = os.Getenv("TS_HOME") // check for the environment variable.
 		if tsHome != "" {
-			fmt.Printf("set TSHome from TS_HOME environment variable '%s'\n", TSHome)
+			toInfoLog = append(toInfoLog, fmt.Sprintf("set TSHome from TS_HOME environment variable '%s'\n", TSHome))
 		} else { // finally check using the config file listing from the rpm package.
 			tsHome = GetTSPackageHome()
 			if tsHome != "" {
-				fmt.Printf("set TSHome from the RPM config file  list '%s'\n", tsHome)
+				toInfoLog = append(toInfoLog, fmt.Sprintf("set TSHome from the RPM config file  list '%s'\n", TSHome))
 			} else {
-				fmt.Printf("no override for TSHome was found, using the configured default: '%s'\n", TSHome)
+				toInfoLog = append(toInfoLog, fmt.Sprintf("no override for TSHome was found, using the configured default: '%s'\n", TSHome))
 			}
 		}
 	}
@@ -439,7 +440,7 @@ If any of the related flags are also set, they override the mode's default behav
 	if tsHome != "" {
 		TSHome = tsHome
 		tsConfigDir = tsHome + "/etc/trafficserver"
-		fmt.Printf("TSHome: %s, TSConfigDir: %s\n", TSHome, tsConfigDir)
+		toInfoLog = append(toInfoLog, fmt.Sprintf("TSHome: %s, TSConfigDir: %s\n", TSHome, tsConfigDir))
 	}
 
 	usageStr := "basic usage: t3c-apply --traffic-ops-url=myurl --traffic-ops-user=myuser --traffic-ops-password=mypass --cache-host-name=my-cache"
@@ -512,6 +513,9 @@ If any of the related flags are also set, they override the mode's default behav
 	for _, str := range modeLogStrs {
 		log.Infoln(str)
 	}
+	for msg := range toInfoLog {
+		log.Infoln(msg)
+	}
 
 	printConfig(cfg)
 
diff --git a/cache-config/t3c-apply/t3c-apply.go b/cache-config/t3c-apply/t3c-apply.go
index e0cc6fd..ba75326 100644
--- a/cache-config/t3c-apply/t3c-apply.go
+++ b/cache-config/t3c-apply/t3c-apply.go
@@ -110,7 +110,7 @@ func main() {
 		}
 	}
 
-	fmt.Println(time.Now().Format(time.UnixDate))
+	log.Infoln(time.Now().Format(time.UnixDate))
 
 	if !util.CheckUser(cfg) {
 		lock.UnlockAndExit(UserCheckError)
diff --git a/cache-config/t3c-apply/torequest/torequest.go b/cache-config/t3c-apply/torequest/torequest.go
index 7763fbb..f106471 100644
--- a/cache-config/t3c-apply/torequest/torequest.go
+++ b/cache-config/t3c-apply/torequest/torequest.go
@@ -160,7 +160,7 @@ func unencodeFilter(body []string) []string {
 // DumpConfigFiles is used for debugging
 func (r *TrafficOpsReq) DumpConfigFiles() {
 	for _, cfg := range r.configFiles {
-		fmt.Printf("Name: %s, Dir: %s, Service: %s\n",
+		log.Infof("Name: %s, Dir: %s, Service: %s\n",
 			cfg.Name, cfg.Dir, cfg.Service)
 	}
 }
diff --git a/cache-config/t3c-check/t3c-check.go b/cache-config/t3c-check/t3c-check.go
index f46fdfa..1cf0b94 100644
--- a/cache-config/t3c-check/t3c-check.go
+++ b/cache-config/t3c-check/t3c-check.go
@@ -20,7 +20,7 @@ package main
  */
 
 import (
-	"fmt"
+	"github.com/apache/trafficcontrol/lib/go-log"
 	"os"
 	"os/exec"
 	"syscall" // TODO change to x/unix ?
@@ -43,19 +43,20 @@ const ExitCodeCommandLookupErr = 5
 func main() {
 	flagHelp := getopt.BoolLong("help", 'h', "Print usage information and exit")
 	getopt.Parse()
+	log.Init(os.Stderr, os.Stderr, os.Stderr, os.Stderr, os.Stderr)
 	if *flagHelp {
-		fmt.Println(usageStr())
+		log.Errorln(usageStr())
 		os.Exit(ExitCodeSuccess)
 	}
 
 	if len(os.Args) < 2 {
-		fmt.Fprintf(os.Stderr, "no command\n\n"+usageStr())
+		log.Errorf("no command\n\n" + usageStr())
 		os.Exit(ExitCodeNoCommand)
 	}
 
 	cmd := os.Args[1]
 	if _, ok := commands[cmd]; !ok {
-		fmt.Fprintf(os.Stderr, "unknown command\n") // TODO print usage
+		log.Errorf("unknown command\n%s", usageStr())
 		os.Exit(ExitCodeUnknownCommand)
 	}
 
@@ -63,7 +64,7 @@ func main() {
 
 	appPath, err := exec.LookPath(app)
 	if err != nil {
-		fmt.Fprintf(os.Stderr, "error finding path to '"+app+"': "+err.Error())
+		log.Errorf("error finding path to '%s': %s\n", app, err.Error())
 		os.Exit(ExitCodeCommandLookupErr)
 	}
 
@@ -72,7 +73,7 @@ func main() {
 	env := os.Environ()
 
 	if err := syscall.Exec(appPath, args, env); err != nil {
-		fmt.Fprintf(os.Stderr, "error executing sub-command: "+err.Error()+"\n")
+		log.Errorf("error executing sub-command: %s\n", err.Error())
 		os.Exit(ExitCodeCommandErr)
 	}
 }
diff --git a/cache-config/t3c-diff/t3c-diff.go b/cache-config/t3c-diff/t3c-diff.go
index 0686a58..854047f 100644
--- a/cache-config/t3c-diff/t3c-diff.go
+++ b/cache-config/t3c-diff/t3c-diff.go
@@ -21,7 +21,7 @@ package main
 
 import (
 	"errors"
-	"fmt"
+	"github.com/apache/trafficcontrol/lib/go-log"
 	"io/ioutil"
 	"os"
 	"regexp"
@@ -35,13 +35,14 @@ import (
 func main() {
 	help := getopt.BoolLong("help", 'h', "Print usage info and exit")
 	getopt.ParseV2()
+	log.Init(os.Stderr, os.Stderr, os.Stderr, os.Stderr, os.Stderr)
 	if *help {
-		fmt.Println(usageStr)
+		log.Errorln(usageStr)
 		os.Exit(0)
 	}
 
 	if len(os.Args) < 3 {
-		fmt.Println(usageStr)
+		log.Errorln(usageStr)
 		os.Exit(3)
 	}
 
@@ -49,18 +50,18 @@ func main() {
 	fileNameB := strings.TrimSpace(os.Args[2])
 
 	if len(fileNameA) == 0 || len(fileNameB) == 0 {
-		fmt.Println(usageStr)
+		log.Errorln(usageStr)
 		os.Exit(4)
 	}
 
 	fileA, fileAExisted, err := readFileOrStdin(fileNameA)
 	if err != nil {
-		fmt.Fprintf(os.Stderr, "error reading first: "+err.Error())
+		log.Errorf("error reading first: %s\n", err.Error())
 		os.Exit(5)
 	}
 	fileB, fileBExisted, err := readFileOrStdin(fileNameB)
 	if err != nil {
-		fmt.Fprintf(os.Stderr, "error reading second: "+err.Error())
+		log.Errorf("error reading second: %s\n", err.Error())
 		os.Exit(6)
 	}
 
@@ -80,7 +81,7 @@ func main() {
 		match := regexp.MustCompile(`(?m)^\+.*|^-.*`)
 		changes := diff.Diff(fileA, fileB)
 		for _, change := range match.FindAllString(changes, -1) {
-			fmt.Println(change)
+			log.Infoln(change)
 		}
 		os.Exit(1)
 	}
diff --git a/cache-config/t3c-generate/plugin/plugin.go b/cache-config/t3c-generate/plugin/plugin.go
index 245d21a..50c6d97 100644
--- a/cache-config/t3c-generate/plugin/plugin.go
+++ b/cache-config/t3c-generate/plugin/plugin.go
@@ -57,14 +57,15 @@ type Plugins interface {
 }
 
 func AddPlugin(priority uint64, funcs Funcs) {
+	// print used here, because this is called in init, loggers don't exist yet
 	_, filename, _, ok := runtime.Caller(1)
 	if !ok {
-		fmt.Println(time.Now().Format(time.RFC3339Nano) + " Error plugin.AddPlugin: runtime.Caller failed, can't get plugin names") // print, because this is called in init, loggers don't exist yet
+		fmt.Println(time.Now().Format(time.RFC3339Nano) + " Error plugin.AddPlugin: runtime.Caller failed, can't get plugin names")
 		os.Exit(1)
 	}
 
 	pluginName := strings.TrimSuffix(path.Base(filename), ".go")
-	log.Infoln("AddPlugin adding " + pluginName)
+	fmt.Println("AddPlugin adding " + pluginName)
 	initPlugins = append(initPlugins, pluginObj{funcs: funcs, priority: priority, name: pluginName})
 }
 
diff --git a/cache-config/t3c-generate/t3c-generate.go b/cache-config/t3c-generate/t3c-generate.go
index 3e8966e..7059a57 100644
--- a/cache-config/t3c-generate/t3c-generate.go
+++ b/cache-config/t3c-generate/t3c-generate.go
@@ -43,7 +43,7 @@ func main() {
 	}
 
 	if cfg.ListPlugins {
-		fmt.Println(strings.Join(plugin.List(), "\n"))
+		log.Errorln(strings.Join(plugin.List(), "\n"))
 		os.Exit(0)
 	}
 
diff --git a/cache-config/t3c-generate/update-to-client/update-to-client.go b/cache-config/t3c-generate/update-to-client/update-to-client.go
index 30fa764..91098e6 100644
--- a/cache-config/t3c-generate/update-to-client/update-to-client.go
+++ b/cache-config/t3c-generate/update-to-client/update-to-client.go
@@ -22,6 +22,7 @@ package main
 import (
 	"errors"
 	"fmt"
+	"github.com/apache/trafficcontrol/lib/go-log"
 	"io/ioutil"
 	"os"
 	"os/exec"
@@ -31,8 +32,9 @@ import (
 )
 
 func main() {
+	log.Init(os.Stderr, os.Stderr, os.Stderr, os.Stderr, os.Stderr)
 	if len(os.Args) < 3 {
-		fmt.Fprintf(os.Stdout, usageStr())
+		log.Errorln(usageStr())
 		os.Exit(0)
 	}
 	dir := os.Args[1]
@@ -40,18 +42,18 @@ func main() {
 
 	workingDir, err := os.Getwd()
 	if err != nil {
-		fmt.Fprintf(os.Stderr, "Error getting working directory: "+err.Error()+"\n")
+		log.Errorf("Error getting working directory: %s\n", err.Error())
 		os.Exit(1)
 	}
 
 	dir = filepath.Join(workingDir, dir) // make the given directory absolute
 
 	if err := updateVendoredTOClient(dir, branch); err != nil {
-		fmt.Fprintf(os.Stderr, "Error updating vendored client: "+err.Error()+"\n")
+		log.Errorf("Error updating vendored client: %s\n", err.Error())
 		os.Exit(1)
 	}
 	if err := updateNewClientUsage(dir); err != nil {
-		fmt.Fprintf(os.Stderr, "Error updating new client usage: "+err.Error()+"\n")
+		log.Errorf("Error updating new client usage: %s\n", err.Error())
 		os.Exit(1)
 	}
 	os.Exit(0)
diff --git a/cache-config/t3c-request/config/config.go b/cache-config/t3c-request/config/config.go
index 27076dd..68da602 100644
--- a/cache-config/t3c-request/config/config.go
+++ b/cache-config/t3c-request/config/config.go
@@ -178,18 +178,18 @@ func InitConfig() (Cfg, error) {
 }
 
 func (cfg Cfg) PrintConfig() {
-	fmt.Printf("CommandArgs: %s\n", cfg.CommandArgs)
-	fmt.Printf("LogLocationDebug: %s\n", cfg.LogLocationDebug)
-	fmt.Printf("LogLocationError: %s\n", cfg.LogLocationError)
-	fmt.Printf("LogLocationInfo: %s\n", cfg.LogLocationInfo)
-	fmt.Printf("LogLocationWarn: %s\n", cfg.LogLocationWarn)
-	fmt.Printf("LoginDispersion : %s\n", cfg.LoginDispersion)
-	fmt.Printf("CacheHostName: %s\n", cfg.CacheHostName)
-	fmt.Printf("TOInsecure: %v\n", cfg.TOInsecure)
-	fmt.Printf("TOTimeoutMS: %s\n", cfg.TOTimeoutMS)
-	fmt.Printf("TOUser: %s\n", cfg.TOUser)
-	fmt.Printf("TOPass: xxxxxx\n")
-	fmt.Printf("TOURL: %s\n", cfg.TOURL)
+	log.Debugf("CommandArgs: %s\n", cfg.CommandArgs)
+	log.Debugf("LogLocationDebug: %s\n", cfg.LogLocationDebug)
+	log.Debugf("LogLocationError: %s\n", cfg.LogLocationError)
+	log.Debugf("LogLocationInfo: %s\n", cfg.LogLocationInfo)
+	log.Debugf("LogLocationWarn: %s\n", cfg.LogLocationWarn)
+	log.Debugf("LoginDispersion : %s\n", cfg.LoginDispersion)
+	log.Debugf("CacheHostName: %s\n", cfg.CacheHostName)
+	log.Debugf("TOInsecure: %v\n", cfg.TOInsecure)
+	log.Debugf("TOTimeoutMS: %s\n", cfg.TOTimeoutMS)
+	log.Debugf("TOUser: %s\n", cfg.TOUser)
+	log.Debugf("TOPass: xxxxxx\n")
+	log.Debugf("TOURL: %s\n", cfg.TOURL)
 }
 
 func LoadOldCfg(path string) (*t3cutil.ConfigData, error) {
diff --git a/cache-config/t3c/t3c.go b/cache-config/t3c/t3c.go
index 8a71406..e59bbd3 100644
--- a/cache-config/t3c/t3c.go
+++ b/cache-config/t3c/t3c.go
@@ -20,7 +20,7 @@ package main
  */
 
 import (
-	"fmt"
+	"github.com/apache/trafficcontrol/lib/go-log"
 	"os"
 	"os/exec"
 	"syscall" // TODO change to x/unix ?
@@ -48,19 +48,20 @@ const ExitCodeCommandLookupErr = 5
 func main() {
 	flagHelp := getopt.BoolLong("help", 'h', "Print usage information and exit")
 	getopt.Parse()
+	log.Init(os.Stderr, os.Stderr, os.Stderr, os.Stderr, os.Stderr)
 	if *flagHelp {
-		fmt.Println(usageStr())
+		log.Errorln(usageStr())
 		os.Exit(ExitCodeSuccess)
 	}
 
 	if len(os.Args) < 2 {
-		fmt.Fprintf(os.Stderr, "no command\n\n"+usageStr())
+		log.Errorf("no command\n\n%s", usageStr())
 		os.Exit(ExitCodeNoCommand)
 	}
 
 	cmd := os.Args[1]
 	if _, ok := commands[cmd]; !ok {
-		fmt.Fprintf(os.Stderr, "unknown command\n") // TODO print usage
+		log.Errorf("unknown command\n%s", usageStr())
 		os.Exit(ExitCodeUnknownCommand)
 	}
 
@@ -68,7 +69,7 @@ func main() {
 
 	appPath, err := exec.LookPath(app)
 	if err != nil {
-		fmt.Fprintf(os.Stderr, "error finding path to '"+app+"': "+err.Error())
+		log.Errorf("error finding path to '%s': %s\n", app, err.Error())
 		os.Exit(ExitCodeCommandLookupErr)
 	}
 
@@ -77,7 +78,7 @@ func main() {
 	env := os.Environ()
 
 	if err := syscall.Exec(appPath, args, env); err != nil {
-		fmt.Fprintf(os.Stderr, "error executing sub-command '"+appPath+"': "+err.Error()+"\n")
+		log.Errorf("error executing sub-command '%s': %s\n", appPath, err.Error())
 		os.Exit(ExitCodeCommandErr)
 	}
 }