log to stdout / stderr

This commit is contained in:
Shreyas Goenka 2025-02-27 12:47:26 +01:00
parent a407a5462d
commit da0e08d72c
No known key found for this signature in database
GPG Key ID: 92A07DF49CCB0622
3 changed files with 7 additions and 21 deletions

View File

@ -1,3 +0,0 @@
Telemetry logs uploaded successfully
Response:
{"errors":[],"numProtoSuccess":2}

View File

@ -1,2 +1,5 @@
>>> [CLI] telemetry upload >>> [CLI] telemetry upload
Telemetry logs uploaded successfully
Response:
{"errors":[],"numProtoSuccess":2}

22
main.go
View File

@ -4,7 +4,6 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io"
"os" "os"
"github.com/databricks/cli/cmd" "github.com/databricks/cli/cmd"
@ -23,19 +22,6 @@ func main() {
// of [root.Execute] ensures that the telemetry upload process is not spawned // of [root.Execute] ensures that the telemetry upload process is not spawned
// infinitely in a recursive manner. // infinitely in a recursive manner.
if len(os.Args) == 3 && os.Args[1] == "telemetry" && os.Args[2] == "upload" { if len(os.Args) == 3 && os.Args[1] == "telemetry" && os.Args[2] == "upload" {
var err error
// By default, this command should not write anything to stdout or stderr.
outW := io.Discard
errW := io.Discard
// If the environment variable is set, redirect stdout to the file.
// This is useful for testing.
if v := os.Getenv(telemetry.UploadLogsFileEnvVar); v != "" {
outW, _ = os.OpenFile(v, os.O_CREATE|os.O_WRONLY, 0o644)
errW = outW
}
// Suppress non-error logs from the SDK. // Suppress non-error logs from the SDK.
logger.DefaultLogger = &logger.SimpleLogger{ logger.DefaultLogger = &logger.SimpleLogger{
Level: logger.LevelError, Level: logger.LevelError,
@ -43,14 +29,14 @@ func main() {
resp, err := telemetry.Upload(ctx) resp, err := telemetry.Upload(ctx)
if err != nil { if err != nil {
fmt.Fprintf(errW, "error: %s\n", err) fmt.Fprintf(os.Stderr, "error: %s\n", err)
os.Exit(1) os.Exit(1)
} }
fmt.Fprintf(outW, "Telemetry logs uploaded successfully\n") fmt.Printf("Telemetry logs uploaded successfully\n")
fmt.Fprintln(outW, "Response:") fmt.Println("Response:")
b, err := json.Marshal(resp) b, err := json.Marshal(resp)
if err == nil { if err == nil {
fmt.Fprintln(outW, string(b)) fmt.Println(string(b))
} }
os.Exit(0) os.Exit(0)
} }