Skip to content

Commit

Permalink
Merge pull request #1388 from getgauge/logs
Browse files Browse the repository at this point in the history
Initialise logger for every gauge command.
  • Loading branch information
sriv committed Apr 22, 2019
2 parents 0438b7d + f6442e1 commit 562f036
Show file tree
Hide file tree
Showing 14 changed files with 31 additions and 25 deletions.
4 changes: 1 addition & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@ go:
addons:
apt:
packages:
- oracle-java8-set-default
- default-jdk
env:
- GAUGE_PREFIX="/tmp/gauge" GAUGE_TELEMETRY_ENABLED=false
before_install:
- if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then export JAVA_HOME="/usr/lib/jvm/java-8-oracle"; fi
script:
- go run build/make.go
- go run build/make.go --test
Expand Down
3 changes: 2 additions & 1 deletion cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ var (
},
DisableAutoGenTag: true,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
initLogger(cmd.Name())
skel.CreateSkelFilesIfRequired()
track.Init()
config.SetProjectRoot(args)
Expand Down Expand Up @@ -215,7 +216,7 @@ var exit = func(err error, additionalText string) {
os.Exit(0)
}

func loadEnvAndInitLogger(cmd *cobra.Command) {
func loadEnvAndReinitLogger(cmd *cobra.Command) {
if e := env.LoadEnv(environment); e != nil {
logger.Fatalf(true, e.Error())
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ var (
if err := config.SetProjectRoot(args); err != nil {
exit(err, cmd.UsageString())
}
loadEnvAndInitLogger(cmd)
loadEnvAndReinitLogger(cmd)
manifest, _ := manifest.ProjectManifest()
language := manifest.Language
if lsp {
Expand Down
2 changes: 1 addition & 1 deletion cmd/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ var docsCmd = &cobra.Command{
Long: `Generate documentation using specified plugin.`,
Example: " gauge docs spectacle specs/",
Run: func(cmd *cobra.Command, args []string) {
loadEnvAndInitLogger(cmd)
loadEnvAndReinitLogger(cmd)
if err := config.SetProjectRoot(args); err != nil {
exit(err, cmd.UsageString())
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ var formatCmd = &cobra.Command{
Long: `Formats the specified spec files.`,
Example: " gauge format specs/",
Run: func(cmd *cobra.Command, args []string) {
loadEnvAndInitLogger(cmd)
loadEnvAndReinitLogger(cmd)
if err := config.SetProjectRoot(args); err != nil {
exit(err, cmd.UsageString())
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/refactor.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ var refactorCmd = &cobra.Command{
Long: `Refactor steps.`,
Example: ` gauge refactor "old step" "new step"`,
Run: func(cmd *cobra.Command, args []string) {
loadEnvAndInitLogger(cmd)
loadEnvAndReinitLogger(cmd)
if len(args) < 2 {
exit(fmt.Errorf("Refactor command needs at least two arguments."), cmd.UsageString())
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ func installMissingPlugins(flag bool) {
}

func execute(cmd *cobra.Command, args []string) {
loadEnvAndInitLogger(cmd)
loadEnvAndReinitLogger(cmd)
if parallel && tagsToFilterForParallelRun != "" && !env.AllowFilteredParallelExecution() {
logger.Fatal(true, "Filtered parallel execution is a experimental feature. It can be enabled via allow_filtered_parallel_execution property.")
}
Expand Down
9 changes: 5 additions & 4 deletions cmd/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ import (
"github.com/spf13/cobra"
)

var path = ""
var projectPath = ""

func before() {
path, _ = filepath.Abs("_testData")
config.ProjectRoot = path
projectPath, _ = filepath.Abs("_testData")
config.ProjectRoot = projectPath
}

func after() {
os.RemoveAll(path)
os.RemoveAll(projectPath)
}

func TestMain(m *testing.M) {
Expand Down Expand Up @@ -416,6 +416,7 @@ func TestLogLevelCanBeOverriddenForRepeat(t *testing.T) {
func TestCorrectFlagsAreSetForRepeat(t *testing.T) {
if os.Getenv("TEST_EXITS") == "1" {
// expect "env" to be set to "test"
os.MkdirAll(filepath.Join(projectPath, "env", "test"), 0755)
execution.ExecuteSpecs = func(s []string) int {
f, err := runCmd.Flags().GetString(environmentName)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ var (
Long: `Check for validation and parse errors.`,
Example: " gauge validate specs/",
Run: func(cmd *cobra.Command, args []string) {
loadEnvAndInitLogger(cmd)
loadEnvAndReinitLogger(cmd)
validation.HideSuggestion = hideSuggestion
if err := config.SetProjectRoot(args); err != nil {
exit(err, cmd.UsageString())
Expand Down
13 changes: 6 additions & 7 deletions plugin/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,6 @@ func installPluginVersion(installDesc *installDescription, versionInstallDescrip

tempDir := common.GetTempDir()
defer common.Remove(tempDir)
logger.Debugf(true, "Downloading %s", filepath.Base(downloadLink))
pluginZip, err := util.Download(downloadLink, tempDir, "", silent)
if err != nil {
return installError(fmt.Errorf("Failed to download the plugin. %s", err.Error()))
Expand Down Expand Up @@ -422,11 +421,11 @@ func constructPluginInstallJSONURL(p string) (string, InstallResult) {
if repoURL == "" {
return "", installError(fmt.Errorf("Could not find gauge repository url from configuration."))
}
JSONURL := fmt.Sprintf("%s/%s", repoURL, p)
jsonURL := fmt.Sprintf("%s/%s", repoURL, p)
if qp := plugin.QueryParams(); qp != "" {
JSONURL += qp
jsonURL += qp
}
return JSONURL, installSuccess("")
return jsonURL, installSuccess("")
}

func (installDesc *installDescription) getVersion(version string) (*versionInstallDescription, error) {
Expand Down Expand Up @@ -518,8 +517,8 @@ func HandleInstallResult(result InstallResult, pluginName string, exitIfFailure
return true
}
if !result.Success {
if (result.Version != "") {
logger.Errorf(true, "Failed to install plugin '%s' version %s.\nReason: %s", pluginName, result.Version, result.getMessage())
if result.Version != "" {
logger.Errorf(true, "Failed to install plugin '%s' version %s.\nReason: %s", pluginName, result.Version, result.getMessage())
} else {
logger.Errorf(true, "Failed to install plugin '%s'.\nReason: %s", pluginName, result.getMessage())
}
Expand All @@ -528,7 +527,7 @@ func HandleInstallResult(result InstallResult, pluginName string, exitIfFailure
}
return false
}
if (result.Version != "") {
if result.Version != "" {
logger.Infof(true, "Successfully installed plugin '%s' version %s", pluginName, result.Version)
} else {
logger.Infof(true, "Successfully installed plugin '%s'", pluginName)
Expand Down
7 changes: 5 additions & 2 deletions projectInit/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ type templateMetadata struct {
func initializeTemplate(templateName string) error {
tempDir := common.GetTempDir()
defer util.Remove(tempDir)
unzippedTemplate, err := util.DownloadAndUnzip(getTemplateURL(templateName), tempDir)
templateURL := getTemplateURL(templateName)
logger.Debugf(true, "Initializing template from %s", templateURL)
unzippedTemplate, err := util.DownloadAndUnzip(templateURL, tempDir)
if err != nil {
return err
}
Expand Down Expand Up @@ -71,6 +73,7 @@ func initializeTemplate(templateName string) error {
}

if metadata.PostInstallCmd != "" {
logger.Debugf(true, "Running post install command %s", metadata.PostInstallCmd)
command := strings.Fields(metadata.PostInstallCmd)
cmd, err := common.ExecuteSystemCommand(command, wd, os.Stdout, os.Stderr)
if err != nil {
Expand All @@ -84,7 +87,7 @@ func initializeTemplate(templateName string) error {
return err
}
}
fmt.Printf("Successfully initialized the project. %s\n", metadata.PostInstallMsg)
logger.Infof(true, "Successfully initialized the project. %s\n", metadata.PostInstallMsg)

util.Remove(metadataFile)
return nil
Expand Down
4 changes: 2 additions & 2 deletions projectInit/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ func ListTemplates() {
templatesURL := config.GaugeTemplatesUrl()
_, err := common.UrlExists(templatesURL)
if err != nil {
logger.Fatalf(true, "Gauge templates URL is not reachable: %s", err.Error())
logger.Fatalf(true, "Gauge templates URL %s is not reachable: %s", templatesURL, err.Error())
}
res, err := http.Get(templatesURL)
if err != nil {
logger.Fatalf(true, "Error occurred while downloading templates list: %s", err.Error())
logger.Fatalf(true, "Error occurred while downloading templates list from %s: %s", templatesURL, err.Error())
}
defer res.Body.Close()
if res.StatusCode >= 400 {
Expand Down
1 change: 1 addition & 0 deletions runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ func ExecuteInitHookForRunner(language string) error {

languageJSONFilePath, err := plugin.GetLanguageJSONFilePath(language)
runnerDir := filepath.Dir(languageJSONFilePath)
logger.Debugf(true, "Running init hook command => %s", command)
cmd, err := common.ExecuteCommand(command, runnerDir, os.Stdout, os.Stderr)

if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions util/httpUtils.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (
"os"
"path/filepath"

"github.com/getgauge/gauge/logger"

"github.com/getgauge/common"
)

Expand Down Expand Up @@ -63,6 +65,7 @@ func Download(url, targetDir, fileName string, silent bool) (string, error) {
}
targetFile := filepath.Join(targetDir, fileName)

logger.Debugf(true, "Downloading %s", url)
resp, err := http.Get(url)
if err != nil {
return "", err
Expand Down

0 comments on commit 562f036

Please sign in to comment.