Skip to content

Commit 90ea722

Browse files
authored
Replace logrus with log/slog (#634)
1 parent 4464cf6 commit 90ea722

154 files changed

Lines changed: 480 additions & 372 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎.github/copilot-instructions.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ The CLI is communicating with the Supervisor using the Supervisor's HTTP REST AP
2020
- **CLI Framework**: Cobra (github.com/spf13/cobra)
2121
- **HTTP Client**: Resty (github.com/go-resty/resty/v2)
2222
- **Configuration**: Viper (github.com/spf13/viper)
23-
- **Logging**: Logrus (github.com/sirupsen/logrus)
23+
- **Logging**: Go stdlib log/slog
2424

2525
## Available Commands
2626
The CLI provides the following main command categories:
@@ -62,7 +62,7 @@ The CLI provides the following main command categories:
6262
- Uses Cobra for command structure and flag parsing
6363
- Resty for HTTP API calls to Home Assistant Supervisor
6464
- Viper for configuration management
65-
- Logrus for structured logging
65+
- Go stdlib log/slog for structured logging
6666
- Custom spinner implementation for progress indication
6767

6868
## Testing

‎client/client.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ package client
33
import (
44
"errors"
55
"fmt"
6+
"log/slog"
67
"net/http"
78
"time"
89

910
resty "github.com/go-resty/resty/v2"
10-
log "github.com/sirupsen/logrus"
1111
)
1212

1313
// RawJSON controls if the client does json handling or outputs it raw
@@ -62,7 +62,7 @@ func genericJSONMethod(get bool, section, command string, body map[string]any, t
6262
resp, err = request.Get(url)
6363
} else {
6464
if len(body) > 0 {
65-
log.WithField("body", body).Debug("Request body")
65+
slog.Debug("Request body", "body", body)
6666
request.SetBody(body)
6767
}
6868
resp, err = request.Post(url)

‎client/helper.go‎

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"errors"
77
"fmt"
88
"io"
9+
"log/slog"
910
"net/url"
1011
"os"
1112
"os/signal"
@@ -18,7 +19,6 @@ import (
1819

1920
yaml "github.com/ghodss/yaml"
2021
resty "github.com/go-resty/resty/v2"
21-
log "github.com/sirupsen/logrus"
2222
"github.com/spf13/viper"
2323

2424
"strings"
@@ -43,11 +43,7 @@ type Response struct {
4343
// URLHelper returns a URL built from the arguments
4444
func URLHelper(section, command string) (string, error) {
4545
base := viper.GetString("endpoint")
46-
log.WithFields(log.Fields{
47-
"base": base,
48-
"section": section,
49-
"command": command,
50-
}).Debug("[GenerateURI]")
46+
slog.Debug("[GenerateURI]", "base", base, "section", section, "command", command)
5147

5248
scheme := ""
5349
if !strings.Contains(base, "://") {
@@ -69,11 +65,7 @@ func URLHelper(section, command string) (string, error) {
6965
myurl.Path = path.Clean(myurl.Path)
7066

7167
res, _ := url.PathUnescape(myurl.String())
72-
log.WithFields(log.Fields{
73-
"uri": uri,
74-
"url": myurl,
75-
"url(string)": res,
76-
}).Debug("[GenerateURI] Result")
68+
slog.Debug("[GenerateURI] Result", "uri", uri, "url", myurl, "url(string)", res)
7769
return res, nil
7870
}
7971

@@ -108,15 +100,7 @@ func GetRequestTimeout(timeout time.Duration) *resty.Request {
108100
// Registering Response Middleware
109101
client.OnAfterResponse(func(c *resty.Client, resp *resty.Response) error {
110102
// explore response object
111-
log.WithFields(log.Fields{
112-
"statuscode": resp.StatusCode(),
113-
"status": resp.Status(),
114-
"time": resp.Time(),
115-
"received-at": resp.ReceivedAt(),
116-
"headers": resp.Header(),
117-
"request": resp.Request.RawRequest,
118-
"body": resp,
119-
}).Debug("Response")
103+
slog.Debug("Response", "statuscode", resp.StatusCode(), "status", resp.Status(), "time", resp.Time(), "received-at", resp.ReceivedAt(), "headers", resp.Header(), "request", resp.Request.RawRequest, "body", resp)
120104

121105
return nil // if its success otherwise return error
122106
})

‎cmd/apps.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package cmd
22

33
import (
4+
"log/slog"
45
"os"
56
"strings"
67

78
helper "github.com/home-assistant/cli/client"
8-
log "github.com/sirupsen/logrus"
99
"github.com/spf13/cobra"
1010
)
1111

@@ -30,7 +30,7 @@ information commands for apps.`,
3030
rootCmd.PersistentPreRun(cmd, args)
3131
},
3232
Run: func(cmd *cobra.Command, args []string) {
33-
log.WithField("args", args).Debug("apps")
33+
slog.Debug("apps", "args", args)
3434

3535
section := "addons"
3636
command := ""
@@ -46,7 +46,7 @@ information commands for apps.`,
4646
}
4747

4848
func init() {
49-
log.Debug("Init apps")
49+
slog.Debug("Init apps")
5050

5151
rootCmd.AddCommand(appsCmd)
5252
}

‎cmd/apps_changelog.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ package cmd
33
import (
44
"errors"
55
"fmt"
6+
"log/slog"
67

78
helper "github.com/home-assistant/cli/client"
8-
log "github.com/sirupsen/logrus"
99
"github.com/spf13/cobra"
1010
)
1111

@@ -22,7 +22,7 @@ ha apps changelog core_mosquitto`,
2222
ValidArgsFunction: appsCompletions,
2323
Args: cobra.ExactArgs(1),
2424
Run: func(cmd *cobra.Command, args []string) {
25-
log.WithField("args", args).Debug("apps changelog")
25+
slog.Debug("apps changelog", "args", args)
2626

2727
section := "addons"
2828
command := "{slug}/changelog"
@@ -48,7 +48,7 @@ ha apps changelog core_mosquitto`,
4848
// returns 200 OK or 400, everything else is wrong
4949
if err == nil && resp.StatusCode() != 200 && resp.StatusCode() != 400 {
5050
err = errors.New("unexpected server response")
51-
log.Error(err)
51+
slog.Error("unexpected server response", "status", resp.StatusCode())
5252
}
5353

5454
if err != nil {

‎cmd/apps_info.go‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package cmd
22

33
import (
4+
"log/slog"
5+
46
helper "github.com/home-assistant/cli/client"
5-
log "github.com/sirupsen/logrus"
67
"github.com/spf13/cobra"
78
)
89

@@ -21,7 +22,7 @@ is provided, information about a specific app.
2122
ValidArgsFunction: appsCompletions,
2223
Args: cobra.MaximumNArgs(1),
2324
Run: func(cmd *cobra.Command, args []string) {
24-
log.WithField("args", args).Debug("apps info")
25+
slog.Debug("apps info", "args", args)
2526

2627
section := "addons"
2728
command := "{slug}/info"

‎cmd/apps_install.go‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package cmd
22

33
import (
4+
"log/slog"
5+
46
helper "github.com/home-assistant/cli/client"
5-
log "github.com/sirupsen/logrus"
67
"github.com/spf13/cobra"
78
)
89

@@ -19,7 +20,7 @@ This command allows you to install a Home Assistant app from the commandline.
1920
ValidArgsFunction: storeAppCompletions,
2021
Args: cobra.ExactArgs(1),
2122
Run: func(cmd *cobra.Command, args []string) {
22-
log.WithField("args", args).Debug("apps install")
23+
slog.Debug("apps install", "args", args)
2324

2425
section := "addons"
2526
command := "{slug}/install"

‎cmd/apps_logs.go‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package cmd
22

33
import (
4+
"log/slog"
5+
46
helper "github.com/home-assistant/cli/client"
5-
log "github.com/sirupsen/logrus"
67
"github.com/spf13/cobra"
78
)
89

@@ -19,7 +20,7 @@ Allowing you to look at the log output generated by a Home Assistant app.
1920
ValidArgsFunction: appsCompletions,
2021
Args: cobra.ExactArgs(1),
2122
Run: func(cmd *cobra.Command, args []string) {
22-
log.WithField("args", args).Debug("apps logs")
23+
slog.Debug("apps logs", "args", args)
2324

2425
section := "addons/{slug}"
2526

‎cmd/apps_rebuild.go‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package cmd
22

33
import (
4+
"log/slog"
5+
46
helper "github.com/home-assistant/cli/client"
5-
log "github.com/sirupsen/logrus"
67
"github.com/spf13/cobra"
78
)
89

@@ -24,7 +25,7 @@ of apps. This command allows you to trigger a rebuild of a locally built app.
2425
ValidArgsFunction: appsCompletions,
2526
Args: cobra.ExactArgs(1),
2627
Run: func(cmd *cobra.Command, args []string) {
27-
log.WithField("args", args).Debug("apps rebuild")
28+
slog.Debug("apps rebuild", "args", args)
2829

2930
section := "addons"
3031
command := "{slug}/rebuild"

‎cmd/apps_restart.go‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package cmd
22

33
import (
4+
"log/slog"
5+
46
helper "github.com/home-assistant/cli/client"
5-
log "github.com/sirupsen/logrus"
67
"github.com/spf13/cobra"
78
)
89

@@ -19,7 +20,7 @@ Restart a Home Assistant app
1920
ValidArgsFunction: appsCompletions,
2021
Args: cobra.ExactArgs(1),
2122
Run: func(cmd *cobra.Command, args []string) {
22-
log.WithField("args", args).Debug("apps restart")
23+
slog.Debug("apps restart", "args", args)
2324

2425
section := "addons"
2526
command := "{slug}/restart"

0 commit comments

Comments
 (0)