From fa04f77b826cd6b2ccd28553cc48e11bfd881b0a Mon Sep 17 00:00:00 2001 From: Darshil Chanpura Date: Sat, 27 Apr 2019 10:42:45 +0530 Subject: [PATCH] Update: Added log with timestamp rather than just using log.Printf() --- cmd/version.go | 4 ++-- utils/functions.go | 15 ++++++++++++--- utils/functions_test.go | 8 ++++++++ 3 files changed, 22 insertions(+), 5 deletions(-) create mode 100644 utils/functions_test.go diff --git a/cmd/version.go b/cmd/version.go index e897316..23a8ae7 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -38,8 +38,8 @@ var versionCmd = &cobra.Command{ func init() { RootCmd.AddCommand(versionCmd) - version = "0.8.0-alpha" - buildDate = "2019-04-12 11:18:28 IST" + version = "0.7.2" + buildDate = "2019-04-27 10:42:09 IST" // Here you will define your flags and configuration settings. // Cobra supports Persistent Flags which will work for this command diff --git a/utils/functions.go b/utils/functions.go index dd73943..a1066c7 100644 --- a/utils/functions.go +++ b/utils/functions.go @@ -5,6 +5,7 @@ import ( "log" "os" "os/exec" + "time" "github.com/dtchanpura/deployment-agent/constants" ) @@ -16,16 +17,24 @@ func ExecuteScript(workdir string, execpath string, args ...string) error { if dirInfo, err := os.Stat(workdir); err == nil && dirInfo.IsDir() { cmd.Dir = workdir } else { - log.Println(err) + logWithTimestampln(err.Error()) } // err := cmd.Run() outputBytes, err := cmd.CombinedOutput() if err != nil { - log.Printf("Command Error: %s\n", string(outputBytes[:])) + logWithTimestampf("Command Error: %s\n", string(outputBytes[:])) return err } - log.Printf("Command Output: %s\n", string(outputBytes[:])) + logWithTimestampf("Command Output: %s\n", string(outputBytes[:])) return nil } return errors.New(constants.ErrorFileNotExecutable) } + +func logWithTimestampf(message string, args ...interface{}) { + log.Printf(time.Now().Format("[ 2006/01/02 15:04:05 ] ")+message, args...) +} + +func logWithTimestampln(message string) { + logWithTimestampf(message + "\n") +} diff --git a/utils/functions_test.go b/utils/functions_test.go new file mode 100644 index 0000000..c1e5d9b --- /dev/null +++ b/utils/functions_test.go @@ -0,0 +1,8 @@ +package utils + +import "testing" + +func TestLog(t *testing.T) { + // t.SkipNow() + logWithTimestampln("testing") +}