Skip to content

Commit

Permalink
Fix various logging issues. Log package refactoring. (#3400)
Browse files Browse the repository at this point in the history
* chore: relative path log hook

* chore: log refactoring

* chore: log formatters

* chore: log formatters

* chore: log formatters

* chore: revert telemtry code

* chore: log writer

* chore: log format options

* chore: log format

* chore: log format

* chore: code improvements

* chore: revert log formatter code

* chore: fix test

* chore: fix tests

* chore: fix mock test

* chore: fix tests

* chore: fix test

* chore: fix test

* chore: fix tflint test

* chore: code improvements

* fix: lint

* chore: fix tests

* chore: fix lint

* chore: fix test

* chore: fix test

* chore: fix test

* chore: fix test

* chore: fix test

* chore: code optimization

* chore: code optimization

* chore: code optimization

* chore: code optimization

* chore: code commenting, docs updating

* chore: add integration test

* fix: forwarding console command output

* chore: code improvements

* feat: add disable logging

* chore: fix regression

* chore: fix regression

* chore: fix regexp

* chore: fix tflog parse time

* chore: fix grammar

* fix: tflog time parse

* chore: code improvements

* chore: code improvements

* chore: fix test
  • Loading branch information
levkohimins committed Sep 13, 2024
1 parent bc8f543 commit 68e95e1
Show file tree
Hide file tree
Showing 110 changed files with 2,207 additions and 1,262 deletions.
60 changes: 22 additions & 38 deletions cli/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import (
"context"
goerrors "errors"
"fmt"
"io"
"os"
"path/filepath"
"sort"
"time"

"github.com/gruntwork-io/terragrunt/engine"

"github.com/gruntwork-io/terragrunt/pkg/log"
"github.com/gruntwork-io/terragrunt/pkg/log/format"
"github.com/gruntwork-io/terragrunt/pkg/log/hooks"
"github.com/gruntwork-io/terragrunt/telemetry"
"github.com/gruntwork-io/terragrunt/terraform"
"golang.org/x/sync/errgroup"
Expand Down Expand Up @@ -43,7 +44,6 @@ import (
terraformCmd "github.com/gruntwork-io/terragrunt/cli/commands/terraform"
terragruntinfo "github.com/gruntwork-io/terragrunt/cli/commands/terragrunt-info"
validateinputs "github.com/gruntwork-io/terragrunt/cli/commands/validate-inputs"
"github.com/gruntwork-io/terragrunt/internal/log"
"github.com/gruntwork-io/terragrunt/options"
"github.com/gruntwork-io/terragrunt/pkg/cli"
)
Expand All @@ -59,21 +59,18 @@ func init() {

type App struct {
*cli.App
opts *options.TerragruntOptions
}

// NewApp creates the Terragrunt CLI App.
func NewApp(writer io.Writer, errWriter io.Writer) *App {
opts := options.NewTerragruntOptions()
opts.Writer = writer
opts.ErrWriter = errWriter

func NewApp(opts *options.TerragruntOptions) *App {
app := cli.NewApp()
app.Name = "terragrunt"
app.Usage = "Terragrunt is a flexible orchestration tool that allows Infrastructure as Code written in OpenTofu/Terraform to scale. For documentation, see https://terragrunt.gruntwork.io/."
app.Author = "Gruntwork <www.gruntwork.io>"
app.Version = version.GetVersion()
app.Writer = writer
app.ErrWriter = errWriter
app.Writer = opts.Writer
app.ErrWriter = opts.ErrWriter

app.Flags = append(
commands.NewGlobalFlags(opts),
Expand All @@ -87,7 +84,7 @@ func NewApp(writer io.Writer, errWriter io.Writer) *App {
app.DefaultCommand = terraformCmd.NewCommand(opts).WrapAction(WrapWithTelemetry(opts)) // by default, if no terragrunt command is specified, run the Terraform command
app.OsExiter = OSExiter

return &App{app}
return &App{app, opts}
}

func (app *App) Run(args []string) error {
Expand All @@ -99,16 +96,16 @@ func (app *App) RunContext(ctx context.Context, args []string) error {
defer cancel()

shell.RegisterSignalHandler(func(signal os.Signal) {
log.Infof("%s signal received. Gracefully shutting down... (it can take up to %v)", cases.Title(language.English).String(signal.String()), shell.SignalForwardingDelay)
app.opts.Logger.Infof("%s signal received. Gracefully shutting down... (it can take up to %v)", cases.Title(language.English).String(signal.String()), shell.SignalForwardingDelay)
cancel()

shell.RegisterSignalHandler(func(signal os.Signal) {
log.Infof("Second %s signal received, force shutting down...", cases.Title(language.English).String(signal.String()))
app.opts.Logger.Infof("Second %s signal received, force shutting down...", cases.Title(language.English).String(signal.String()))
os.Exit(1)
})

time.Sleep(forceExitInterval)
log.Infof("Failed to gracefully shutdown within %v, force shutting down...", forceExitInterval)
app.opts.Logger.Infof("Failed to gracefully shutdown within %v, force shutting down...", forceExitInterval)
os.Exit(1)
}, shell.InterruptSignals...)

Expand Down Expand Up @@ -283,19 +280,6 @@ func initialSetup(cliCtx *cli.Context, opts *options.TerragruntOptions) error {

opts.Env = env.Parse(os.Environ())

// --- Logger
if opts.DisableLogColors {
util.DisableLogColors()
}

if opts.DisableLogFormatting {
util.DisableLogFormatting()
}

if opts.JsonLogFormat {
util.JsonFormat()
}

// --- Working Dir
if opts.WorkingDir == "" {
currentDir, err := os.Getwd()
Expand All @@ -313,8 +297,19 @@ func initialSetup(cliCtx *cli.Context, opts *options.TerragruntOptions) error {
return errors.WithStackTrace(err)
}

opts.Logger = opts.Logger.WithField(format.PrefixKeyName, workingDir)

opts.RootWorkingDir = filepath.ToSlash(workingDir)

if !opts.LogShowAbsPaths {
hook, err := hooks.NewRelativePathHook(opts.RootWorkingDir)
if err != nil {
return err
}

opts.Logger.SetOptions(log.WithHooks(hook))
}

// --- Download Dir
if opts.DownloadDir == "" {
opts.DownloadDir = util.JoinPath(opts.WorkingDir, util.TerragruntCacheDir)
Expand All @@ -327,12 +322,6 @@ func initialSetup(cliCtx *cli.Context, opts *options.TerragruntOptions) error {

opts.DownloadDir = filepath.ToSlash(downloadDir)

opts.LogLevel = util.ParseLogLevel(opts.LogLevelStr)
opts.Logger = util.CreateLogEntry("", opts.LogLevel, nil, opts.DisableLogColors, opts.DisableLogFormatting)
opts.Logger.Logger.SetOutput(cliCtx.App.ErrWriter)

log.SetLogger(opts.Logger.Logger)

// --- Terragrunt ConfigPath
if opts.TerragruntConfigPath == "" {
opts.TerragruntConfigPath = config.GetDefaultConfigPath(opts.WorkingDir)
Expand All @@ -345,11 +334,6 @@ func initialSetup(cliCtx *cli.Context, opts *options.TerragruntOptions) error {
return errors.WithStackTrace(err)
}

opts.RelativeTerragruntConfigPath, err = util.GetPathRelativeToWithSeparator(opts.TerragruntConfigPath, opts.RootWorkingDir)
if err != nil {
return err
}

opts.TerraformPath = filepath.ToSlash(opts.TerraformPath)

opts.ExcludeDirs, err = util.GlobCanonicalPath(opts.WorkingDir, opts.ExcludeDirs...)
Expand Down
36 changes: 20 additions & 16 deletions cli/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ import (
"github.com/gruntwork-io/terragrunt/config"
"github.com/gruntwork-io/terragrunt/options"
cliPkg "github.com/gruntwork-io/terragrunt/pkg/cli"
"github.com/gruntwork-io/terragrunt/pkg/log"
"github.com/gruntwork-io/terragrunt/terraform"
"github.com/gruntwork-io/terragrunt/util"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

var defaultLogLevel = util.GetDefaultLogLevel()
var defaultLogLevel = log.DebugLevel

func TestParseTerragruntOptionsFromArgs(t *testing.T) {
t.Parallel()
Expand Down Expand Up @@ -154,7 +154,7 @@ func TestParseTerragruntOptionsFromArgs(t *testing.T) {
// Adding the --terragrunt-log-level flag should result in DebugLevel configured
{
[]string{doubleDashed(commands.TerragruntLogLevelFlagName), "debug"},
mockOptions(t, util.JoinPath(workingDir, config.DefaultTerragruntConfigPath), workingDir, []string{}, false, "", false, false, logrus.DebugLevel, false),
mockOptions(t, util.JoinPath(workingDir, config.DefaultTerragruntConfigPath), workingDir, []string{}, false, "", false, false, log.DebugLevel, false),
nil,
},
{
Expand Down Expand Up @@ -220,7 +220,7 @@ func assertOptionsEqual(t *testing.T, expected options.TerragruntOptions, actual
assert.Equal(t, expected.SourceMap, actual.SourceMap, msgAndArgs...)
}

func mockOptions(t *testing.T, terragruntConfigPath string, workingDir string, terraformCliArgs []string, nonInteractive bool, terragruntSource string, ignoreDependencyErrors bool, includeExternalDependencies bool, logLevel logrus.Level, debug bool) *options.TerragruntOptions {
func mockOptions(t *testing.T, terragruntConfigPath string, workingDir string, terraformCliArgs []string, nonInteractive bool, terragruntSource string, ignoreDependencyErrors bool, includeExternalDependencies bool, logLevel log.Level, debug bool) *options.TerragruntOptions {
t.Helper()

opts, err := options.NewTerragruntOptionsForTest(terragruntConfigPath)
Expand All @@ -234,7 +234,7 @@ func mockOptions(t *testing.T, terragruntConfigPath string, workingDir string, t
opts.Source = terragruntSource
opts.IgnoreDependencyErrors = ignoreDependencyErrors
opts.IncludeExternalDependencies = includeExternalDependencies
opts.Logger.Level = logLevel
opts.Logger.SetOptions(log.WithLevel(logLevel))
opts.Debug = debug

return opts
Expand Down Expand Up @@ -390,7 +390,8 @@ func TestTerragruntVersion(t *testing.T) {

for _, testCase := range testCases {
output := &bytes.Buffer{}
app := cli.NewApp(output, os.Stderr)
opts := options.NewTerragruntOptionsWithWriters(output, os.Stderr)
app := cli.NewApp(opts)
app.Version = version

err := app.Run(testCase.args)
Expand All @@ -403,7 +404,8 @@ func TestTerragruntVersion(t *testing.T) {
func TestTerragruntHelp(t *testing.T) {
t.Parallel()

app := cli.NewApp(os.Stdout, os.Stderr)
opts := options.NewTerragruntOptions()
app := cli.NewApp(opts)

testCases := []struct {
args []string
Expand All @@ -419,7 +421,8 @@ func TestTerragruntHelp(t *testing.T) {

for _, testCase := range testCases {
output := &bytes.Buffer{}
app := cli.NewApp(output, os.Stderr)
opts := options.NewTerragruntOptionsWithWriters(output, os.Stderr)
app := cli.NewApp(opts)
err := app.Run(testCase.args)
require.NoError(t, err, testCase)

Expand All @@ -446,7 +449,8 @@ func TestTerraformHelp(t *testing.T) {

for _, testCase := range testCases {
output := &bytes.Buffer{}
app := cli.NewApp(output, os.Stderr)
opts := options.NewTerragruntOptionsWithWriters(output, os.Stderr)
app := cli.NewApp(opts)
err := app.Run(testCase.args)
require.NoError(t, err)

Expand All @@ -460,10 +464,10 @@ func TestTerraformHelp(t *testing.T) {
func TestTerraformHelp_wrongHelpFlag(t *testing.T) {
t.Parallel()

app := cli.NewApp(os.Stdout, os.Stderr)

output := &bytes.Buffer{}
app.Writer = output

opts := options.NewTerragruntOptionsWithWriters(output, os.Stderr)
app := cli.NewApp(opts)

err := app.Run([]string{"terragrunt", "plan", "help"})
require.Error(t, err)
Expand Down Expand Up @@ -509,9 +513,7 @@ func (err argMissingValueError) Error() string {
return "flag needs an argument: -" + string(err)
}

func TestAutocomplete(t *testing.T) {
t.Parallel()

func TestAutocomplete(t *testing.T) { //nolint:paralleltest
defer os.Unsetenv("COMP_LINE")

testCases := []struct {
Expand Down Expand Up @@ -540,7 +542,9 @@ func TestAutocomplete(t *testing.T) {
os.Setenv("COMP_LINE", "terragrunt "+testCase.compLine)

output := &bytes.Buffer{}
app := cli.NewApp(output, os.Stderr)
opts := options.NewTerragruntOptionsWithWriters(output, os.Stderr)
app := cli.NewApp(opts)

app.Commands = app.Commands.Filter([]string{"aws-provider-patch", "graph-dependencies", "hclfmt", "output-module-groups", "render-json", "run-all", "terragrunt-info", "validate-inputs"})

err := app.Run([]string{"terragrunt"})
Expand Down
5 changes: 2 additions & 3 deletions cli/commands/catalog/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/gruntwork-io/terragrunt/cli/commands/catalog/module"
"github.com/gruntwork-io/terragrunt/cli/commands/catalog/tui"
"github.com/gruntwork-io/terragrunt/config"
"github.com/gruntwork-io/terragrunt/internal/log"
"github.com/gruntwork-io/terragrunt/options"
"github.com/gruntwork-io/terragrunt/util"
)
Expand Down Expand Up @@ -40,7 +39,7 @@ func Run(ctx context.Context, opts *options.TerragruntOptions, repoURL string) e
for _, repoURL := range repoURLs {
tempDir := filepath.Join(os.TempDir(), fmt.Sprintf(tempDirFormat, util.EncodeBase64Sha1(repoURL)))

repo, err := module.NewRepo(ctx, repoURL, tempDir)
repo, err := module.NewRepo(ctx, opts.Logger, repoURL, tempDir)
if err != nil {
return err
}
Expand All @@ -50,7 +49,7 @@ func Run(ctx context.Context, opts *options.TerragruntOptions, repoURL string) e
return err
}

log.Infof("Found %d modules in repository %q", len(repoModules), repoURL)
opts.Logger.Infof("Found %d modules in repository %q", len(repoModules), repoURL)

modules = append(modules, repoModules...)
}
Expand Down
10 changes: 8 additions & 2 deletions cli/commands/catalog/module/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

"github.com/gruntwork-io/go-commons/collections"
"github.com/gruntwork-io/go-commons/errors"
"github.com/gruntwork-io/terragrunt/internal/log"
"github.com/gruntwork-io/terragrunt/pkg/log"
)

const (
Expand All @@ -22,6 +22,7 @@ var (
type Modules []*Module

type Module struct {
*Repo
*Doc

cloneURL string
Expand All @@ -33,6 +34,7 @@ type Module struct {
// NewModule returns a module instance if the given `moduleDir` path contains a Terraform module, otherwise returns nil.
func NewModule(repo *Repo, moduleDir string) (*Module, error) {
module := &Module{
Repo: repo,
cloneURL: repo.cloneURL,
repoPath: repo.path,
moduleDir: moduleDir,
Expand All @@ -42,7 +44,7 @@ func NewModule(repo *Repo, moduleDir string) (*Module, error) {
return nil, err
}

log.Debugf("Found module in directory %q", moduleDir)
repo.logger.Debugf("Found module in directory %q", moduleDir)

moduleURL, err := repo.ModuleURL(moduleDir)
if err != nil {
Expand All @@ -63,6 +65,10 @@ func NewModule(repo *Repo, moduleDir string) (*Module, error) {
return module, nil
}

func (module *Module) Logger() log.Logger {
return module.logger
}

// FilterValue implements /github.com/charmbracelet/bubbles.list.Item.FilterValue
func (module *Module) FilterValue() string {
return module.Title()
Expand Down
Loading

0 comments on commit 68e95e1

Please sign in to comment.