Skip to content

Commit

Permalink
Raise the default log level of TF from WARN to ERROR
Browse files Browse the repository at this point in the history
  • Loading branch information
iwahbe committed Feb 14, 2024
1 parent 7eef4a3 commit 02eea91
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 28 deletions.
32 changes: 9 additions & 23 deletions unstable/logging/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,10 +207,15 @@ func setupRootLoggers(ctx context.Context, output io.Writer) context.Context {
}

// Choose the default level carefully: logs at this level or higher (more severe) will be shown to the user of Pulumi
// CLI directly by default. Experimentally it seems that Info is too verbose, for example Cloudflare provider emits
// routine authentication messages at INFO level.
// CLI directly by default. Experimentally it seems that WARN is too verbose:
//
// - AWS (via terraform-plugin-sdk@v2) emits developer logs at WARN which are not shown to terraform users.
//
// Citation:
// - https://github.com/hashicorp/terraform-plugin-sdk/blob/43cfd3282307f68ea77eb4c15548100386f3a317/helper/customdiff/force_new.go#L32-L35

Check failure on line 215 in unstable/logging/logging.go

View workflow job for this annotation

GitHub Actions / Build and Test Bridge (1.20.x, ubuntu-latest)

line is 139 characters (lll)

Check failure on line 215 in unstable/logging/logging.go

View workflow job for this annotation

GitHub Actions / Build and Test Bridge (1.21.x, ubuntu-latest)

line is 139 characters (lll)
// - https://github.com/pulumi/pulumi-aws/issues/3389
func defaultTFLogLevel() hclog.Level {
return hclog.Warn
return hclog.Error
}

func makeLoggerOptions(name string, level hclog.Level, output io.Writer) *hclog.LoggerOptions {
Expand Down Expand Up @@ -270,26 +275,7 @@ func (w *logSinkWriter) Write(p []byte) (n int, err error) {
}

func parseTfLogEnvVar() hclog.Level {
env, present := os.LookupEnv(tfLogEnvVar)
if !present {
return hclog.NoLevel
}
switch strings.ToUpper(env) {
case "ERROR":
return hclog.Error
case "WARN":
return hclog.Warn
case "INFO":
return hclog.Info
case "TRACE":
return hclog.Trace
case "DEBUG":
return hclog.Debug
case "OFF":
return hclog.Off
default:
return hclog.NoLevel
}
return hclog.LevelFromString(os.Getenv(tfLogEnvVar))
}

var quotedUrnPattern = regexp.MustCompile(`[ ]urn=["]([^"]+)["]`)
Expand Down
15 changes: 10 additions & 5 deletions unstable/logging/logging_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ import (
func TestLogging(t *testing.T) {
urn := resource.URN("urn:pulumi:prod::web::custom:resources:Resource$random:index/password:Password::my-pw")

warn := map[string]string{"TF_LOG": "WARN"}

cases := []struct {
name string
opts LogOptions
Expand All @@ -39,8 +41,11 @@ func TestLogging(t *testing.T) {
env map[string]string
}{
{
name: "WARN and higher propagates by default",
name: "logging is disabled by default",
opts: LogOptions{},
env: map[string]string{
"TF_LOG": "",
},
emit: func(ctx context.Context) {
tflog.Trace(ctx, "Something went wrong TRACE")
tflog.Debug(ctx, "Something went wrong DEBUG")
Expand All @@ -49,10 +54,6 @@ func TestLogging(t *testing.T) {
tflog.Error(ctx, "Something went wrong ERROR ")
},
logs: []log{
{
msg: `Something went wrong WARN`,
sev: diag.Warning,
},
{
msg: `Something went wrong ERROR`,
sev: diag.Error,
Expand Down Expand Up @@ -114,13 +115,15 @@ func TestLogging(t *testing.T) {
{
name: "URN propagates when set",
opts: LogOptions{URN: urn},
env: warn,
emit: func(ctx context.Context) {
tflog.Warn(ctx, "OK")
},
logs: []log{{sev: diag.Warning, msg: `OK`, urn: urn}},
},
{
name: "Provider propagates when set",
env: warn,
opts: LogOptions{ProviderName: "random"},
emit: func(ctx context.Context) {
tflog.Warn(ctx, "OK")
Expand All @@ -130,13 +133,15 @@ func TestLogging(t *testing.T) {
{
name: "ProviderVersion propagates when set",
opts: LogOptions{ProviderName: "random", ProviderVersion: "4.12.0"},
env: warn,
emit: func(ctx context.Context) {
tflog.Warn(ctx, "OK")
},
logs: []log{{sev: diag.Warning, msg: `provider\[email protected]`}},
},
{
name: "User Logging",
env: warn,
opts: LogOptions{URN: urn},
emit: func(ctx context.Context) {
log := getLogger(ctx)
Expand Down

0 comments on commit 02eea91

Please sign in to comment.