Skip to content

Commit

Permalink
feat: add consistent logging
Browse files Browse the repository at this point in the history
  • Loading branch information
acch committed Sep 27, 2024
1 parent 11560c1 commit 71abdfe
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 17 deletions.
16 changes: 7 additions & 9 deletions internal/provider/organization_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ func (d *organizationDataSource) Configure(_ context.Context, req datasource.Con

// Read refreshes the Terraform state with the latest data.
func (d *organizationDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
tflog.Trace(ctx, "Read organization data source - begin")

var data organizationDataSourceModel

// Read Terraform configuration data into model
Expand All @@ -120,18 +122,12 @@ func (d *organizationDataSource) Read(ctx context.Context, req datasource.ReadRe
return
}

// Write logs using tflog package
tflog.Trace(ctx, "get organization by name", map[string]interface{}{
"name": data.Name.ValueString(),
})
tflog.Info(ctx, "Get organization by name", map[string]any{"name": data.Name.ValueString()})

// Use forgejo.Client to get organization by name
// Use Forgejo client to get organization by name
o, r, err := d.client.GetOrg(data.Name.ValueString())
if err != nil {
// Log HTTP status
tflog.Error(ctx, "error", map[string]interface{}{
"status": r.Status,
})
tflog.Error(ctx, "Error", map[string]any{"status": r.Status})

msg := err.Error()
if r.StatusCode == 404 {
Expand All @@ -155,4 +151,6 @@ func (d *organizationDataSource) Read(ctx context.Context, req datasource.ReadRe
// Save data into Terraform state
diags = resp.State.Set(ctx, &data)
resp.Diagnostics.Append(diags...)

tflog.Trace(ctx, "Read organization data source - end", map[string]any{"success": true})
}
15 changes: 7 additions & 8 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (p *forgejoProvider) Schema(_ context.Context, _ provider.SchemaRequest, re
}

func (p *forgejoProvider) Configure(ctx context.Context, req provider.ConfigureRequest, resp *provider.ConfigureResponse) {
tflog.Info(ctx, "Configuring Forgejo client")
tflog.Trace(ctx, "Configure provider - begin")

// Retrieve provider data from configuration
var config forgejoProviderModel
Expand Down Expand Up @@ -170,12 +170,11 @@ func (p *forgejoProvider) Configure(ctx context.Context, req provider.ConfigureR
return
}

ctx = tflog.SetField(ctx, "forgejo_host", host)
ctx = tflog.SetField(ctx, "forgejo_username", username)
ctx = tflog.SetField(ctx, "forgejo_password", password)
ctx = tflog.MaskFieldValuesWithFieldKeys(ctx, "forgejo_password")

tflog.Debug(ctx, "Creating Forgejo client")
tflog.Info(ctx, "Create Forgejo client", map[string]any{
"forgejo_host": host,
"forgejo_username": username,
"forgejo_password": "***",
})

// Create a new Forgejo client using the configuration values
client, err := forgejo.NewClient(host, forgejo.SetBasicAuth(username, password))
Expand All @@ -194,7 +193,7 @@ func (p *forgejoProvider) Configure(ctx context.Context, req provider.ConfigureR
resp.DataSourceData = client
resp.ResourceData = client

tflog.Info(ctx, "Configured Forgejo client", map[string]any{"success": true})
tflog.Trace(ctx, "Configure provider - end", map[string]any{"success": true})
}

// DataSources defines the data sources implemented in the provider.
Expand Down

0 comments on commit 71abdfe

Please sign in to comment.