Skip to content

Commit

Permalink
Improve logging
Browse files Browse the repository at this point in the history
  • Loading branch information
jocgir committed May 16, 2017
1 parent 4007fc9 commit 0f3138e
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 8 deletions.
8 changes: 7 additions & 1 deletion configstack/running_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"github.com/gruntwork-io/terragrunt/errors"
"github.com/gruntwork-io/terragrunt/shell"
"github.com/gruntwork-io/terragrunt/util"
"io"
"strings"
"sync"
Expand Down Expand Up @@ -232,7 +233,12 @@ func (module *runningModule) moduleFinished(moduleErr error) {
if module.Handler != nil {
moduleErr = module.Handler(*module.Module, module.OutStream.String(), moduleErr)
}
fmt.Fprintf(module.Writer, "%s\n%v\n\n%v\n", separator, module.Module.Path, module.OutStream.String())
out := module.OutStream.String()
if out == "" {
module.Module.TerragruntOptions.Logger.Notice("No output")
} else {
fmt.Fprintf(module.Writer, "%s\n%v\n\n%v\n", separator, util.GetPathRelativeToWorkingDir(module.Module.Path), module.OutStream.String())
}

module.Status = Finished
module.Err = moduleErr
Expand Down
20 changes: 16 additions & 4 deletions configstack/stack_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,12 @@ func getResultHandler(detailedExitCode bool, results *[]moduleResult) ModuleHand
err = nil
}

message, count := extractSummaryResultFromPlan(output)
if output != "" {
message, count := extractSummaryResultFromPlan(output)

// We add the result to the result list (there is no concurrency problem because it is handled by the running_module)
*results = append(*results, moduleResult{module, err, message, count})
// We add the result to the result list (there is no concurrency problem because it is handled by the running_module)
*results = append(*results, moduleResult{module, err, message, count})
}

return err
}
Expand All @@ -75,13 +77,23 @@ func getResultHandler(detailedExitCode bool, results *[]moduleResult) ModuleHand
// Print a little summary of the plan execution
func printSummary(terragruntOptions *options.TerragruntOptions, results []moduleResult) {
fmt.Fprintf(terragruntOptions.Writer, "%s\nSummary:\n", separator)

var length int
for _, result := range results {
nameLength := len(util.GetPathRelativeToWorkingDir(result.Module.Path))
if nameLength > length {
length = nameLength
}
}

format := fmt.Sprintf(" %%-%dv : %%v%%v\n", length)
for _, result := range results {
errMsg := ""
if result.Err != nil {
errMsg = fmt.Sprintf(", Error: %v", result.Err)
}

fmt.Fprintf(terragruntOptions.Writer, " %v : %v%v\n", result.Module.Path, result.Message, errMsg)
fmt.Fprintf(terragruntOptions.Writer, format, util.GetPathRelativeToWorkingDir(result.Module.Path), result.Message, errMsg)
}
}

Expand Down
2 changes: 1 addition & 1 deletion options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func (terragruntOptions *TerragruntOptions) Clone(terragruntConfigPath string) *
NonInteractive: terragruntOptions.NonInteractive,
TerraformCliArgs: terragruntOptions.TerraformCliArgs,
WorkingDir: workingDir,
Logger: util.CreateLogger(workingDir),
Logger: util.CreateLogger(util.GetPathRelativeToWorkingDir(workingDir)),
Env: terragruntOptions.Env,
Variables: VariableList{},
Source: terragruntOptions.Source,
Expand Down
10 changes: 10 additions & 0 deletions util/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,16 @@ func GetPathRelativeTo(path string, basePath string) (string, error) {
return filepath.ToSlash(relPath), nil
}

// Return the path relative to the current working directory
func GetPathRelativeToWorkingDir(path string) (result string) {
currentDir, err := os.Getwd()
result = path
if err == nil {
result, err = GetPathRelativeTo(path, currentDir)
}
return
}

// Return the contents of the file at the given path as a string
func ReadFileAsString(path string) (string, error) {
bytes, err := ioutil.ReadFile(path)
Expand Down
4 changes: 2 additions & 2 deletions util/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ func CreateLogger(prefix string) *logging.Logger {
func InitLogging(levelName string, defaultLevel logging.Level, color bool) error {
var format string
if color {
format = `[terragrunt%{module}] %{time:2006/01/02 15:04:05}: %{color}%{level:-8s} %{message}%{color:reset}`
format = `[terragrunt%{module}] %{time:2006/01/02 15:04:05} %{color}%{level:-8s} %{message}%{color:reset}`
} else {
format = `[terragrunt%{module}] %{time:2006/01/02 15:04:05}: %{level:-8s} %{message}`
format = `[terragrunt%{module}] %{time:2006/01/02 15:04:05} %{level:-8s} %{message}`
}

logging.SetBackend(logging.NewBackendFormatter(logging.NewLogBackend(os.Stderr, "", 0), logging.MustStringFormatter(format)))
Expand Down

0 comments on commit 0f3138e

Please sign in to comment.