Skip to content

Commit 1a54777

Browse files
authored
refactor: Make the JSON view for init commands use defaults from the hclog package (#37840)
* refactor: Change `(v *InitJSON) Output` to use logic from the hclog package to take advantage of default settings * docs: Add code comments
1 parent 3df28c3 commit 1a54777

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

internal/command/views/init.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,9 @@
44
package views
55

66
import (
7-
"encoding/json"
87
"fmt"
98
"strings"
10-
"time"
119

12-
"github.com/hashicorp/go-hclog"
1310
"github.com/hashicorp/terraform/internal/command/arguments"
1411
"github.com/hashicorp/terraform/internal/tfdiags"
1512
)
@@ -98,18 +95,21 @@ func (v *InitJSON) Output(messageCode InitMessageCode, params ...any) {
9895
return
9996
}
10097

101-
current_timestamp := time.Now().UTC().Format(hclog.TimeFormat)
102-
json_data := map[string]string{
103-
"@level": "info",
104-
"@message": preppedMessage,
105-
"@module": "terraform.ui",
106-
"@timestamp": current_timestamp,
107-
"type": "init_output",
108-
"message_code": string(messageCode),
109-
}
110-
111-
init_output, _ := json.Marshal(json_data)
112-
v.view.view.streams.Println(string(init_output))
98+
// Logged data includes by default:
99+
// @level as "info"
100+
// @module as "terraform.ui" (See NewJSONView)
101+
// @timestamp formatted in the default way
102+
//
103+
// In the method below we:
104+
// * Set @message as the first argument value
105+
// * Annotate with extra data:
106+
// "type":"init_output"
107+
// "message_code":"<value>"
108+
v.view.log.Info(
109+
preppedMessage,
110+
"type", "init_output",
111+
"message_code", string(messageCode),
112+
)
113113
}
114114

115115
func (v *InitJSON) LogInitMessage(messageCode InitMessageCode, params ...any) {

0 commit comments

Comments
 (0)